编写可从命令行自定义的 Gradle 任务的最佳方法?
我想为我的属性提供默认值,但允许以相对简单的方式从命令行覆盖它们。 我正在使用 Gradle 1.0 Milestone 3。
这就是我所拥有的,它的工作方式几乎按照我想要的方式工作,但它非常冗长。它看起来不太像 Gradle 风格——与我正在做的实际工作相反,其中 150 多行 Java 代码已压缩为大约 2 行 gradle 脚本。
task sbtCopyTask {
fromProp = "defaultCopyFrom"
toProp = "defaultCopyTo"
overrideFromProp = System.getProperty('fromSysProp')
if( overrideFromProp != null && !overrideFromProp.trim().empty ){
fromProp = overrideFromProp
}
doLast {
println "copying ${fromProp} to ${toProp}"
}
}
执行
$GRADLE_HOME/bin/gradle sbtCopyTask
结果为:
copying defaultCopyFrom to defaultCopyTo
执行
$GRADLE_HOME/bin/gradle sbtCopyTask -DfromSysProp="wibble"
结果为:
copying wibble to defaultCopyTo
- 我知道 Gradle 中构建了一个复制任务 - 我问的问题是“如何编写可从命令行自定义的构建脚本”。
- 我尝试了“-P”命令行选项 - 似乎没有达到我想要的效果。
- 我希望能够从命令行执行此操作,属性文件或环境变量会很不方便。
编辑: 哦,发布后 10 秒 - 这似乎很明显。
所以,这工作正常,对我来说完全足够了(除非有更好的方法?)
fromProp = System.getProperty('fromProp', 'defaultFromProp')
I want to provide defaults for my properties, but allow them to be overridden from the command line in a relatively easy way.
I'm using Gradle 1.0 Milestone 3.
This is what I've got, it works pretty much the way I want but it's quite verbose. It just doesn't seem very Gradle-ish - as opposed to the actual work I'm doing, where 150-odd lines of Java code have compressed down to about 2 lines of gradle script.
task sbtCopyTask {
fromProp = "defaultCopyFrom"
toProp = "defaultCopyTo"
overrideFromProp = System.getProperty('fromSysProp')
if( overrideFromProp != null && !overrideFromProp.trim().empty ){
fromProp = overrideFromProp
}
doLast {
println "copying ${fromProp} to ${toProp}"
}
}
executing
$GRADLE_HOME/bin/gradle sbtCopyTask
results in:
copying defaultCopyFrom to defaultCopyTo
and executing
$GRADLE_HOME/bin/gradle sbtCopyTask -DfromSysProp="wibble"
results in:
copying wibble to defaultCopyTo
- I know there's a copy task build into Gradle - the question I'm asking is "how do I write a build script that is customisable from the command line".
- I tried the "-P" command line option - doesn't seem to do what I want.
- I want to be able to do this from the command line, properties files or environment variables would be inconvenient.
Edit:
Doh, 10 seconds after posting - this seems kind of obvious.
So, this works fine and is completely good enough for me (unless there's a better way?)
fromProp = System.getProperty('fromProp', 'defaultFromProp')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论