Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass properties to custom gradle task

Tags:

gradle

groovy

How can I pass properties to gradle custom task? In ant It will look like this:

public class MyCustomTask extends Task {

    private String name;
    private String version;

    @Override
    public void execute() throws BuildException {
        // do the job
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setVersion(String version) {
        this.version = version;
    }

}

How to do it in gradle?

class MyCustomTask extends DefaultTask {

    private String name
    private String version

    @TaskAction
    def build() {
        // do the job
    }

}

Properties name and version are required and user needs to pass them to task.

like image 986
pepuch Avatar asked Jun 05 '13 08:06

pepuch


People also ask

How do I pass system properties to Gradle build?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle.

How do I pass JVM options to Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.


1 Answers

I found the solution:

class MyCustomTask extends DefaultTask {

    @Input
    String name
    @Input
    String version

    @TaskAction
    def build() {
        // do the job
        println name
        println version
    }

}

Example use:

task exampleTask(type: MyCustomTask) {
    name = "MyName"
    version = "1.0"
}
like image 135
pepuch Avatar answered Oct 18 '22 03:10

pepuch