用自定义参数为gradle任务定义别名



我想运行

./gradlew extractMyDeps

不是

./gradlew :app:dependencies --configuration productionDebugRuntimeClasspath

但是我不能通过dependOn传递参数给这个任务

如何实现我的目标?

task("extractMyDeps") {
dependsOn(":app:dependencies") // how pass --configuration
}

我认为你不能将命令行参数传递给另一个任务。但是如果你想为一个任务创建别名,你可以使用Exec Tasks,像这样:

task('extractMyDeps', type: Exec) {
workingDir '..'
def arguments = ['cmd', '/c', 'gradlew', ':app:dependencies', '--configuration', 'testCompileClasspath']
commandLine arguments
}

通过使用此任务,您可以运行:

> gradlew extractMyDeps

还可以通过传递配置名使其更具动态性,如:

task('extractMyDeps', type: Exec) {
workingDir '..'
def arguments = ['cmd', '/c', 'gradlew', ':app:dependencies']
if (project.hasProperty("conf")) {
def conf = project.property("conf")
arguments.add('--configuration')
arguments.add(conf.toString())
}
commandLine arguments
}

这样这些命令是有效的:

> gradlew extractMyDeps

> gradlew extractMyDeps -Pconf=testCompileClasspath

我希望它能帮助你。

最新更新