Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle custom plugin depends on system task

I hava a custom plugin called MyPlugin and it contains a custom task called MyTask. I want to make MyTask dependsOn compileJava. I tried to give MyTask.dependsOn(compileJava), but it says Task called 'MyTask' is not existing. Is there any way to do it?

MyPlugin.java

public class MyPlugin implements Plugin<Project> {
    public static final String TASK_NAME = "MyTask";

    @Override
    public void apply(Project project) {
        project.getExtensions().create(TASK_NAME, MyExtension.class);
        project.getTasks().create(TASK_NAME, MyTask.class);
    }
}

MyTask.java

public class MyTask extends DefaultTask {
    private MyExtension extension;

    @TaskAction
    public void myTask() {
        Project project = getProject();
        extension = project.getExtensions().findByType(MyExtension.class);
        PropertyManager propertyManager = new PropertyManager(project, extension);
        propertyManager.setProperties();
        System.out.println(extension.getValue());
    }

MyExtension.java

public class MyExtension {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

build.gradle

apply plugin: 'maven'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'com.jfrog.bintray'

sourceCompatibility = 1.8

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile gradleApi()
}

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
    }
}

task copyLibs(type: Copy) {
    from configurations.runtime
    into "$projectDir/libs"
}

MyTask.dependsOn(copyLibs)  //not working
MyTask.dependsOn(compileJava)   //not working
like image 324
Msp Avatar asked Dec 06 '25 15:12

Msp


1 Answers

You have to use

tasks.MyTask.dependsOn(copyLibs) 
tasks.MyTask.dependsOn(compileJava)

because the task object you defined is not in the current scope like copyLibs. But you can access it via the tasks reference (see: https://docs.gradle.org/current/dsl/org.gradle.api.Project.html)

like image 79
and_dev Avatar answered Dec 09 '25 18:12

and_dev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!