Gradle 从子项目调用任务时,如何从根梯度任务引用子项目目录
我有一个根项目gradle任务定义如下。我是Gradle 从子项目调用任务时,如何从根梯度任务引用子项目目录,gradle,Gradle,我有一个根项目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
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)
}
}
在一些子项目中,我希望运行此任务并在子项目的src/main/resources/VERSION.txt
中创建VERSION.txt
文件。我的问题是根级别任务的$projectDir
是根项目
在调用子项目目录时,是否可以定义使用子项目目录的根级别任务?或者可能有更好的方法。我最终只是在每个子项目中定义了任务,并在适当的子项目中设置了对它的依赖关系:
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 createversionxtresourcefile
当您注册一个操作以等待java
插件应用于子项目时,您可以稍微控制它。这样,您只能在包含所需compileJava
任务的子项目中创建任务,并配置根项目中的所有内容
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
}
}
说得好。我做了类似的事情来添加到“测试”任务中,但只针对具有类型测试的项目。我可能也可以为android特定模块做类似的事情。谢谢