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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 ant.
  25. Consult your IDE documentation for IDE-specific information upon proxy setup.
  26. </p>
  27. <p>
  28. All tasks 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 explictly 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 somewhat disappointing.
  63. </p>
  64. <p>
  65. We are not entirely sure where it reads the property settings from.
  66. For windows, it probably reads the appropriate bits of the registry. For
  67. Unix/Linux it may use the current Gnome2 settings.
  68. <p>
  69. One limitation of this feature, other than requiring a 1.5+ JVM,
  70. is that it is not dynamic. A long-lasting build hosted on a laptop will
  71. not adapt to changes in proxy settings.
  72. </p>
  73. <p>
  74. It is has also been reported a breaking the IBM Java 5 JRE on AIX,
  75. and does not appear to work reliably on Linux.
  76. Other odd things can go wrong, like Oracle JDBC drivers or pure Java SVN clients.
  77. </p>
  78. <p>
  79. To make the <code>-autproxy</code> option the default, add it to the environment variable
  80. <code>ANT_ARGS</code>, which contains a list of arguments to pass to Ant on every
  81. command line run.
  82. </p>
  83. <h3>JVM options</h3>
  84. <p>
  85. Any JVM can have its proxy options explicitly configured by passing
  86. the appropriate <code>-D</code> system property options to the runtime.
  87. Ant can be configured through all its shell scripts via the
  88. <code>ANT_OPTS</code> environment variable, which is a list of options to
  89. supply to Ant's JVM:
  90. </p>
  91. <p>
  92. For bash:
  93. </p>
  94. <pre>
  95. export ANT_OPTS="-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  96. </pre>
  97. For csh/tcsh:
  98. <pre>
  99. setenv ANT_OPTS "-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
  100. </pre>
  101. <p>
  102. For Windows, set the ANT_OPTS environment variable in the appropriate "MyComputer"
  103. properties dialog box.
  104. </p>
  105. <p>
  106. This mechanism works across Java versions, is cross-platform and reliable.
  107. Once set, all build files run via the command line will automatically have
  108. their proxy setup correctly, without needing any build file changes. It also
  109. apparently overrides Ant's automatic proxy settings options.
  110. </p>
  111. <p>
  112. It is limited in the following ways:
  113. </p>
  114. <ol>
  115. <li>Does not work under IDEs. These need their own proxy settings changed</li>
  116. <li>Not dynamic enough to deal with laptop configuration changes.</li>
  117. </ol>
  118. <h3>SetProxy Task</h3>
  119. <p>
  120. The <a href="OptionalTasks/setproxy.html">setproxy task</a> can be used to
  121. explicitly set a proxy in a build file. This manipulates the many proxy
  122. configuration properties of a JVM, and controls the proxy settings for all
  123. network operations in the same JVM from that moment.
  124. </p>
  125. <p>
  126. If you have a build file that is only to be used in-house, behind a firewall, on
  127. an older JVM, <i>and you cannot change Ant's JVM proxy settings</i>, then
  128. this is your best option. It is ugly and brittle, because the build file now contains
  129. system configuration information. It is also hard to get this right across
  130. the many possible proxy options of different users (none, HTTP, SOCKS).
  131. </p>
  132. <p>
  133. Note that proxy configurations set with this task will probably override
  134. any set by other mechanisms. It can also be used with fancy tricks to
  135. only set a proxy if the proxy is considered reachable:
  136. </p>
  137. <pre>
  138. &lt;target name="probe-proxy" depends="init"&gt;
  139. &lt;condition property="proxy.enabled"&gt;
  140. &lt;and&gt;
  141. &lt;isset property="proxy.host"/&gt;
  142. &lt;isreachable host="${proxy.host}"/&gt;
  143. &lt;/and&gt;
  144. &lt;/condition&gt;
  145. &lt;/target&gt;
  146. &lt;target name="proxy" depends="probe-proxy" if="proxy.enabled"&gt;
  147. &lt;property name="proxy.port" value="80"/&gt;
  148. &lt;property name="proxy.user" value=""/&gt;
  149. &lt;property name="proxy.pass" value=""/&gt;
  150. &lt;setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
  151. proxyuser="${proxy.user}" proxypassword="${proxy.pass}"/&gt;
  152. &lt;/target&gt;
  153. </pre>
  154. <h3>Summary and conclusions</h3>
  155. <p>
  156. There are three ways to set up proxies in Ant.
  157. </p>
  158. <ol>
  159. <li>With Ant1.7 using the <code>-autoproxy</code> parameter.</li>
  160. <li>Via JVM system properties -set these in the ANT_ARGS environment variable.</li>
  161. <li>Via the &lt;setproxy&gt; task.</li>
  162. </ol>
  163. <h4>Further reading</h4>
  164. <ul>
  165. <li><a href="http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html">
  166. Java Networking Properties</a>. Notice how not all proxy settings are documented
  167. there.
  168. <li><a href="http://blogs.sun.com/roller/resources/jcc/Proxies.pdf">Proxies</a>
  169. </li>
  170. </ul>
  171. </body>
  172. </html>