我有一个依赖于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
}
}
}
我在做什么错了?
答案 0 :(得分:1)
您不应触发其他任务动作(使用task.execute()
来触发任务的执行:即使Gradle API中提供了此功能,也不应使用此功能。改用任务依赖项(请参阅任务依赖项here的文档)
编辑:从下面的评论中,如果您只想在自定义任务之后强制执行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 :根据您的最新评论:您似乎遇到了与custom gradle plugin causes: Cannot configure the 'publishing' extension
类似的已知问题一种解决方法是替换:
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"
}
}