我在下面定义了一个根项目gradle任务。我是
task createVersionTxtResourceFile {
doLast {
def webAppVersionFile = new File("$projectDir/src/main/resources/VERSION.txt")
def appVersion = project.ext.$full_version
println "writing VERSION.txt to " + webAppVersionFile + ", containing " + appVersion
webAppVersionFile.delete()
webAppVersionFile.write(appVersion)
}
}
在一些子项目中,我想运行此任务并在子项目的VERSION.txt
中创建src/main/resources/VERSION.txt
文件。我的问题是,根级任务的$projectDir
是根项目。
是否可以定义在调用子项目目录时使用子项目目录的根级任务?也许总有一种更好的方法。
答案 0 :(得分:1)
注册一个动作以等待将java
插件应用于子项目时,可以稍微控制一点。这样,您就只能在包含所需compileJava
任务的子项目中创建任务,并配置root
项目中的所有内容。
subprojects { sub ->
//register an action which gets executed when the java plugins gets applied.
//if the project is already configured with the java plugin
//then this action gets executed right away.
sub.plugins.withId("java") {
//create the task and save it.
def createVersionTxtResourceFile = sub.tasks.create("createVersionTxtResourceFile") {
doLast {
def webAppVersionFile = new File("${sub.projectDir}/src/main/resources/VERSION.txt")
def appVersion = rootProject.full_version
println "writing VERSION.txt to " + webAppVersionFile + ", containing " + appVersion
webAppVersionFile.delete()
webAppVersionFile.write(appVersion)
}
}
// set the task dependency
sub.tasks.compileJava.dependsOn createVersionTxtResourceFile
}
}
答案 1 :(得分:0)
我最终只是在每个子项目中定义了任务,并在相应的子项目中设置了对它的依赖:
subprojects {
task createVersionTxtResourceFile {
doLast {
def webAppVersionFile = new File("$projectDir/src/main/resources/VERSION.txt")
def appVersion = rootProject.full_version
println "writing VERSION.txt to " + webAppVersionFile + ", containing " + appVersion
webAppVersionFile.delete()
webAppVersionFile.write(appVersion)
}
}
}
然后在子项目build.gradle
中:
compileJava.dependsOn createVersionTxtResourceFile