0

I've spent the day yesterday up and down StackOverflow and Google and have come close to some solutions, but nothing is working. I hope someone could just tell me whether this is even possible.

We have a multi-module Maven project. I just learned that the structure is like this:

Web Service XYX
   +- pom.xml
   +- Web Module
      +- pom.xml (parent pom is WebService 123)
   +- API Module
      +- pom.xml (parent pom is API ABC)
   +- API Implementation
      +- pom.xml (parent pom is API ABC)

What I have done is setup UT and IT Code Coverage from Jacoco to Sonar via Jenkins. The UT Coverage gets sent to Sonar correctly for the UT combined. The IT Coverage is always 0%. My IT is run against the Web Module after starting the WAR up using the Maven Tomcat plugin. The code that it should be exercising lives inside the module API Implementation. Even though a jacoco-it.exec file was generated and analyzed by Sonar, it always shows me that no lines of code were hit. Is there a way to actually do this?

BTW - I know it can be done because this article indicates exactly what I need: http://www.sonarqube.org/measure-code-coverage-by-integration-tests-with-sonar/

However, the article fails to mention if the modules are referred to the same parent in the project. I'm making an assumption that it does.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107

2 Answers2

0

After searching up and down the interwebs, the best solution seem to have been add another module for ITs that actually referred to the main pom as the parent. Used Cargo to grab and run the WAR from the IT module using the Tomcat Maven Module. The Code Coverage now has all the classes, not just the ones from the Web Module.

0

I doubt it's due to using different parent poms. When analyzing the coverage sonar checks the code of each module against the coverage file that is specified in the sonar.jacoco.itReportPath property. The default is target/jacoco-it.exec. So when analyzing WebModule it checks for coverage info in WebModule/target/jacoco-it.exec.

So what I do for my projects, is to use a central file in the root module for the the IT coverage data instead.

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.4.201502262128</version>
  <executions>
    <execution>
      <id>prepare-it-agent</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${session.executionRootDirectory}/target/jacoco-it.exec</destFile>
        <append>true</append>
        <propertyName>failsafeArgLine</propertyName>
      </configuration>
    </execution>
  </executions>
</plugin>

The ${session.executionRootDirectory} property is the root of execution, in your case the WebServiceXYZ if you run the build of that. This also works if you have multi-module with more than one level of nesting.

Now you need to point sonar to use that file when analyzing IT coverage. So you have to set the sonar.jacoco.itReportPath to that file. Unfortunately, this does not work with the session.executionRootDirectory property and you have to set the absolute path to the file manually. I do not recommend to specify the absolute path in the pom.xml as this path is specific to your build environment. So either set the path in Sonar or as System property of your build environment. I set it directly in the Sonar Project Settings (Java > Jacoco), for example /opt/buildroot/myProject/target/jacoco-it.exec. Now sonar will check that file for the IT coverage analysis of each module.

Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67