简体   繁体   中英

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

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 )

If you gradle plugin isapplied to a different gradle project, say projectA, your plugin adds a task called MyTask to projectA. The build.gradle you posted above is for the plugin-project and does not have a task called MyTask.

As far as I can see your task is called myTask , not MyTask . It was a typo in author's code.

UPDATE :

You don't apply your plugin so the task doesn't get created. And you cannot apply a plugin from a plugin's build.gradle . At least you cannot apply a version of the plugin being build. The best you can do is to apply a previously built version of the plugin that's stored in a Maven repo. But it doesn't look like a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM