Merging sonar metrics across a maven multi-module project

送分小仙女□ 提交于 2020-01-25 12:40:09

问题


I've inherited an odd maven multi module project. The standard business classes and unit tests are in a module called 'code' and we've nice simple working sonar metrics for this module. The second module holds various integration style tests which run against a deployed glassfish server, these run as unit test in the integ-test module.

pom.xml // root level,
    code // business code and unit tests :-)
        moduleA
        moduleB
    integ-test // ;-(
        moduleA
        moduleB

I know the simplest option is to move the 'integ-test' classes into the 'code' module and refactor them as real integration tests but for the moment this isn't an option.

I've figured out a way to record the level of code coverage for the 'integ-tests' module by using the jacoco:dump goal to download the jacococ.exec to the integ-test modules.

My maven pom structure now looks like this

 pom.xml // defined jacoco verify -> report goal
     code // business code and unit test
         moduleA
            target/jacoco.exec - unit test coverage
            pom.xml
     pom.xml - defines standard jacoco prepare-agent goal
     integ-test //
         moduleA
            target/jacoco.exec - really integration test coverage
            pom.xml
     pom.xml- defines jacoco dump and merge

My current approach is to merge all the jacococ.exec files in the 'integ-test' module and use the 'destFile' value to save these details the 'code' module as a jacoco-it.exec file. The standard sonar:sonar goal should then pick up this file and use it as the integration test coverage.

 pom.xml
     integ-test
         moduleA
            target/jacoco.exec - really integration test coverage modA
         moduleB
            target/jacoco.exec - really integration test coverage modB
     code
         moduleA
         moduleB
     target/jacoco-it.exec - This should be the merged content of the two files above
     pom.xml

My root level pom.xml defines this jacoco-maven-plugin config

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
        <append>true</append>
        <includes>
            <include>com/a/b/c/**</include>
        </includes>
    </configuration>
    <executions>

        <execution>
            <id>jacoco-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>

        <execution>
            <id>jacoco-dump</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>dump</goal>
            </goals>
        </execution>

        <execution>
            <id>jacoco-merge</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>merge</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileSet implementation="org.apache.maven.shared.model.fileset.FileSet">
                        <directory>${project.basedir}/integ-test</directory>
                        <includes>
                            <include>*.exec</include>
                        </includes>
                    </fileSet>
                </fileSets>
                <destFile>${sonar.jacoco.itReportPath}</destFile>
            </configuration>
        </execution>

        <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>

I've linked the jacoco:merge goal to the maven post-integration-test phase to ensure the merge happens after the tests are run, but the 'jacoco-it.exec' file is always created within the 'integ-test' folder.

Any suggestions?


回答1:


What about pointing to destFile in target directory of the master pom? Just define

<configuration>
    <append>true</append>
    <destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
    <propertyName>jacoco.integrationtest.arguments</propertyName>
</configuration>

in the sub-projects. All coverage data will be appended in the jacoco-it.exec file. This file is picked up by sonarqube and you will see the coverage as IT coverage. You can use jacoco.exec as file name as well.



来源:https://stackoverflow.com/questions/31324380/merging-sonar-metrics-across-a-maven-multi-module-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!