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

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