Pass properties to buildSrc build part

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

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

What kind of configuration do you need to do?

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.

What’s the exact output that you see?

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

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:

I discovered why it worked for me and not for you:

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

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?