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 kB

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