Pages

Saturday, March 31, 2012

Gradle wrapper behind a proxy

I was trying to setup gradle wrapper to work with Teamcity behind a proxy but only to realize that gradle wrapper was not using the java proxy options I was providing in command line. I decided to tweak the gradlew.sh and gradlew.bat a little to make it use the proxy so that I can provide proxy options in teamcity console when I configure the build step using gradlew. I don’t have access to build agents to configure gradle with properties file. Insert the below code in batch and shell script at a place between the first line and last line (Just before the invocation of GradleWrapperMain). If you put below pieces of code at wrong place then this will not work.
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
.
.
.
@rem Add default JVM options by parsing command line. Anything starting with -D is option to java.
:SYSTEM_PROPS_EXTRACT
set _OPTION=%1
set prefix1=%_OPTION:~0,2%
if "%prefix1%" == "-D" (
SET DEFAULT_JVM_OPTS=%DEFAULT_JVM_OPTS% %_OPTION%=%2%
)
shift
if not "%~1"=="" goto SYSTEM_PROPS_EXTRACT

echo Default JVM Options: %DEFAULT_JVM_OPTS%
.
.
.
.
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS%
"-Dorg.gradle.appname=%APP_BASE_NAME%"
-classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

Shell Script changes

.
.
.
.
#Add default JVM options by parsing command line arguments.
#Anything starting with -D is passed to JVM also.
for var in "$@"
do
prefix=${var:0:2}
if [[ $prefix == "-D" ]] ; then
DEFAULT_JVM_OPTS=$DEFAULT_JVM_OPTS" "$var
fi
done

# Split up the JVM_OPTS And GRADLE_OPTS values into an array,
# following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"

echo "JVM_OPTS :""${JVM_OPTS[@]}"
.
.
.
exec "$JAVACMD" "${JVM_OPTS[@]}"
-classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"

And finally the proxy option to use gradlew build -Dhttp.proxyHost=www-xxx.com -Dhttp.proxyPort=80 -Dhttp.proxyUser=user1 -Dhttp.proxyPassword=user1 -Dhttp.auth.ntlm.domain=COMMON