Configuring a proxy for Gradle is essential when working behind firewalls or in corporate networks that restrict direct internet access. Proper proxy setup ensures your builds can fetch dependencies, plugins, and other resources smoothly without interruptions. This guide walks you through the entire process, from initial setup to troubleshooting common issues, so you can maintain efficient and reliable Gradle builds in restricted network environments.
What You’ll Need
- Access to your system’s environment variables or the ability to modify Gradle configuration files.
- Proxy server address, port, and optionally, username and password if authentication is required.
- Basic knowledge of Gradle build files and environment configuration.
Step-by-Step
- Identify Your Proxy Details: Obtain your proxy server’s address (e.g., proxy.company.com), port (e.g., 8080), and credentials if needed. This information is typically provided by your network administrator.
- Set Environment Variables (Optional but Recommended): Configure your system environment variables for HTTP and HTTPS proxies. On Linux/macOS, add these to your shell profile (e.g., ~/.bashrc or ~/.zshrc):
export http_proxy=http://username:password@proxy.company.com:8080 export https_proxy=https://username:password@proxy.company.com:8080On Windows, set environment variables via System Properties or Command Prompt:
setx HTTP_PROXY http://username:password@proxy.company.com:8080 setx HTTPS_PROXY https://username:password@proxy.company.com:8080 - Configure Gradle Properties: Modify your Gradle configuration to include proxy settings. You can do this in your
gradle.propertiesfile, located in the project root or in theUSER_HOME/.gradledirectory.systemProp.http.proxyHost=proxy.company.com systemProp.http.proxyPort=8080 systemProp.https.proxyHost=proxy.company.com systemProp.https.proxyPort=8080If your proxy requires authentication, add:
systemProp.http.proxyUser=your_username systemProp.http.proxyPassword=your_password systemProp.https.proxyUser=your_username systemProp.https.proxyPassword=your_password - Configure Proxy for Gradle Daemon (Optional): To ensure the proxy settings persist across daemon restarts, include the above in your
gradle.propertiesfile. Restart your terminal or IDE to apply changes. - Verify Proxy Settings: Run a Gradle build or dependency fetch command (e.g.,
gradle build) and observe if dependencies are downloaded successfully. If issues occur, check logs for proxy-related errors. - Adjust as Needed: If you encounter issues, verify proxy details, check for typos, ensure credentials are correct, and confirm network connectivity to the proxy server.
Common Mistakes
- Forgetting to include proxy settings in
gradle.propertiesor environment variables. - Using incorrect proxy host or port.
- Not including authentication details when required by the proxy server.
- Misplacing configuration files or editing the wrong
gradle.propertiesfile. - Failing to restart the terminal or IDE after changing environment variables or configuration files.
- Using HTTP proxy settings for HTTPS connections without proper SSL configuration, leading to SSL errors.
Tips
- Always verify proxy details with your network administrator before configuration.
- Use environment variables for global proxy settings, especially if multiple tools rely on the same proxy.
- Securely store credentials, and avoid hardcoding passwords in configuration files when possible. Use credential managers or encrypted files.
- Test your proxy configuration with simple commands like
curlbefore applying to Gradle. - Update your proxy settings if your network configuration changes, to prevent build failures.
- Use verbose logging in Gradle (e.g.,
gradle --infoor--debug) to diagnose proxy issues.
Troubleshooting
If your Gradle builds are failing due to proxy issues, follow these steps:
- Check Proxy Connectivity: Verify that your system can reach the proxy server. Use commands like
ping proxy.company.comortelnet proxy.company.com 8080. - Test Proxy with Curl or Browser: Use
curlwith proxy settings to confirm access:curl -x http://proxy.company.com:8080 http://www.google.com. If this fails, the issue is with network or proxy configuration. - Review Gradle Logs: Run your build with increased verbosity:
gradle --infoorgradle --debug. Look for proxy-related errors such as SSL handshake failures or connection timeouts. - Validate Configuration Files: Ensure that
gradle.propertieshas correct syntax and values. Remove or comment out previous proxy settings if they conflict. - Update or Remove Proxy Settings: Temporarily disable proxy settings to verify if the issue is proxy-related. If builds succeed without proxy, recheck your proxy configuration.
- Check SSL/TLS Settings: For HTTPS proxies, ensure your JVM trusts the proxy’s SSL certificate. You might need to import the proxy’s SSL certificate into your JVM trust store.
- Consult Network Administrators: If issues persist, seek assistance to confirm proxy credentials, firewall rules, and network restrictions.
Frequently Asked Questions
1. Do I need to set proxy settings if I’m behind a corporate firewall?
Yes, setting proxy configurations in Gradle ensures it can access external repositories and plugins behind a firewall.
2. Can I set proxy settings globally for all Java applications?
Yes, by configuring system environment variables like http_proxy and https_proxy, most Java applications, including Gradle, will use these settings.
3. What if my proxy requires authentication?
Include your username and password in the proxy URL or set systemProp.proxyUser and systemProp.proxyPassword in gradle.properties.
4. How do I handle proxy issues with SSL certificates?
If SSL errors occur, import the proxy’s SSL certificate into your JVM trust store or disable SSL verification temporarily (not recommended for production).
5. Can I configure proxy settings per project?
Yes, by placing proxy configurations in the project’s gradle.properties file, you can override global settings for specific projects.
6. What should I do if my proxy settings work in browsers but not in Gradle?
Ensure Gradle’s proxy configurations are correct and that environment variables are set properly. Browser settings do not automatically propagate to Gradle.