Dynamic build dependencies

In my project I have some buildependencies that are conditional, I want to be able to build other tasks if these are not present.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        if(icegridguiProguard.toBoolean()) {
            classpath group: 'net.sourceforge', name: 'proguard', version: '5.0'
        }
    }
}

if(icegridguiProguard.toBoolean()) {
    // Build a proguard JAR
else {
    // Build a plain JAR
}

So when I build with -PofflineBuild=true I build a plain JAR and avoid proguard dependency, and for “online” builds I build a proguard JAR.

That doesn’t seems to work as I expected, if I first build with -PicegridguiProguard=true the plain JAR is build correctly, but if I now try to do a proguard build with -PicegridguiProguard=false.

Can somebody explain what is the problem with this approach and how to correctly define dynamic dependencies for the build script.

That is part of a more large build script see
https://github.com/zeroc-ice/ice/blob/3.6/java/src/IceGridGUI/build.gradle

and the complete error

    jose@amulet:~/Documents/ZeroC/ice3.6/java$ ./gradlew -PicegridguiProguard=false
Downloading https://services.gradle.org/distributions/gradle-2.6-bin.zip
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Unzipping /home/jose/.gradle/wrapper/dists/gradle-2.6-bin/7og9jkn1p1nxjafu1htp0hhvc/gradle-2.6-bin.zip to /home/jose/.gradle/wrapper/dists/gradle-2.6-bin/7og9jkn1p1nxjafu1htp0hhvc
Set executable permissions for: /home/jose/.gradle/wrapper/dists/gradle-2.6-bin/7og9jkn1p1nxjafu1htp0hhvc/gradle-2.6/bin/gradle
Starting a new Gradle Daemon for this build (subsequent builds will be faster).
Parallel execution is an incubating feature.
Download https://repo.zeroc.com/nexus/content/repositories/releases/com/zeroc/gradle/ice-builder/slice/1.3.13/slice-1.3.13.pom
Download https://repo.zeroc.com/nexus/content/repositories/releases/com/zeroc/gradle/ice-builder/slice/1.3.13/slice-1.3.13.jar
Download https://repo.zeroc.com/nexus/content/repositories/thirdparty/org/apache/tools/bzip2/1.0/bzip2-1.0.pom
Download https://repo.zeroc.com/nexus/content/repositories/thirdparty/org/apache/tools/bzip2/1.0/bzip2-1.0.jar
:help

Welcome to Gradle 2.6.

To run a build, run gradlew <task> ...

To see a list of available tasks, run gradlew tasks

To see a list of command-line options, run gradlew --help

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL

Total time: 31.119 secs
jose@amulet:~/Documents/ZeroC/ice3.6/java$ ./gradlew clean
Parallel execution is an incubating feature.
Download https://repo.zeroc.com/nexus/content/repositories/thirdparty/net/sourceforge/proguard/5.0/proguard-5.0.pom
Download https://repo.zeroc.com/nexus/content/repositories/thirdparty/net/sourceforge/proguard/5.0/proguard-5.0.jar

FAILURE: Build failed with an exception.

* Where:
Build file '/home/jose/Documents/ZeroC/ice3.6/java/src/IceGridGUI/build.gradle' line: 98

* What went wrong:
A problem occurred evaluating project ':IceGridGUI'.
> Could not find property 'proguard' on project ':IceGridGUI'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.556 secs

Hi PeponeOnrez,

You need to define the property value for the clean task too:

$ ./gradlew clean -PicegridguiProguard=false

That is the problem it should work with or without setting it.

If I first build with the property set to false, the next build with property set to true fails because the proguard dependency is not correctly added to the classpath. Even as you can see it the build log proguard-5.0.jar is downloaded

Hi PeponeOnrez,

I don’t like this kind of build script, it should work always without parameters, you should only define the sequence of tasks that the script should run, like: ./gradlew clean build etc …

why don’t you download always the proguard-5.0.jar ?

a workaround for the not defined -P parameter is something like this:

First lines in the build.gradle file:

// define default value
def localIcegridguiProguard = true
if (project.hasProperty('icegridguiProguard')) {
    // override the local variable if the -PicegridguiProguard=false is defined 
    localIcegridguiProguard = icegridguiProguard.toBoolean()
}
println localIcegridguiProguard

Update the references of the script using the ‘localIcegridguiProguard’ variable

why don’t you download always the proguard-5.0.jar ?

When building packages for some Linux distribution (Debian/Ubuntu) you cannot access internet.

I don’t like this kind of build script, it should work always without parameters, you should only define the sequence of
tasks that the script should run, like: ./gradlew clean build etc …

It does work, but gradle dependency resolution seems being cached somehow…

The problem is not that the property is not defined, as I has the default value set in my gradle.properties , the problems is that if I first run with the property set to false, and them with the property set to true proguard is missing from the classpath.

In the otherhand if I first run with the property set to true and the to false proguard is in the classpath, so seems to me the buildscript classpath is not being evaluated each time.

Gradle cache all jars, it checks for download when it does not exist in the local cache or after a while (need to check documentation about the refresh timeout).

If you don’t have access to the internet, you should copy the jar to your local network or in the project and use it from there.

I cannot file the proguard variable in the scripts :confused:

The JAR is downloaded as show in the log but because some gradle internal issue is not added to buildscript classpath, from outside it seems like gradle cache the buildscript classpath

proguard is not a variable but part of the classpath for a task like in https://github.com/zeroc-ice/ice/blob/4942522714a072d139150f98d95bf825e5bc40ab/java/src/IceGridGUI/build.gradle#L98

What I trying to explain but maybe is not clear is that buildscript CLASSPATH is not correctly setup when using properties to dynamically add dependencies.

Seems this classpath is evaluated the first time the script runs and saved for subsequent executions of the same build script.

Hope this make my issue clear.

BTW I have already found a workaround spliting the script in two and use apply from like in https://github.com/zeroc-ice/ice/blob/3.6/java/src/IceGridGUI/build.gradle#L80-L85