0

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?

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67

1 Answers1

1

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"
    }
}
M.Ricciuti
  • 11,070
  • 2
  • 34
  • 54
  • The spelling error happenned just in this question, my mistake sorry. The spelling mistake doesn't exist in the project. Second thing, if I do buildAndUploadTest task depends on publish task, gradle runs publish task before buildAndUploadTest task which is not I want. I want publish task to be executed after buildAndUploadTest task – Umer Farooq Jan 11 '19 at 12:43
  • then try to use the other dependency type: "buildAndUploadTest.finalizedBy publish " – M.Ricciuti Jan 11 '19 at 12:45
  • when I do project.getTasks().getByName("buildAndUploadTest").doLast {task project.getTasks().getByName("publish")}, I get the following error: `* What went wrong: Execution failed for task ':app:buildAndUploadTest'. > Could not find method task() for arguments [task ':app:publish'] on task ':app:buildAndUploadTest' of type com.picavi.gradlePlugins.plugins.BuildAndUpload. ` – Umer Farooq Jan 11 '19 at 12:47
  • I already tried project.getTasks().getByName("buildAndUploadTest").finalizedBy("publish") however it never runs the publish task either. Publish task does run when I use buildAndUploadTest.dependsOn("publish") – Umer Farooq Jan 11 '19 at 12:49
  • I updated my answer. since you use multi-project build, you should access tasks by path instead of name, like : `tasks.getByPath(':app:publish')` .. – M.Ricciuti Jan 11 '19 at 12:53
  • It still doesn't do anything. The publish task is not running – Umer Farooq Jan 11 '19 at 12:57
  • DO you have some logs? when trying to execute `buildAndUploadTest` do you see `publish` in the executed tasks list (maybe it's in "UP TO DATE" status? try to create dependency from `buildAndUploadTest` to another simple task ( `projects` for example) and see if it's triggered or not. there is no reason this doesn't work. – M.Ricciuti Jan 11 '19 at 13:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186549/discussion-between-m-ricciuti-and-umer-farooq). – M.Ricciuti Jan 11 '19 at 14:08
  • 1
    Definitely the second point. Friends don't let friends call execute – tim_yates Jan 11 '19 at 15:22
  • @M.Ricciuti I narrowed down the issue. The problem seems to be with the maven-publish task. when I execute project.getTasks.getByName(publish).execute, it doesn't run cuz apparently it's dependent on some sort of gradle graph. Another thing I tried was I wrote the maven publish groovy script ins a method in my buildAndUpload task and then just called that method. It threw following error:`Caused by: org.gradle.api.InvalidUserDataException: Cannot configure the 'publishing' extension after it has been accessed.` This is seriously a bad api – Umer Farooq Jan 12 '19 at 00:34
  • The entire objective is: executing the maven-publish task after my buildAndUpload task has gotten the arguments from the command line. I've no idea how to do that, that's why I'm doing all these experiments – Umer Farooq Jan 12 '19 at 00:49
  • Hi. I finally reproduced your problem.. and it's certainly related to : https://stackoverflow.com/questions/28020520/custom-gradle-plugin-causes-cannot-configure-the-publishing-extension – M.Ricciuti Jan 14 '19 at 13:25