[java] Gradle proxy configuration

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.

This question is related to java proxy active-directory windows-server-2008 gradle

The answer is


If this issue with HTTP error 407 happened to selected packages only then the problem is not in the proxy setting and internet connection. You even may expose your PC to the internet through NAT and still will face this problem. Typically at the same time you can download desired packages in browser. The only solution I find: delete the .gradle folder in your profile (not in the project). After that sync the project, it will take a long time but restore everything.


Try the following:

gradle -Dhttp.proxyHost=yourProxy -Dhttp.proxyPort=yourPort -Dhttp.proxyUser=usernameProxy -Dhttp.proxyPassword=yourPassoword


In case my I try to set up proxy from android studio Appearance & Behaviour => System Settings => HTTP Proxy. But the proxy did not worked out so I click no proxy.

Checking NO PROXY will not remove the proxy setting from the gradle.properties(Global). You need to manually remove it.

So I remove all the properties starting with systemProp for example - systemProp.http.nonProxyHosts=*.local, localhost


There are 2 ways for using Gradle behind a proxy :

Add arguments in command line

(From Guillaume Berche's post)

Add these arguments in your gradle command :

-Dhttp.proxyHost=your_proxy_http_host -Dhttp.proxyPort=your_proxy_http_port

or these arguments if you are using https :

-Dhttps.proxyHost=your_proxy_https_host -Dhttps.proxyPort=your_proxy_https_port

Add lines in gradle configuration file

in gradle.properties add the following lines :

systemProp.http.proxyHost=your_proxy_http_host
systemProp.http.proxyPort=your_proxy_http_port
systemProp.https.proxyHost=your_proxy_https_host
systemProp.https.proxyPort=your_proxy_https_port

(for gradle.properties file location, please refer to official documentation https://docs.gradle.org/current/userguide/build_environment.html


EDIT : as said by @Joost : A small but important detail that I initially overlooked: notice that the actual host name does NOT contain http:// protocol part of the URL...


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


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).


An update to @sourcesimian 's and @kunal-b's answer which dynamically sets the username and password if configured in the system properties.

The following sets the username and password if provided or just adds the host and port if no username and password is set.

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]
            //Get proxyHost,port, username, and password from http system properties 
            // in the format http://username:password@proxyhost:proxyport
            def (val1,val2) = e.value.tokenize( '@' )
            def (val3,val4) = val1.tokenize( '//' )
            def(userName, password) = val4.tokenize(':')
            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())
            System.setProperty("${base}.proxyUser", userName.toString())
            System.setProperty("${base}.proxyPassword", password.toString())
        }
    }
}

Based on SourceSimian's response; this worked on Windows domain user accounts. Note that the Username does not have domain included,

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())
            System.setProperty("${base}.proxyUser", "Username")
            System.setProperty("${base}.proxyPassword", "Password")
        }
    }
}
build.dependsOn setHttpProxyFromEnv

If you are behind proxy and using eclipse, go to Window Menu --> Preferences --> General --> Network Connections. Select the Active Providers as 'Manual'.

Under Proxy entries section, click on HTTPS, click Edit and add proxy host & port. If username and password are required, give that as well. It worked for me!


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

Check out at c:\Users\your username\.gradle\gradle.properties:

systemProp.http.proxyHost=<proxy host>
systemProp.http.proxyPort=<proxy port>
systemProp.http.proxyUser=<proxy user>
systemProp.http.proxyPassword=<proxy password>
systemProp.http.nonProxyHosts=<csv of exceptions>
systemProp.https.proxyHost=<proxy host>
systemProp.https.proxyPort=<proxy port>
systemProp.https.proxyUser=<proxy user>
systemProp.https.proxyPassword=<proxy password>
systemProp.https.nonProxyHosts=<csv of exceptions seperated by | >

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

Create a file called gradle.properties inside the project folder where the build.gradle file is present. Add the following entry

     systemProp.http.proxyHost=proxy_url
     systemProp.http.proxyPort=proxy_port
     systemProp.http.proxyUser=USER
     systemProp.http.proxyPassword=PWD
     systemProp.https.proxyHost=proxy_url 
     systemProp.https.proxyPort=proxy_port
     systemProp.https.proxyUser=USER
     systemProp.https.proxyPassword=PWD

If you are using DNS for proxy then add it like systemProp.https.proxyHost=www.proxysite.com

For IP just specify the IP with out http:// or https://
Check gradle official doc for more details and setting up proxy at global level


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to proxy

Axios having CORS issue Running conda with proxy WebSockets and Apache proxy : how to configure mod_proxy_wstunnel? "Proxy server connection failed" in google chrome Set proxy through windows command line including login parameters Could not resolve all dependencies for configuration ':classpath' Problems using Maven and SSL behind proxy Using npm behind corporate proxy .pac git returns http error 407 from proxy after CONNECT Forwarding port 80 to 8080 using NGINX

Examples related to active-directory

Powershell: A positional parameter cannot be found that accepts argument "xxx" How to switch to another domain and get-aduser How can I verify if an AD account is locked? Powershell script to see currently logged in users (domain and machine) + status (active, idle, away) Querying Windows Active Directory server using ldapsearch from command line How to list AD group membership for AD users using input list? Import-Module : The specified module 'activedirectory' was not loaded because no valid module file was found in any module directory What are CN, OU, DC in an LDAP search? PowerShell script to return members of multiple security groups How do I get specific properties with Get-AdUser

Examples related to windows-server-2008

Multiple -and -or in PowerShell Where-Object statement The FastCGI process exited unexpectedly Import-Module : The specified module 'activedirectory' was not loaded because no valid module file was found in any module directory How can I enable the Windows Server Task Scheduler History recording? IIS7: A process serving application pool 'YYYYY' suffered a fatal communication error with the Windows Process Activation Service Gradle proxy configuration Installing Tomcat 7 as Service on Windows Server 2008 How to get current user who's accessing an ASP.NET application? ASP.Net which user account running Web Service on IIS 7? A process crashed in windows .. Crash dump location

Examples related to gradle

Gradle - Move a folder from ABC to XYZ A failure occurred while executing com.android.build.gradle.internal.tasks Gradle: Could not determine java version from '11.0.2' Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Failed to resolve: com.android.support:appcompat-v7:28.0 Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion Error - Android resource linking failed (AAPT2 27.0.3 Daemon #0)