0

我有一个依赖于 maven-publish 插件的自定义任务。在运行 maven-publish 插件之前,我的自定义任务需要命令行中的一些参数。

为此,我在我的自定义任务上尝试了 doLast 闭包,但是发布任务没有运行。

 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
            }
        }
    }

我究竟做错了什么?

4

1 回答 1

1

您不应从另一个任务操作(使用task.execute())触发任务的执行:即使在 Gradle API 中可用,也不应使用它。改为使用任务依赖项(请参阅此处的任务依赖项文档 )

编辑:从您下面的评论中,如果您只想publish在自定义任务之后强制执行任务,那么:

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:根据您的最后评论:您似乎遇到了类似于自定义 gradle 插件原因的已知问题:无法配置“发布”扩展

一种解决方法是替换:

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

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

和:

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

    }
}
publishing.repositories {
    maven {
        url "../maven-repo"
    }
}
于 2019-01-11T12:32:06.640 回答