How do I correctly integrate Maven, Jenkins, Sonar and Cobertura?
Asked Answered
L

1

13

hope someone can help.

  • our project is an Eclipse plug-in project
  • we have a Jenkins server
  • our project uses Maven and Tycho (for plug-in projects) for build management
  • we installed a Sonar server
  • we integrated Sonar into Jenkins by using the Jenkins Web-Config

Now if we do not use Sonar, everything is okay. But if we turn it on, the Maven Cobertura plugin for Sonar throws errors. Everything else (as far as the output goes) is okay.

Now, from what I understand, the following happens:

  • Jenkins builds the project
  • Sonar uses its static code analysis plugins and metrics (Findbugs, PMD, RFC, etc.)
  • Sonar starts the Cobertura plugin
  • Cobertura tries to build the project AGAIN
  • Cobertura doesn't succeed in building it because it builds it in a different order
  • Cobertura fails

Some output excerpts from Jenkins:

[INFO] ------------------------------------------------------------------------
[INFO] Building Project
[INFO] ------------------------------------------------------------------------

[WARNING] The following dependencies could not be resolved at this point of the build but seem to be part of the reactor:

[WARNING] o de.tool.core:de.tool.core:eclipse-plugin:1.2.0-SNAPSHOT (provided)

[WARNING] Try running the build up to the lifecycle phase "package"

Later...The build order is that core is built before evolution, but evolution seems to be the first thing Cobertura wants to build...

[INFO]  Execute maven plugin cobertura-maven-plugin...
[INFO]  Execute org.codehaus.mojo:cobertura-maven-plugin:2.5:cobertura...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building de.tool.evolution 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> cobertura-maven-plugin:2.5:cobertura (default-cli) @ de.tool.evolution >>>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.240s
[INFO] Finished at: Tue Oct 25 11:28:29 CEST 2011
[INFO] Final Memory: 57M/231M

After that the builds always amount to something like this:

[INFO] ------------------------------------------------------------------------
[INFO] Building de.tool.core 1.2.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> cobertura-maven-plugin:2.5:cobertura (default-cli) @ de.tool.core >>>
[INFO] 
[INFO] --- tycho-packaging-plugin:0.13.0:build-qualifier (default-build-qualifier) @ de.tool.core ---
[INFO] 
[INFO] --- tycho-packaging-plugin:0.13.0:validate-id (default-validate-id) @ de.tool.core ---
[INFO] 
[INFO] --- tycho-packaging-plugin:0.13.0:validate-version (default-validate-version) @ de.tool.core ---
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ de.tool.core ---
[INFO] Using 'Cp1252' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/jobs/testBuild/workspace/de.tool.core/src/main/resources
[INFO] 
[INFO] --- tycho-compiler-plugin:0.13.0:compile (default-compile) @ de.tool.core ---
[INFO] Using compile source roots from build.properties
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- cobertura-maven-plugin:2.5:instrument (default-cli) @ de.tool.core ---
[INFO] Cobertura 1.9.4.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 88 files to /var/lib/jenkins/jobs/testBuild/workspace/de.tool.core/target/generated-classes/cobertura
Cobertura: Saved information on 88 classes.
Instrument time: 256ms

[INFO] Instrumentation was successful.
[INFO] NOT adding cobertura ser file to attached artifacts list.
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ de.tool.core ---
[INFO] Using 'Cp1252' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/jobs/testBuild/workspace/de.tool.core/src/test/resources
[INFO] 
[INFO] <<< cobertura-maven-plugin:2.5:cobertura (default-cli) @ de.tool.core <<<
[INFO] 
[INFO] --- cobertura-maven-plugin:2.5:cobertura (default-cli) @ de.tool.core ---
[INFO] Cobertura 1.9.4.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 88 classes.
Report time: 800ms

[INFO] Cobertura Report generation was successful.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 50.133s
[INFO] Finished at: Tue Oct 25 11:28:44 CEST 2011
[INFO] Final Memory: 33M/263M

Followed by warnings:

[INFO]  Java bytecode scan...
[WARN]  Class 'de/tool/core/util/EObjectUtil' is not accessible through the ClassLoader.
[INFO]  Java bytecode scan done: 105 ms
...
The following classes needed for analysis were missing:
  de.tool.core.util.EObjectUtil

...and a final punch in the gut:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] de.tool.core ............................... SUCCESS [2.364s]
[INFO] de.tool.evolution .......................... FAILURE [0.023s]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:52.418s
[INFO] Finished at: Tue Oct 25 11:29:46 CEST 2011
[INFO] Final Memory: 51M/411M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project de.tool.evolution: Could not resolve dependencies for project de.tool.evolution:de.tool.evolution:eclipse-plugin:1.0.0-SNAPSHOT: Could not find artifact de.tool.core:de.tool.core:eclipse-plugin:1.2.0-SNAPSHOT -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project de.tool.evolution: Could not resolve dependencies for project de.tool.evolution:de.tool.evolution:eclipse-plugin:1.0.0-SNAPSHOT: Could not find artifact de.tool.core:de.tool.core:eclipse-plugin:1.2.0-SNAPSHOT

My guess is that the build order is incorrect in Sonar. But how would it get jumbled (since Jenkins builds the project in the correct order)?

And why on Earth does Cobertura build the project again? Jenkins already built it and Cobertura could use the compiled classes from there...or am I misunderstanding something?

Lozier answered 25/10, 2011 at 13:52 Comment(2)
I cannot answer the main question but I think Cobertura needs to rebuild project to weave code capturing coverage info (used later by Sonar). It is frustrating that code is being build twice. In our case we moved all code analysis/coverage into separate Jenkins job and running it nightly because it takes too long.Kolva
@Lozier Have you maybe SonarQube + Jenkins + Maven and Cobertura ?Yulma
H
12

I just completed configuring my company's Jenkins server with Sonar for use with our Maven-built project. So here are the steps I had to go through.

In my pom.xml (Maven2 version), I had to add the following code:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>1.0-beta-2</version>
        </plugin>

On our Linux build server, we already had MySQL installed, so we followed instructions from Wakaleo Consulting on how to get Sonar to talk to MySQL. Note the escaped ampersand in the MySQL URL in the Jenkins configuration section; that threw us for a loop for a few builds.

Also, take note that you do not need to include the sonar:sonar target on your Maven targets line...Enabling Sonar at the project level was enough for our Jenkins job to gather all of its statistics.

I think that about covers it.

Halverson answered 2/11, 2011 at 21:28 Comment(1)
Un-zombified the Wakaleo link.Halverson

© 2022 - 2024 — McMap. All rights reserved.