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 7.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
  5. <title>Proxy Configuration</title>
  6. </head>
  7. <body>
  8. <h2>Proxy Configuration</h2>
  9. <p>
  10. All tasks running in Ant's JVM share the same HTTP/FTP/Socks
  11. proxy configuration.
  12. </p>
  13. <p>
  14. When any task tries to retrieve content from an HTTP page, including the
  15. <code>&lt;get&gt;</code> task, any automated URL retrieval in
  16. an XML/XSL task, or any third-party task that uses the <code>java.net.URL</code>
  17. classes, the proxy settings may make the difference between success and failure.
  18. </p>
  19. <p>
  20. Anyone authoring a build file behind a blocking firewall will immediately appreciate
  21. the problems and may want to write a build file to deal with the problem, but
  22. users of third party build build files may find that the build file itself
  23. does not work behind the firewall.
  24. </p>
  25. <p>
  26. This is a longstanding problem with Java and Ant, which Java1.5 finally
  27. addresses. When Ant is run under Java1.5, it automatically sets the
  28. <code>java.net.useSystemProxies</code> system property, telling the JVM to
  29. switch to the OS-configured proxy settings.This is automatic, but it can be disabled.
  30. </p>
  31. <p>
  32. When running Ant on older JVMs, this property is ignored. There are two other
  33. ways to configure Ant's proxy settings in these cases.
  34. </p>
  35. <h3>Java1.5 automatic proxy support (new for Ant1.7)</h3>
  36. <p>
  37. When Ant starts up, it automatically sets the
  38. <code>java.net.useSystemProxies</code> system property. This tells
  39. a Java1.5+ JVM to use the current set of property settings of the host
  40. environment. Other JVMs, such as the Kaffe and Apache Harmony runtimes,
  41. may also use this property in future, which is why Ant always sets the
  42. property -it is ignored on the Java1.4 and earlier runtimes.
  43. </p>
  44. <p>
  45. This property should be enough to automatically give Ant hosted
  46. builds network access. It may also work under an IDE, though that depends
  47. upon the IDE and how it starts ant. If it bypasses Ant's Main entry point,
  48. the proxy setup may be skipped, and if networking has already started up
  49. by the time ant is run, the option may be ignored. Consult your IDE
  50. documentation for IDE-specific information upon proxy setup.
  51. </p>
  52. <p>
  53. To disable this automatic feature, set the command line option
  54. <tt>-noproxy</tt>, or set a JVM or Ant property
  55. <code>java.net.useSystemProxies</code> to a value other than
  56. <code>true</code> or <code>on</code>. If the JVM option is already set,
  57. Ant will not touch it; if an Ant property of that name is set, Ant will
  58. pass the value of that property down to the JVM.
  59. </p>
  60. <p>
  61. We are not entirely sure where it reads the property settings from.
  62. For windows, it probably reads the appropriate bits of the registry. For
  63. Unix/Linux it may use the current Gnome2 settings.
  64. <p>
  65. The biggest limitation of this feature, other than requiring a 1.5+ JVM,
  66. is that it is not dynamic. A long-lasting build hosted on a laptop will
  67. not adapt to changes in proxy settings. Furthermore, unless the user
  68. is running <a
  69. href="http://www.hpl.hp.com/techreports/2001/HPL-2001-158.html">an adaptive
  70. laptop</a>, the host's proxy settings will not-automatically adapt to
  71. changes in network context. The <code>-noproxy</code> option may be a useful
  72. one to use when roaming.
  73. </p>
  74. <h3>JVM options</h3>
  75. <p>
  76. Any JVM can have its proxy options explicitly configured by passing
  77. the appropriate <code>-D</code> system property options to the runtime.
  78. Ant can be configured through all its shell scripts via the
  79. <code>ANT_OPTS</code> environment variable, which is a list of options to
  80. supply to Ant's JVM:
  81. </p>
  82. <p>
  83. For bash:
  84. </p>
  85. <pre>
  86. export ANT_OPTS="-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  87. </pre>
  88. For csh/tcsh:
  89. <pre>
  90. setenv ANT_OPTS "-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  91. </pre>
  92. <p>
  93. For Windows, set the ANT_OPTS environment variable in the appropriate "MyComputer"
  94. properties dialog box.
  95. </p>
  96. <p>
  97. This mechanism works across Java versions, is cross-platform and reliable.
  98. Once set, all build files run via the command line will automatically have
  99. their proxy setup correctly, without needing any build file changes. It also
  100. apparently overrides ants automatic proxy settings options
  101. </p>
  102. <p>
  103. It is limited in the following ways:
  104. </p>
  105. <ol>
  106. <li>Does not work under IDEs. These need their own proxy settings changed</li>
  107. <li>Not dynamic enough to deal with laptop configuration changes.</li>
  108. </ol>
  109. <p>
  110. If you are using Ant on a pre-Java1.5 machine behind a firewall, this is
  111. the simplest and best way to configure your runtime to work with build files
  112. that go beyond the firewall.
  113. </p>
  114. <h3>SetProxy Task</h3>
  115. <p>
  116. The <a href="OptionalTasks/setproxy.html">setproxy task</a> can be used to
  117. explicitly set a proxy in a build file. This manipulates the many proxy
  118. configuration properties of a JVM, and controls the proxy settings for all
  119. network operations in the same JVM from that moment.
  120. </p>
  121. <p>
  122. If you have a build file that is only to be used in-house, behind a firewall, on
  123. an older JVM, <i>and you cannot change Ant's JVM proxy settings</i>, then
  124. this is your best option. It is ugly and brittle, because the build file now contains
  125. system configuration information. It is also hard to get this right across
  126. the many possible proxy options of different users (none, HTTP, SOCKS).
  127. </p>
  128. <p>
  129. Note that proxy configurations set with this task will probably override
  130. any set by other mechanisms. It can also be used with fancy tricks to
  131. only set a proxy if the proxy is considered reachable:
  132. </p>
  133. <pre>
  134. &lt;target name="probe-proxy" depends="init"&gt;
  135. &lt;condition property="proxy.enabled"&gt;
  136. &lt;and&gt;
  137. &lt;isset property="proxy.host"/&gt;
  138. &lt;isreachable host="${proxy.host}"/&gt;
  139. &lt;/and&gt;
  140. &lt;/condition&gt;
  141. &lt;/target&gt;
  142. &lt;target name="proxy" depends="probe-proxy" if="proxy.enabled"&gt;
  143. &lt;property name="proxy.port" value="80"/&gt;
  144. &lt;property name="proxy.user" value=""/&gt;
  145. &lt;property name="proxy.pass" value=""/&gt;
  146. &lt;setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
  147. proxyuser="${proxy.user}" proxypassword="${proxy.pass}"/&gt;
  148. &lt;/target&gt;
  149. </pre>
  150. <h3>Summary and conclusions</h3>
  151. <p>
  152. There are three ways to set up proxies in Ant.
  153. </p>
  154. <ol>
  155. <li>Automatically, with Ant1.7 -use <code>-noproxy</code> to disable this.</li>
  156. <li>Via JVM system properties -set these in the ANT_ARGS environment variable.</li>
  157. <li>Via the &lt;setproxy&gt; task.</li>
  158. </ol>
  159. <p>
  160. As Java1.5 adoption increases, automatic proxy support should become more
  161. widespread. For this reason, we would encourage users not to try and
  162. second-guess network configurations with setproxy, and instead to encourage
  163. users to move up to Ant1.7/Java1.5, and if they cannot, to set proxy options
  164. in the ANT_ARGS environment variable.
  165. </p>
  166. <h4>Further reading</h4>
  167. <ul>
  168. <li><a href="http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html">
  169. Java Networking Properties</a>. Notice how not all proxy settings are documented
  170. there.
  171. <li><a href="http://blogs.sun.com/roller/resources/jcc/Proxies.pdf">Proxies</a>
  172. </li>
  173. </ul>
  174. <hr>
  175. <p align="center">Copyright &copy; 2005 The Apache Software Foundation. All rights
  176. Reserved.</p>
  177. </body>
  178. </html>