不同的任务不能有不同的系统属性值
Cannot have different system properties values for different tasks
我正在尝试创建 2 个任务来执行 sonarcube 任务。我希望能够根据任务指定不同的属性
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
好像那些属性是在 运行 时设置的,无法更新。我觉得我遗漏了一些明显的东西,任何收到的建议。
首先:NEVER use execute()
on tasks。该方法不是 public Gradle API 的一部分,因此,它的行为可以更改或未定义。 Gradle 将自行执行任务,因为您指定了它们(命令行或 settings.gradle
)或作为任务依赖项。
您的代码不起作用的原因是 difference between the configuration phase and the execution phase。在配置阶段,执行任务闭包中的所有(配置)代码,但不执行任务。因此,您将始终覆盖系统属性。只有(内部)任务操作,doFirst
和 doLast
闭包在 执行阶段 中执行。请注意,每个任务在一次构建中只执行一次 ONCE,因此您将任务参数化两次的方法将永远行不通。
此外,我不明白您为什么要使用系统属性来配置您的 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
}
我正在尝试创建 2 个任务来执行 sonarcube 任务。我希望能够根据任务指定不同的属性
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
好像那些属性是在 运行 时设置的,无法更新。我觉得我遗漏了一些明显的东西,任何收到的建议。
首先:NEVER use execute()
on tasks。该方法不是 public Gradle API 的一部分,因此,它的行为可以更改或未定义。 Gradle 将自行执行任务,因为您指定了它们(命令行或 settings.gradle
)或作为任务依赖项。
您的代码不起作用的原因是 difference between the configuration phase and the execution phase。在配置阶段,执行任务闭包中的所有(配置)代码,但不执行任务。因此,您将始终覆盖系统属性。只有(内部)任务操作,doFirst
和 doLast
闭包在 执行阶段 中执行。请注意,每个任务在一次构建中只执行一次 ONCE,因此您将任务参数化两次的方法将永远行不通。
此外,我不明白您为什么要使用系统属性来配置您的 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
}