每个项目模块的Gradle可重用自定义任务
如何定义可用于模块的自定义任务?在我的例子中,我想使用一个每个项目模块的Gradle可重用自定义任务,gradle,build.gradle,Gradle,Build.gradle,如何定义可用于模块的自定义任务?在我的例子中,我想使用一个Exec任务来运行一个脚本,该脚本带有特定于子项目的commandLine参数 例如: 脚本 #!/usr/bin/env sh echo "$@" #!/usr/bin/env sh echo "$@" build.gradle task customExecTask(type: Exec) { if (project.name == "a") { commandLine './script', "Project 'a'
Exec
任务来运行一个脚本,该脚本带有特定于子项目的commandLine
参数
例如:
脚本
#!/usr/bin/env sh
echo "$@"
#!/usr/bin/env sh
echo "$@"
build.gradle
task customExecTask(type: Exec) {
if (project.name == "a") {
commandLine './script', "Project 'a'"
} else if (project.name == "b") {
commandLine './script', "Project 'b'"
}
}
project('a') {
build.dependsOn ':customExecTask'
}
project('b') {
build.dependsOn ':customExecTask'
}
subprojects { // Define the task here
task customExecTask(type: Exec) {
// Change the directory where the script resides
workingDir = rootDir
// Conditional arguments depending on each project modules.
// We'll use the property 'project.name' to determine the
// current project this task is running from
def customArgs = project.name == 'a' ? "Hello" : "World"
// Then execute the script with customArgs variable
commandLine './script', customArgs
// Or in Windows: commandLine 'cmd', '/c', 'script.bat'
}
}
project('a') {
build.dependsOn 'customExecTask'
}
project('b') {
build.dependsOn 'customExecTask'
}
编辑
或者像这样:
task customExecTask(type: Exec) {
def dynamicVariable = ""
commandLine './script', dynamicVariable
}
project('a') {
task(':customExecTask').dynamicVariable = "Project 'a'"
build.dependsOn ':customExecTask'
}
project('b') {
task(':customExecTask').dynamicVariable = "Project 'b'"
build.dependsOn ':customExecTask'
}
预期结果:
$ gradle :a:build
Project 'a'
$ gradle :b:build
Project 'b'
请参阅上的文档 因此,您可以将任务放在单独的gradle文件
mytask.gradle
中,并在添加到build.gradle的每个模块中:
应用于:“${rootDir}/pathtomycustomgradlefile/mytask.gradle”
如果您需要更多的逻辑来决定应用哪一个,您可以查看主题 我找到了解决办法。对于我的第一个示例,我只想执行根项目目录中的脚本,每个子项目都有特定的参数。为了使任务正常工作,您必须在
子项目中定义任务。以下是单个build.gradle
设置的完整工作示例
目录结构:
├─ a # Module 'a' folder
├─ b # Module 'b' folder
├─ build.gradle # Root project build file
├─ script # Executable bash script
└─ **/ # Other files and folder
脚本
#!/usr/bin/env sh
echo "$@"
#!/usr/bin/env sh
echo "$@"
build.gradle
task customExecTask(type: Exec) {
if (project.name == "a") {
commandLine './script', "Project 'a'"
} else if (project.name == "b") {
commandLine './script', "Project 'b'"
}
}
project('a') {
build.dependsOn ':customExecTask'
}
project('b') {
build.dependsOn ':customExecTask'
}
subprojects { // Define the task here
task customExecTask(type: Exec) {
// Change the directory where the script resides
workingDir = rootDir
// Conditional arguments depending on each project modules.
// We'll use the property 'project.name' to determine the
// current project this task is running from
def customArgs = project.name == 'a' ? "Hello" : "World"
// Then execute the script with customArgs variable
commandLine './script', customArgs
// Or in Windows: commandLine 'cmd', '/c', 'script.bat'
}
}
project('a') {
build.dependsOn 'customExecTask'
}
project('b') {
build.dependsOn 'customExecTask'
}
控制台结果:
$ gradle build
> Task :a:execute
Hello
> Task :b:execute
World
这只是一个简单的Exec任务,它将根据子项目执行带有可选参数的脚本。请再次查看我编辑的问题,以了解我想要实现的目标的其他逻辑。感谢您分享此答案。我现在有了一个解决方案,没有创建单独的.gradle
文件。