Passing properties from build.gradle to java custom plugin JVM

I wrote a Gradle custom plugin and would like to pass some properties from build.gradle into plugin in JVM. Both plugin and consumer script compiles without passing any property. However, with passing properties, I am getting a build error on consumer project.

My build.gradle from consumer project:

buildscript {
repositories {
maven {
url uri(‘…/…/…/…/repo’)
}
mavenLocal()
}

dependencies {
    //Custom plugin class path. Group=com.testdependencies, artifactId=ttsclient, version=0.0.1-BUILD-SNAPSHOT 
    classpath 'com.testdependencies:ttsclient:0.0.1-BUILD-SNAPSHOT'
}

}
apply plugin: ‘org.tts’

task uploadJacocoDump(type: org.ttsgradle.UploadGradleTask) {
println(“running consumer task!”)

//These are the properties I want to pass into JVM
systemProperty 'release', 'F12.2.1'
systemProperty 'groupId', 'manifest.publish.label.WLS_GENERIC'

}


org.tts.properties contents:
implementation-class=org.tts.client.ttsgradle.UploadGradlePlugin

Custom plugin:

package org.tts.client.ttsgradle;

import org.gradle.api.Project;
import java.util.List;
import org.gradle.api.Plugin;

public class UploadGradlePlugin implements Plugin < Project > {

@Override
public void apply(Project target) {
    target.task("uploadTask");
}

}


Custom Task:
package org.tts.client.ttsgradle;

import org.gradle.api.tasks.TaskAction;
import java.util.Properties;
import org.gradle.api.DefaultTask;

public class UploadGradleTask extends DefaultTask {

@TaskAction
    public void uploadTask() {
	
	Properties props = System.getProperties();
	props.list(System.out);
	
	String propRelease = System.getProperty("release");
    
	System.out.println("Release: " + propRelease);
}

}


Note that plugin classes are located in a different project. As a result of this, plugin project compiles and uploadArchives also deploys a jar file. However, consumer project failed on build with following message:

  • What went wrong:
    A problem occurred evaluating root project ‘java’.

Could not find method systemProperty() for arguments [release, F12.2.1] on r
oot project ‘java’.

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

BUILD FAILED


Any suggestions for how to pass properties from build.gradle into plugin java code?

You can pass System properties via gradle.properties file.

  • The prefix of property is systemProp
  • The name of property follows it.

For example:

gradle.properties file

systemProp.foo=bar
systemProp.baz=hoge

You can obtain these System properties with the code…

String foo = System.getProperty("foo"); // -> bar

More detail is available from the document The Build Environment.

Thank you Shinya. It worked :smile: