我正在尝试配置以下自定义任务:
task antecedeRelease(type: AntecedeReleaseTask) {
antecedeWithVersion = project.'antecede-with-version'
antecedeToVersion = project.'antecede-to-version'
}
问题是使用-P选项通过命令行设置antecede-with-version和antecede-to-version属性.如果它们未设置且未调用antecedeRelease,则不应导致错误:
$./gradlew tasks
org.gradle.api.GradleScriptException: A problem occurred evaluating project ...
Caused by: groovy.lang.MissingPropertyException: Could not find property 'antecede-with-version' on project ...
我可以有条件地定义antecedeRelease任务,这样只有在定义了这些属性时才定义它,但我想保持build.gradle文件尽可能干净.
最佳答案 如果您需要antecedeRelease任务以“懒惰”的方式运行,在配置阶段结束时或在执行阶段开始时,最好的办法是使用doFirst
task antecedeRelease(type: AntecedeReleaseTask) {
doFirst {
antecedeWithVersion = project.'antecede-with-version'
antecedeToVersion = project.'antecede-to-version'
}
}