fxviking
fxviking

Reputation: 105

Configure jacoco agent for sonar2.12 (Multi Module maven)

The Sonar latest version 2.12 has the Jacoco plugin integrated and i want to use it for my code coverage part on a multi module project.

I have a structure like this

proj.com.parent
   proj.com.provider
   proj,com.test

The Test cases for the provider project are in the test project. When i set the Code coverage plugin in sonar as jacoco it executes fine , but the combined code covergae is not presented on the DashBoard. Ihave seen a post that a single jacoco.exec file can solve the problem , but i am unable to do so.

I have tried to configure the below in my pom as below

<profile>
        <id>sonar</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                          <argLine>-javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.reportPath}</argLine> 
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <properties>
            <sonar.jacoco.reportPath>${basedir}/code-coverage/jacoco.exec</sonar.jacoco.reportPath>
            <sonar.jacoco.jar> C:\sonar-2.12\war\sonar-server\deploy\plugins\jacoco\META-INF\lib\org.jacoco.agent-0.5.3.201107060350.jar</sonar.jacoco.jar>
        </properties>

But on maven commandline " mvn clean install " i get this error :

Failed to find Premain-Class manifest attribute in C:\sonar-2.12\war\sonar-server\deploy\plugins\jacoco\META-INF\lib\org.jacoco.agent-0.5.3.201107060350.jar Error occurred during initialization of VM agent library failed to init: instrument

Can anyone provide any help on this ?

Upvotes: 2

Views: 6506

Answers (3)

G&#225;bor Lipt&#225;k
G&#225;bor Lipt&#225;k

Reputation: 9796

I had the same problem. Take a look at the Jacoco agent artifacts at the central repository.

There is a normal jar artifact, and there is a jar with classifier runtime. You need the "runtime" artifact to be used as agent jar. What I do, I simply download the Jacoco agent runtime jar with maven dependency plugin like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>download-jacoco-agent</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.jacoco</groupId>
                        <artifactId>org.jacoco.agent</artifactId>
                        <version>0.6.3.201306030806</version>
                        <classifier>runtime</classifier>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <destFileName>jacoco-agent.jar</destFileName>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

Then you just need to define the following command line option:

<argLine>-javaagent:${project.build.directory}/jacoco-agent.jar=destfile=${sonar.jacoco.reportPath}</argLine> 

Upvotes: 3

raghav
raghav

Reputation: 101

the jar you are pointing to is not the jar..extract that using winrar and you will get another jar inside it. called jacocoagent.jar .to check whether you got the right jar just extract jacocoagent.jar and look for manifest.mf and it should have an entry for premain class.

that should do.

Upvotes: 10

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 78021

Perhaps you should try setting the property sonar.core.codeCoveragePlugin to the value jacoco. The default code coverage tool in Sonar is still cobertura. See the following doco on code coverage.

If that doesn't help, I found the following link (which runs Jacoco from Maven as your trying to do):

Upvotes: 1

Related Questions