Gradle proxy configuration

390,650

Solution 1

Using a very simple "Request a URL" Java program, I was able to replicate the issue.

http.proxyUser and http.proxyPassword seem to be non-standard, albeit popular, options, as they're not described in the Java reference page linked from the Gradle tutorial; even though the Gradle manual mentions them.

It seems Java programs that wish to support proxy authentication need to do this manually (and I was able to do this using the code on the linked page).


I submitted this issue (and a fix) to the Gradle issue tracker. Raised issue GRADLE-1556 was resolved in 1.0-milestone-8 (Feb 2012)

Solution 2

Refinement over Daniel's response:

HTTP Only Proxy configuration

gradlew -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=3128 "-Dhttp.nonProxyHosts=*.nonproxyrepos.com|localhost"

HTTPS Only Proxy configuration

gradlew -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=3129 "-Dhttp.nonProxyHosts=*.nonproxyrepos.com|localhost"

Both HTTP and HTTPS Proxy configuration

gradlew -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=3128 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=3129 "-Dhttp.nonProxyHosts=*.nonproxyrepos.com|localhost"

Proxy configuration with user and password

gradlew -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=3128 - Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=3129 -Dhttps.proxyUser=user -Dhttps.proxyPassword=pass -Dhttp.proxyUser=user -Dhttp.proxyPassword=pass -Dhttp.nonProxyHosts=host1.com|host2.com

worked for me (with gradle.properties in either homedir or project dir, build was still failing). Thanks for pointing the issue at gradle that gave this workaround. See reference doc at https://docs.gradle.org/current/userguide/build_environment.html#sec:accessing_the_web_via_a_proxy

Update You can also put these properties into gradle-wrapper.properties (see: https://stackoverflow.com/a/50492027/474034).

Solution 3

This is my gradle.properties, please note those HTTPS portion

systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=8118
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=8118

Solution 4

In my build.gradle I have the following task, which uses the usual linux proxy settings, HTTP_PROXY and HTTPS_PROXY, from the shell env:

task setHttpProxyFromEnv {
    def map = ['HTTP_PROXY': 'http', 'HTTPS_PROXY': 'https']
    for (e in System.getenv()) {
        def key = e.key.toUpperCase()
        if (key in map) {
            def base = map[key]
            def url = e.value.toURL()
            println " - systemProp.${base}.proxy=${url.host}:${url.port}"
            System.setProperty("${base}.proxyHost", url.host.toString())
            System.setProperty("${base}.proxyPort", url.port.toString())
        }
    }
}
build.dependsOn setHttpProxyFromEnv

Solution 5

For me, works adding this configuration in the gradle.properties file of the project, where the build.gradle file is:

systemProp.http.proxyHost=proxyURL
systemProp.http.proxyPort=proxyPort
systemProp.http.proxyUser=USER
systemProp.http.proxyPassword=PASSWORD
systemProp.https.proxyHost=proxyUrl 
systemProp.https.proxyPort=proxyPort
systemProp.https.proxyUser=USER
systemProp.https.proxyPassword=PASSWORD

Where : proxyUrl is the url of the proxy server (http://.....)

proxyPort is the port (usually 8080)

USER is my domain user

PASSWORD, my password

In this case, the proxy for http and https is the same

Share:
390,650

Related videos on Youtube

Malcolm Diggs
Author by

Malcolm Diggs

Updated on August 24, 2021

Comments

  • Malcolm Diggs
    Malcolm Diggs almost 2 years

    I need web access from Gradle through a proxy server to use the Gradle/Artifactory integration for Jenkins. To reduce possible causes for issues, I manually add the Artifactory plugin in build.gradle and run it from command line:

    apply {
        apply from: "http://gradle.artifactoryonline.com/gradle/plugins/org/jfrog/buildinfo/build-info-extractor-gradle/1.0.1/artifactoryplugin-1.0.1.gradle"
    }
    

    Following this description I specified the following in .gradle/gradle.properties in my home directory:

    systemProp.http.proxyHost=hostname
    systemProp.http.proxyPort=8080
    systemProp.http.proxyUser=de\\username
    systemProp.http.proxyPassword=xxx
    

    With the above proxy configuration (that is otherwise known to work), it fails:

    11:33:17.699 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.io.IOException: Server returned HTTP response code: 407 for URL: http://gradle.artifactoryonline.com/gradle/plugins/org/jfrog/buildinfo/build-info-extractor-gradle/1.0.1/artifactoryplugin-1.0.1.gradle

    I have two proxy servers to choose from, and one always responds with 407 (Proxy authentication required), the other with 502 (Bad gateway), so obviously, the proxyHost and proxyPort options are used.

    As the user name (based on an Active Directory user) contains a backslash, I tried both \\ and \, but neither worked. The user specified is different from the user that is logged in to the machine and Active Directory. This user's credentials aren't valid for the proxy, so I need to be able to specify a different user.

    Setting the same options in Jenkins' or Artifactory's GUI worked.

    • hkoosha
      hkoosha over 7 years
      having the same problem, none of the answers here helped (even those following the deprecated/non-deprecated methods). then I realized https proxy MUST be set: stackoverflow.com/a/27686730/1024839
    • Guillaume Husta
      Guillaume Husta over 6 years
      You should look at this url, more complete and updated (with http and https) : docs.gradle.org/current/userguide/…
    • Malcolm Diggs
      Malcolm Diggs over 6 years
      @GuillaumeHusta Thanks, but please note this question was posted more than 5 years ago when Gradle 0.8 was the latest release, and the documentation on this was just plain wrong (see my own response to this question).
    • ir2pid
      ir2pid almost 4 years
      I had this problem when using quotes in the proxy, use systemProp.http.proxyHost=x.y.z not systemProp.http.proxyHost='x.y.z'
    • observer
      observer over 3 years
  • Mike Yockey
    Mike Yockey almost 12 years
    The linked block comes up for me as invitation-only. Is there a public example of this workaround?
  • Malcolm Diggs
    Malcolm Diggs almost 12 years
    @yock See the attachment of the issue I submitted to Gradle, it's very similar.
  • Łukasz Siwiński
    Łukasz Siwiński about 9 years
    helped me too when using cntlm
  • Malcolm Diggs
    Malcolm Diggs over 8 years
    Consider when this question was asked. The issue I link to in my answer was marked resolved towards Gradle 1.0-milestone-8…
  • Jono
    Jono about 8 years
    how do u remove proxy?
  • Miao1007
    Miao1007 over 7 years
    It's fun! It is your root's build.gradle or app's build.gradle?
  • Owen B
    Owen B about 7 years
    also -Dhttps.proxyUser=user -Dhttps.proxyPassword=pass
  • akirti
    akirti about 7 years
    in which file we add this settings do we have any file like gradlew.properties? or it goes with gradle.properties file?
  • Stefan Haberl
    Stefan Haberl about 6 years
    If you put the properties into ~/.gradle/gradle.properties make sure you prefix the properties with systemProp, like for example systemProp.http.proxyHost=127.0.0.1
  • Admin
    Admin over 5 years
    Works on Windows 7 as well! This should be the accepted answer!
  • Betlista
    Betlista over 5 years
    @Daniel this question was first thing that popped up, when I searched for "gradle proxy", I skimmed the answers and that one was the clearest one for my need...
  • Norbert
    Norbert about 5 years
    Thanks! There is an update on how to get proxy username and passwords dynamically below
  • Norbert
    Norbert about 5 years
    Thanks! the above worked for configuration that doesn't need a proxy password. I have modified it to work with or w/o proxy password see below
  • lanoxx
    lanoxx almost 5 years
    @akirti: There is ~/.gradle/gradle-wrapper.properties which is read by gradlew. See: stackoverflow.com/a/50492027/474034
  • Max Vollmer
    Max Vollmer over 4 years
    @MurrayFoxcroft Where do you see a link in that answer?
  • Murray Foxcroft
    Murray Foxcroft over 4 years
    Basically a link to the local machine and a file dump. Please describe the answer in more detail.
  • GoldenD
    GoldenD over 4 years
    I tried this and didn't work for me. I had to set the options (-Dhttp.proxyHost and the likes) as JAVA_OPTS environment variable in order to make it work.
  • JustAnotherCoder
    JustAnotherCoder over 4 years
    if (key in map.keySet()) {
  • Avindra Goolcharan
    Avindra Goolcharan over 4 years
    best answer. wish the java ecosystem worked like this out of the box
  • Seyed Ali Roshan
    Seyed Ali Roshan about 4 years
    superrrrrr tnx, u may don't realize how useful ur post was for me but still big thanks
  • Joost
    Joost about 4 years
    A small but important detail that I initially overlooked: notice that the actual host name does NOT contain http:// protocol part of the URL...
  • zeehio
    zeehio over 3 years
    This should be a default (if it added the NO_PROXY env variable as well)
  • EpicPandaForce
    EpicPandaForce over 3 years
    I've been ripping my hair out for an hour, apparently on Mac, these properties were auto-added to my ~/.gradle/gradle.properties. Thanks for pointing it out.
  • Nullpointer
    Nullpointer about 3 years
    @SeyedAliRoshan How can we pass proxyUser and password by argument ? i dont want to user globle gradle.propaerties file.
  • Seyed Ali Roshan
    Seyed Ali Roshan about 3 years
    @Nullpointer did u try to use the proxy setting inside the gradle of your project?
  • Nullpointer
    Nullpointer about 3 years
    @SeyedAliRoshan yes, If I add all above variable on the gradle.properties then I can download gradle-wrapper pkgs from artifactory but those variable are not safe to keep in the server or project. So I have to pass variable for download the gradle-wrapper.
  • ben
    ben almost 3 years
    Add it to ~/.gradle/init.d/ as a gradle init script. This way, it will initialize all your gradle builds based on your env variables without having to copy the snippet from project to project.
  • Zordid
    Zordid about 1 year
    The most important info is ALWAYS missing - like here. These settings -D... need to be the FIRST and before the tasks to execute on the gradlew command! This is why some people (like me) struggle to make this work! With JAVA_OPTS and _JAVA_OPTIONS you don't have this problem... Please MENTION how important the order is!
  • Sonu kumar
    Sonu kumar about 1 year
    Thanks for your grate support it is working fine now!