You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

proxy.html 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
  19. <title>Proxy Configuration</title>
  20. </head>
  21. <body>
  22. <h2>Proxy Configuration</h2>
  23. <p>
  24. This page discussing proxy issues on command-line ant.
  25. Consult your IDE documentation for IDE-specific information upon proxy setup.
  26. </p>
  27. <p>
  28. All tasks and threads running in Ant's JVM share the same HTTP/FTP/Socks
  29. proxy configuration.
  30. </p>
  31. <p>
  32. When any task tries to retrieve content from an HTTP page, including the
  33. <code>&lt;get&gt;</code> task, any automated URL retrieval in
  34. an XML/XSL task, or any third-party task that uses the <code>java.net.URL</code>
  35. classes, the proxy settings may make the difference between success and failure.
  36. </p>
  37. <p>
  38. Anyone authoring a build file behind a blocking firewall will immediately appreciate
  39. the problems and may want to write a build file to deal with the problem, but
  40. users of third party build build files may find that the build file itself
  41. does not work behind the firewall.
  42. </p>
  43. <p>
  44. This is a long standing problem with Java and Ant. The only way to fix
  45. it is to explictly configure Ant with the proxy settings, either
  46. by passing down the proxy details as JVM properties, or to
  47. tell Ant on a Java1.5+ system to have the JVM work it out for itself.
  48. </p>
  49. <h3>Java1.5+ proxy support (new for Ant1.7)</h3>
  50. <p>
  51. When Ant starts up, if the <code>-autoproxy</code>
  52. command is supplied, Ant sets the
  53. <code>java.net.useSystemProxies</code> system property. This tells
  54. a Java1.5+ JVM to use the current set of property settings of the host
  55. environment. Other JVMs, such as the Kaffe and Apache Harmony runtimes,
  56. may also use this property in future.
  57. It is ignored on the Java1.4 and earlier runtimes.
  58. </p>
  59. <p>
  60. This property maybe enough to give command-line Ant
  61. builds network access, although in practise the results
  62. are inconsistent.
  63. </p>
  64. <p>
  65. It is has also been reported a breaking the IBM Java 5 JRE on AIX,
  66. and does not always work on Linux (presumably due to missing gconf settings)
  67. Other odd things can go wrong, like Oracle JDBC drivers or pure Java SVN clients.
  68. </p>
  69. <p>
  70. To make the <code>-autoproxy</code> option the default, add it to the environment variable
  71. <code>ANT_ARGS</code>, which contains a list of arguments to pass to Ant on every
  72. command line run.
  73. </p>
  74. <h4>How Autoproxy works</h4>
  75. <p>
  76. We are grateful for some input from Sun as to how the proxy code works under
  77. Java 1.5 and up. The <code>java.net.useSystemProxies</code> is checked only
  78. once, at startup time, the other checks (registry, gconf, system properties) are done
  79. dynamically whenever needed (socket connection, URL connection etc..).
  80. </p>
  81. <h5>Windows</h5>
  82. <p>
  83. The JVM goes straight to the registry, bypassing WinInet, as it is not
  84. present/consistent on all supported Windows platforms (it is part of IE,
  85. really). Java 7 may use the Windows APIs on the platforms when it is present.
  86. </p>
  87. <h5>Linux</h5>
  88. <p>
  89. The JVM uses the gconf library to look at specific entries.
  90. The GConf-2 settings used are:
  91. </p>
  92. <pre>
  93. - /system/http_proxy/use_http_proxy boolean
  94. - /system/http_proxy/use_authentication boolean
  95. - /system/http_proxy/host string
  96. - /system/http_proxy/authentication_user string
  97. - /system/http_proxy/authentication_password string
  98. - /system/http_proxy/port int
  99. - /system/proxy/socks_host string
  100. - /system/proxy/mode string
  101. - /system/proxy/ftp_host string
  102. - /system/proxy/secure_host string
  103. - /system/proxy/socks_port int
  104. - /system/proxy/ftp_port int
  105. - /system/proxy/secure_port int
  106. - /system/proxy/no_proxy_for list
  107. - /system/proxy/gopher_host string
  108. - /system/proxy/gopher_port int
  109. </pre>
  110. <p>
  111. If you are using KDE or another GUI than Gnome, you can still use the
  112. <code>gconf-editor</code> tool to add these entries.
  113. </p>
  114. <h3>Manual JVM options</h3>
  115. <p>
  116. Any JVM can have its proxy options explicitly configured by passing
  117. the appropriate <code>-D</code> system property options to the runtime.
  118. Ant can be configured through all its shell scripts via the
  119. <code>ANT_OPTS</code> environment variable, which is a list of options to
  120. supply to Ant's JVM:
  121. </p>
  122. <p>
  123. For bash:
  124. </p>
  125. <pre>
  126. export ANT_OPTS="-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  127. </pre>
  128. For csh/tcsh:
  129. <pre>
  130. setenv ANT_OPTS "-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  131. </pre>
  132. <p>
  133. If you insert this line into the Ant shell script itself, it gets picked up
  134. by all continuous integration tools running on the system that call Ant via the
  135. command line.
  136. </p>
  137. <p>
  138. For Windows, set the <code>ANT_OPTS</code> environment variable in the appropriate "My Computer"
  139. properties dialog box (winXP), "Computer" properties (Vista)
  140. </p>
  141. <p>
  142. This mechanism works across Java versions, is cross-platform and reliable.
  143. Once set, all build files run via the command line will automatically have
  144. their proxy setup correctly, without needing any build file changes. It also
  145. apparently overrides Ant's automatic proxy settings options.
  146. </p>
  147. <p>
  148. It is limited in the following ways:
  149. </p>
  150. <ol>
  151. <li>Does not work under IDEs. These need their own proxy settings changed</li>
  152. <li>Not dynamic enough to deal with laptop configuration changes.</li>
  153. </ol>
  154. <h3>SetProxy Task</h3>
  155. <p>
  156. The <a href="OptionalTasks/setproxy.html">setproxy task</a> can be used to
  157. explicitly set a proxy in a build file. This manipulates the many proxy
  158. configuration properties of a JVM, and controls the proxy settings for all
  159. network operations in the same JVM from that moment.
  160. </p>
  161. <p>
  162. If you have a build file that is only to be used in-house, behind a firewall, on
  163. an older JVM, <i>and you cannot change Ant's JVM proxy settings</i>, then
  164. this is your best option. It is ugly and brittle, because the build file now contains
  165. system configuration information. It is also hard to get this right across
  166. the many possible proxy options of different users (none, HTTP, SOCKS).
  167. </p>
  168. <p>
  169. Note that proxy configurations set with this task will probably override
  170. any set by other mechanisms. It can also be used with fancy tricks to
  171. only set a proxy if the proxy is considered reachable:
  172. </p>
  173. <pre>
  174. &lt;target name="probe-proxy" depends="init"&gt;
  175. &lt;condition property="proxy.enabled"&gt;
  176. &lt;and&gt;
  177. &lt;isset property="proxy.host"/&gt;
  178. &lt;isreachable host="${proxy.host}"/&gt;
  179. &lt;/and&gt;
  180. &lt;/condition&gt;
  181. &lt;/target&gt;
  182. &lt;target name="proxy" depends="probe-proxy" if="proxy.enabled"&gt;
  183. &lt;property name="proxy.port" value="80"/&gt;
  184. &lt;property name="proxy.user" value=""/&gt;
  185. &lt;property name="proxy.pass" value=""/&gt;
  186. &lt;setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
  187. proxyuser="${proxy.user}" proxypassword="${proxy.pass}"/&gt;
  188. &lt;/target&gt;
  189. </pre>
  190. <h3>Custom ProxySelector implementations</h3>
  191. <p>
  192. As Java lets developers write their own ProxySelector implementations, it
  193. is theoretically possible for someone to write their own proxy selector class that uses
  194. different policies to determine proxy settings. There is no explicit support
  195. for this in Ant, and it has not, to the team's knowledge, been attempted.
  196. </p>
  197. <p>
  198. This could be the most flexible of solutions, as one could easily imagine
  199. an Ant-specific proxy selector that was driven off ant properties, rather
  200. than system properties. Developers could set proxy options in their
  201. custom build.properties files, and have this propagate.
  202. </p>
  203. <p>
  204. One issue here is with concurrency: the default proxy selector is per-JVM,
  205. not per-thread, and so the proxy settings will apply to all sockets opened
  206. on all threads; we also have the problem of how to propagate options from
  207. one build to the JVM-wide selector.
  208. </p>
  209. <h3>Configuring the Proxy settings of Java programs under Ant</h3>
  210. <p>
  211. Any program that is executed with <code>&lt;java&gt;</code> without setting
  212. <code>fork="true"</code> will pick up the Ant's settings. If you need
  213. different values, set <code>fork="false"</code> and provide the values
  214. in <code>&lt;sysproperty&gt;</code> elements.
  215. </p>
  216. If you wish to have
  217. a forked process pick up the Ant's settings, use the
  218. <a href="CoreTypes/propertyset.html"><code>&lt;syspropertyset&gt;</code></a>
  219. element to propagate the normal proxy settings. The following propertyset
  220. is a datatype which can be referenced in a <code>&lt;java&gt;</code> task to
  221. pass down the current values.
  222. </p>
  223. <pre>
  224. &lt;propertyset id="proxy.properties">
  225. &lt;propertyref prefix="java.net.useSystemProxies"/>
  226. &lt;propertyref prefix="http."/>
  227. &lt;propertyref prefix="https."/>
  228. &lt;propertyref prefix="ftp."/>
  229. &lt;propertyref prefix="socksProxy"/>
  230. &lt;/propertyset>
  231. </pre>
  232. <h3>Summary and conclusions</h3>
  233. <p>
  234. There are four ways to set up proxies in Ant.
  235. </p>
  236. <ol>
  237. <li>With Ant1.7 and Java 1.5+ using the <code>-autoproxy</code> parameter.</li>
  238. <li>Via JVM system properties -set these in the ANT_ARGS environment variable.</li>
  239. <li>Via the &lt;setproxy&gt; task.</li>
  240. <li>Custom ProxySelector implementations</li>
  241. </ol>
  242. <p>
  243. Proxy settings are automatically shared with Java programs started under Ant <i>
  244. that are not forked</i>; to pass proxy settings down to subsidiary programs, use
  245. a propertyset.
  246. </p>
  247. <p>
  248. Over time, we expect the Java 5+ proxy features to stabilize, and for Java code
  249. to adapt to them. However, given the fact that it currently does break some
  250. builds, it will be some time before Ant enables the automatic proxy feature by
  251. default. Until then, you have to enable the <code>-autoproxy</code> option or
  252. use one of the alternate mechanisms to configure the JVM.
  253. <h4>Further reading</h4>
  254. <ul>
  255. <li><a href="http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html">
  256. Java Networking Properties</a>. Notice how not all proxy settings are documented
  257. there.
  258. <li><a href="http://blogs.sun.com/roller/resources/jcc/Proxies.pdf">Proxies</a>
  259. </li>
  260. </ul>
  261. </body>
  262. </html>