简体   繁体   中英

doLast gradle task not running

I've a custom task that depends on the maven-publish plugin. My custom task needs some arguments from the the command line before the maven-publish plugin should run.

For that I tried the doLast closure on my custom task however the publish task is not running.

 class MyGradlePlguins implements Plugin<Project> {
        @Override
        void apply(Project project) {
            project.getPluginManager().apply("maven-publish")
            BuildAndUpload buildAndUpload = project.getTasks().create("BuildAndUpload", BuildAndUpload.class);
            project.getTasks().getByName("buildAndUploadTest").doLast {
                println "running publish task from the doLast clause"
                project.getTasks().getByName("publish").execute() <-- doens't throw error but doesn't run either
            }
        }
    }

what am I doing wrong?

You should not trigger execution of a task from another task action (using task.execute() ) : even if this is available in the Gradle API this should not be used. Use tasks dependencies instead ( see documentation on Tasks dependencies here )

EDIT : from your comment below, if you just want to force publish task to be executed after your custom task, then :

class MyGradlePlguins implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.getPluginManager().apply("maven-publish")
        BuildAndUpload buildAndUpload = project.getTasks().create("BuildAndUpload", BuildAndUpload.class);

        // Make your task 'finalized by' task "publish"
        buildAndUpload.finalizedBy project.tasks.getByName('publish')  

        // You can also have the other way:
        // project.tasks.getByPath(':app:publish').dependsOn buildAndUpload           
    }
}

EDIT2 : From your last comments: it seems you have run into a known issue similar to custom gradle plugin causes: Cannot configure the 'publishing' extension

A workaround is to replace:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

        }
    }
    repositories {
        maven {
            url "../maven-repo"
        }
    }
}

With:

publishing.publications {
    mavenJava(MavenPublication) {
        from components.java

    }
}
publishing.repositories {
    maven {
        url "../maven-repo"
    }
}

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