Explain Codes LogoExplain Codes Logo

Gradle proxy configuration

java
gradle
proxy
configuration
Anton ShumikhinbyAnton Shumikhin·Sep 30, 2024
TLDR

Configure Gradle to use a proxy by setting system properties in gradle.properties:

systemProp.http.proxyHost=proxy.example.com systemProp.http.proxyPort=8080 systemProp.https.proxyHost=proxy.example.com systemProp.https.proxyPort=8080

For authentication with the proxy, include:

systemProp.http.proxyUser=username systemProp.http.proxyPassword=password systemProp.https.proxyUser=username systemProp.https.proxyPassword=password

These settings instruct Gradle to direct all HTTP and HTTPS traffic through the given proxy server, using the provided username and password for authenticated proxies.

Mastering specials: characters and exceptions

For special characters in the username or password, encode them appropriately:

systemProp.http.proxyUser='domain\\username' // Backslashes call for reinforcements - use double '\\' systemProp.http.proxyPassword='passwordWith$pecial' // Got dollars in your password? Keep 'em safe in single quotes.

These settings handle Active Directory usernames including backslashes, and passwords with special characters like dollar signs.

To bypass the proxy for specific hosts, use the nonProxyHosts property:

systemProp.http.nonProxyHosts=*.local|localhost|10.* // 'Cause not every host needs to go through thorough security checks 😉

These establishments (domains or IP ranges) get a VIP, direct-access pass, bypassing the proxy.

Tweaking the nuts and bolts

Custom setup for individual projects

For project-specific proxy configurations, place a gradle.properties file in the root directory:

# VIP treatment for a specific project 👑 systemProp.http.proxyHost=custom-proxy.example.com systemProp.http.proxyPort=8081

Proxy via environment variables

Proxy configuration can be drawn from environment variables by creating a task in build.gradle:

tasks.register('setHttpProxyFromEnv') { doLast { def proxyHost = System.getenv('HTTP_PROXY_HOST') def proxyPort = System.getenv('HTTP_PROXY_PORT') if (proxyHost != null && proxyPort != null) { System.setProperty('http.proxyHost', proxyHost) System.setProperty('http.proxyPort', proxyPort) } } }

Ensure this task runs before any others to set the proxy right out the front door.

Fixing common HTTP issues: 407, 502

HTTP errors like 407 and 502 often mean your proxy setup has hiccupped. Double-check your proxyHost, proxyPort, proxyUser, and proxyPassword in the gradle.properties file. If Sheriff IT gives your credentials the nod, you're all set.

Settings, checks, and fine tunes

Separate proxy for Gradle Wrapper

Sometimes, the Gradle wrapper's whims may be different. In such cases, proxy settings are stated in gradle-wrapper.properties:

systemProp.http.proxyHost=wrapper-proxy.example.com systemProp.http.proxyPort=8082

Be sure these abide by the Gradle wrapper's tastes & your network's rules.

Proxy settings: Gradle & system environment

To avoid a proxy tug-of-war and build failures, have your system's environment proxy settings match Gradle's:

echo $HTTP_PROXY echo $HTTPS_PROXY

Make these match the gradle.properties settings for seamless navigation.

Proxies in complex setups

For sophisticated setups needing multiple proxies or dynamic switching, write custom tasks or rope in network admins to raft through the minefield.