Rechercher dans ce blog

samedi 28 avril 2012

6. Code coverage

Maven post was not containing the full configuration. For simplicity sake, I deliberately removed the code coverage section.

Now that jenkins and maven are configured, we can now take some time to configure the code coverage. For this part, I use Cobertura because it runs under Jenkins and it is easy to use...

To configure it, there are two things to do:


  1. Configure the Maven configuration 
  2. Configure Jenkins to support Cobertura

Configure Maven

To configure maven, we have to edit the project pom.xml to set that cobertura must be run. We will run cobertura during the package goal:

 
49    
50    
51       <profiles> 
52           <!-- Jenkins by default defines a property BUILD_NUMBER which is used to enable the profile. --> 
53           <profile> 
54               <id>jenkins</id> 
55               <activation> 
56                   <property> 
57                       <name>env.BUILD_NUMBER</name> 
58                   </property> 
59               </activation> 
60               <build> 
61                   <pluginManagement> 
62                       <plugins> 
63                           <plugin> 
64                               <groupId>org.codehaus.mojo</groupId> 
65                               <artifactId>cobertura-maven-plugin</artifactId> 
66                               <version>2.5.1</version> 
67                               <configuration> 
68                                   <formats> 
69                                       <format>xml</format> 
70                                   </formats> 
71                               </configuration> 
72                               <executions> 
73                                   <execution> 
74                                       <phase>package</phase> 
75                                       <goals> 
76                                           <goal>cobertura</goal> 
77                                       </goals> 
78                                   </execution> 
79                               </executions> 
80    
81                           </plugin> 
82                       </plugins> 
83                   </pluginManagement> 
84    
85                   <plugins> 
86                       <plugin> 
87                           <groupId>org.codehaus.mojo</groupId> 
88                           <artifactId>findbugs-maven-plugin</artifactId> 
89                           <version>2.4.0</version> 
90                           <executions> 
91                               <execution> 
92                                   <phase>package</phase> 
93                                   <goals> 
94                                       <goal>findbugs</goal> 
95                                   </goals> 
96                               </execution> 
97                           </executions> 
98                       </plugin> 
99                   </plugins> 
100              </build> 
101          </profile> 
102      </profiles>

A few explanations are needed:

  • We set here a profile: that means this goal is run only when a particular "user" is running the target. It is good in this case because cobertura will not be run in developer's environment but only by jenkins
  • The plugin is specified to run during the package phase

Configure Jenkins

Now that maven is configured, we can configure Jenkins to use cobertura and this is quite easy:

  1. Install the Cobertura plugin: Jenkins Cobertura plugin. It will be installed with a couple of dependencies.
  2. Go in the project settings and set that you want to run the following goals:
    • package: to run cobertura
    • cobertura:cobertura: to build the result
  3. Go in the project settings and set that you want to display the coverage report. You are asked for a report pattern: set: **/target/site/cobertura/coverage.xml. This will inform cobertura where the pattern are stored and that they are in xml (exactly what we set in the pom.xml.
Now you can run a build and you will see the code coverage of the project.

Aucun commentaire:

Enregistrer un commentaire