Pass properties to buildSrc build part


(david koch) #1

Is it possible to pass properties from the command line to the buildSrc build?

When passing properties with -D or -P option they are not available in the buildSrc part of the build.

Is there some hook that is called before the buildSrc part or is it possible to configure the buildSrc otherwise? Thanks, David


(Luke Daley) #2

System properties (i.e. ‘-D’) should work, but ‘-P’ project properties will not.

What kind of configuration do you need to do?


(david koch) #3

I try to pass some configs to my build via system property (repository location, …). Unfortunately system properties get lost for the buildSrc build part.

Here the example:

The build.gradle in the project root:

def message = System.getProperty('message') ?: 'NO_MESSAGE'
println "Greetings from root: ${message}"
  defaultTasks 'build'
task build{
}

The build.gradle in project root/buildSrc:

def message = System.getProperty('message') ?: 'NO_MESSAGE'
println "Greetings from buildSrc: ${message}"
}

The command line:

gradle -Dmessage=hi -q

I would have expected the message twice, but get it only for the root build.gradle.


(Luke Daley) #4

What’s the exact output that you see?


(david koch) #5

C:\workspace\gradleBuildSrcProperties>gradle -Dmessage=hi -q Greetings from buildSrc: NO_MESSAGE Greetings from root: hi


(Luke Daley) #6

When I try and reproduce, I do get the system property in my buildSrc build. Can you please email me the example project that you are using: luke.daley@gradleware.com.


(Luke Daley) #7

I discovered why it worked for me and not for you: http://issues.gradle.org/browse/GRADLE-2475

The only workaround that is reliable is to set the system property via the ‘GRADLE_OPTS’ env var instead of the Gradle invocation…

GRADLE_OPTS="$GRADLE_OPTS -Dmessage=hello" ./gradlew help


(michael.ottati) #8

Is there any supported mechanism for sharing a property between the root and buildSrc parts of a build?

My use case is that I have a code generator task that is packaged as a plugin in the buildSrc, the output of the root build is a package that will consume code generated by the code generator plugin. It would be nice to maintain and publish them together under the same version and different artifact IDs

e.g. the output published into a Maven repo would be something like:

‘org.groupid:artifactPlugin:1.2.3’ ‘org.groupid:artifactPackage:1.2.3’

For the two artifacts above, I would like to define the version ‘1.2.3’ in exactly one place and have the artifacts from the buidSrc and root be at the same version.

Is there a simple way to do this without the use of System properties?