如何配置Maven以运行具有两个不同质量配置文件的SonarQube项目分析



我们通过Maven为Java项目运行SonarQube分析。Maven以某种方式自动做到了这一点;我们所做的只是将sonar-maven-plugin添加到我们的pom.xml:中

<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>

这很好用。

但现在我们需要运行SonarQube分析两次,使用不同的质量配置文件。由于您无法轻松更改Maven中的项目密钥,我们使用SonarQube的branch属性来区分SonarQobe项目,如下所示(同样来自pom.xml):

<properties>
<sonar.profile>MyQualityProfile1</sonar.profile>
<sonar.branch>Dev_${sonar.profile}</sonar.branch>
...
</properties>

这样,我们在SonarQube UI中得到了两个项目条目,这两个条目都包含完全相同的代码,但根据它们的质量配置文件(一个使用质量配置文件1,另一个使用了质量配置文件2),会有不同的问题。

问题:为了实现这一点,我必须手动更改pom.xml属性,并运行整个构建两次。

问题:如何将maven配置为使用不同的属性简单地运行两次sonar:sonar目标?

这将为我们的构建节省大量时间。我已经找到了类似的问题,但到目前为止还没有答案。谢谢

扩展Eldad AK之前给出的关于配置文件的答案:

创建两个maven配置文件,如下所示:

<properties>
<sonar.branch>Dev_${sonar.profile}</sonar.branch>
</properties>
<profiles>
<profile>
<id>QualityProfileOne</id>
<properties>
<sonar.profile>MyQualityProfile1</sonar.profile>
</properties>
</profile>
<profile>
<id>QualityProfileTwo</id>
<properties>
<sonar.profile>MyQualityProfile2</sonar.profile>
</properties>
</profile>
</profiles>

Then run the following:
$ mvn clean install -DskipTests
$ mvn sonar:sonar -PQualityProfileOne
$ mvn sonar:sonar -PQualityProfileTwo
(you may need to perform a clean between running sonar, not sure)

尝试配置插件的两个执行。类似这样的东西:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<executions>
<execution>
<id>s1</id>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
<configuration>
<sonar.branch>MyQualityProfile1</sonar.branch>
</configuration>
</execution>
<execution>
<id>s2</id>
<phase>install</phase>
<goals>
<goal>sonar</goal>
</goals>
<configuration>
<sonar.branch>MyQualityProfile2</sonar.branch>
</configuration>
</execution>
</executions>
</plugin>

这将分阶段开始执行两次声纳验证和安装,每次都有另一个声纳分支值。在Sonar中,您可以在第一次分析后配置所需的质量配置文件。

maven和Ant的组合可能会起作用:像您已经做的那样,使用maven进行第一次声纳分析,并使用maven Antrun插件执行使用SonarQube Ant任务定义的另一个SonarQobe配置。

我会选择maven配置文件
每个配置文件都有自己的属性。

我希望这能有所帮助。

最新更新