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

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