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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 Apache 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 explicitly 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. The <code>java.net.useSystemProxies</code> is checked only
  77. once, at startup time, the other checks (registry, gconf, system properties) are done
  78. dynamically whenever needed (socket connection, URL connection etc..).
  79. </p>
  80. <h5>Windows</h5>
  81. <p>
  82. The JVM goes straight to the registry, bypassing WinInet, as it is not
  83. present/consistent on all supported Windows platforms (it is part of IE,
  84. really). Java 7 may use the Windows APIs on the platforms when it is present.
  85. </p>
  86. <h5>Linux</h5>
  87. <p>
  88. The JVM uses the gconf library to look at specific entries.
  89. The GConf-2 settings used are:
  90. </p>
  91. <pre>
  92. - /system/http_proxy/use_http_proxy boolean
  93. - /system/http_proxy/use_authentication boolean
  94. - /system/http_proxy/host string
  95. - /system/http_proxy/authentication_user string
  96. - /system/http_proxy/authentication_password string
  97. - /system/http_proxy/port int
  98. - /system/proxy/socks_host string
  99. - /system/proxy/mode string
  100. - /system/proxy/ftp_host string
  101. - /system/proxy/secure_host string
  102. - /system/proxy/socks_port int
  103. - /system/proxy/ftp_port int
  104. - /system/proxy/secure_port int
  105. - /system/proxy/no_proxy_for list
  106. - /system/proxy/gopher_host string
  107. - /system/proxy/gopher_port int
  108. </pre>
  109. <p>
  110. If you are using KDE or another GUI than Gnome, you can still use the
  111. <code>gconf-editor</code> tool to add these entries.
  112. </p>
  113. <h3>Manual JVM options</h3>
  114. <p>
  115. Any JVM can have its proxy options explicitly configured by passing
  116. the appropriate <code>-D</code> system property options to the runtime.
  117. Ant can be configured through all its shell scripts via the
  118. <code>ANT_OPTS</code> environment variable, which is a list of options to
  119. supply to Ant's JVM:
  120. </p>
  121. <p>
  122. For bash:
  123. </p>
  124. <pre>
  125. export ANT_OPTS="-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  126. </pre>
  127. For csh/tcsh:
  128. <pre>
  129. setenv ANT_OPTS "-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  130. </pre>
  131. <p>
  132. If you insert this line into the Ant shell script itself, it gets picked up
  133. by all continuous integration tools running on the system that call Ant via the
  134. command line.
  135. </p>
  136. <p>
  137. For Windows, set the <code>ANT_OPTS</code> environment variable in the appropriate "My Computer"
  138. properties dialog box (winXP), "Computer" properties (Vista)
  139. </p>
  140. <p>
  141. This mechanism works across Java versions, is cross-platform and reliable.
  142. Once set, all build files run via the command line will automatically have
  143. their proxy setup correctly, without needing any build file changes. It also
  144. apparently overrides Ant's automatic proxy settings options.
  145. </p>
  146. <p>
  147. It is limited in the following ways:
  148. </p>
  149. <ol>
  150. <li>Does not work under IDEs. These need their own proxy settings changed</li>
  151. <li>Not dynamic enough to deal with laptop configuration changes.</li>
  152. </ol>
  153. <h3>SetProxy Task</h3>
  154. <p>
  155. The <a href="Tasks/setproxy.html">setproxy task</a> can be used to
  156. explicitly set a proxy in a build file. This manipulates the many proxy
  157. configuration properties of a JVM, and controls the proxy settings for all
  158. network operations in the same JVM from that moment.
  159. </p>
  160. <p>
  161. If you have a build file that is only to be used in-house, behind a firewall, on
  162. an older JVM, <i>and you cannot change Ant's JVM proxy settings</i>, then
  163. this is your best option. It is ugly and brittle, because the build file now contains
  164. system configuration information. It is also hard to get this right across
  165. the many possible proxy options of different users (none, HTTP, SOCKS).
  166. </p>
  167. <p>
  168. Note that proxy configurations set with this task will probably override
  169. any set by other mechanisms. It can also be used with fancy tricks to
  170. only set a proxy if the proxy is considered reachable:
  171. </p>
  172. <pre>
  173. &lt;target name="probe-proxy" depends="init"&gt;
  174. &lt;condition property="proxy.enabled"&gt;
  175. &lt;and&gt;
  176. &lt;isset property="proxy.host"/&gt;
  177. &lt;isreachable host="${proxy.host}"/&gt;
  178. &lt;/and&gt;
  179. &lt;/condition&gt;
  180. &lt;/target&gt;
  181. &lt;target name="proxy" depends="probe-proxy" if="proxy.enabled"&gt;
  182. &lt;property name="proxy.port" value="80"/&gt;
  183. &lt;property name="proxy.user" value=""/&gt;
  184. &lt;property name="proxy.pass" value=""/&gt;
  185. &lt;setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
  186. proxyuser="${proxy.user}" proxypassword="${proxy.pass}"/&gt;
  187. &lt;/target&gt;
  188. </pre>
  189. <h3>Custom ProxySelector implementations</h3>
  190. <p>
  191. As Java lets developers write their own ProxySelector implementations, it
  192. is theoretically possible for someone to write their own proxy selector class that uses
  193. different policies to determine proxy settings. There is no explicit support
  194. for this in Ant, and it has not, to the team's knowledge, been attempted.
  195. </p>
  196. <p>
  197. This could be the most flexible of solutions, as one could easily imagine
  198. an Ant-specific proxy selector that was driven off ant properties, rather
  199. than system properties. Developers could set proxy options in their
  200. custom build.properties files, and have this propagate.
  201. </p>
  202. <p>
  203. One issue here is with concurrency: the default proxy selector is per-JVM,
  204. not per-thread, and so the proxy settings will apply to all sockets opened
  205. on all threads; we also have the problem of how to propagate options from
  206. one build to the JVM-wide selector.
  207. </p>
  208. <h3>Configuring the Proxy settings of Java programs under Ant</h3>
  209. <p>
  210. Any program that is executed with <code>&lt;java&gt;</code> without setting
  211. <code>fork="true"</code> will pick up the Ant's settings. If you need
  212. different values, set <code>fork="false"</code> and provide the values
  213. in <code>&lt;sysproperty&gt;</code> elements.
  214. </p>
  215. If you wish to have
  216. a forked process pick up the Ant's settings, use the
  217. <a href="Types/propertyset.html"><code>&lt;syspropertyset&gt;</code></a>
  218. element to propagate the normal proxy settings. The following propertyset
  219. is a datatype which can be referenced in a <code>&lt;java&gt;</code> task to
  220. pass down the current values.
  221. </p>
  222. <pre>
  223. &lt;propertyset id="proxy.properties">
  224. &lt;propertyref prefix="java.net.useSystemProxies"/>
  225. &lt;propertyref prefix="http."/>
  226. &lt;propertyref prefix="https."/>
  227. &lt;propertyref prefix="ftp."/>
  228. &lt;propertyref prefix="socksProxy"/>
  229. &lt;/propertyset>
  230. </pre>
  231. <h3>Summary and conclusions</h3>
  232. <p>
  233. There are four ways to set up proxies in Ant.
  234. </p>
  235. <ol>
  236. <li>With Ant1.7 and Java 1.5+ using the <code>-autoproxy</code> parameter.</li>
  237. <li>Via JVM system properties -set these in the ANT_ARGS environment variable.</li>
  238. <li>Via the &lt;setproxy&gt; task.</li>
  239. <li>Custom ProxySelector implementations</li>
  240. </ol>
  241. <p>
  242. Proxy settings are automatically shared with Java programs started under Ant <i>
  243. that are not forked</i>; to pass proxy settings down to subsidiary programs, use
  244. a propertyset.
  245. </p>
  246. <p>
  247. Over time, we expect the Java 5+ proxy features to stabilize, and for Java code
  248. to adapt to them. However, given the fact that it currently does break some
  249. builds, it will be some time before Ant enables the automatic proxy feature by
  250. default. Until then, you have to enable the <code>-autoproxy</code> option or
  251. use one of the alternate mechanisms to configure the JVM.
  252. <h4>Further reading</h4>
  253. <ul>
  254. <li><a href="http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html">
  255. Java Networking Properties</a>.
  256. </li>
  257. </ul>
  258. </body>
  259. </html>