我正在尝试创建 2 个任务来执行声纳立方体任务。我希望能够根据任务指定不同的属性
task sonarqubePullRequest(type: Test){
System.setProperty( "sonar.projectName", "sonarqubePullRequest")
System.setProperty("sonar.projectKey", "sonarqubePullRequest")
System.setProperty("sonar.projectVersion", serviceVersion)
System.setProperty("sonar.jacoco.reportPath",
"${project.buildDir}/jacoco/test.exec")
tasks.sonarqube.execute()
}
task sonarqubeFullScan(type: Test){
System.setProperty("sonar.projectName", "sonarqubeFullScan")
System.setProperty("sonar.projectKey", "sonarqubeFullScan")
System.setProperty("sonar.projectVersion", serviceVersion)
System.setProperty("sonar.jacoco.reportPath",
"${project.buildDir}/jacoco/test.exec")
tasks.sonarqube.execute()
}
任务有效,但我设置的属性似乎存在问题
如果我运行第一个任务 sonarqubePullRequest 那么一切都很好,但是如果运行 sonarqubeFullScan 那么如果使用 sonarqubePullRequest 中指定的值。所以项目名称设置为 sonarqubePullRequest
就好像这些属性是在运行时设置的,无法更新。我觉得我错过了一些明显的东西,任何收到的建议都很明显。
首先:永远不要execute()
在任务上使用。该方法不是公共 Gradle API 的一部分,因此,其行为可以更改或未定义。Gradle 将自行执行任务,因为您指定了它们(命令行或settings.gradle
)或作为任务依赖项。
您的代码不起作用的原因是配置阶段和执行阶段之间的差异。在配置阶段,任务闭包中的所有(配置)代码都被执行,但不执行任务。因此,您将始终覆盖系统属性。在执行阶段只执行(内部)任务操作doFirst
和doLast
闭包。请注意,每个任务在构建中仅执行一次,因此您将任务参数化两次的方法将永远行不通。
另外,我不明白您为什么使用系统属性来配置您的sonarqube
任务。您可以通过以下方式直接配置任务:
sonarqube {
properties {
property 'sonar.projectName', 'sonarqubePullRequest'
// ...
}
}
现在您可以配置sonarqube
任务。为了区分您的两种情况,您可以为不同的属性值添加条件。下一个示例使用项目属性作为条件:
sonarqube {
properties {
// Same value for both cases
property 'sonar.projectVersion', serviceVersion
// Value based on condition
if (project.findProperty('fullScan') {
property 'sonar.projectName', 'sonarqubeFullScan'
} else {
property 'sonar.projectName', 'sonarqubePullRequest'
}
}
}
或者,您可以添加另一个类型的任务SonarQubeTask
。这样,您可以对这两个任务进行不同的参数化,并在需要时调用它们(通过命令行或依赖项):
sonarqube {
// Generated by the plugin, parametrize like described above
}
task sonarqubeFull(type: org.sonarqube.gradle.SonarQubeTask) {
// Generated by your build script, parametrize in the same way
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句