Managing report plugins in maven site - maven

I'm using Maven 3 in a multi module project and configured site with following report plugins:
1. findbugs
2. cobertura
3. surefire report for tests
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven.site.version}</version>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
</plugin>
</reportPlugins>
</configuration>
</plugin>
If I use:
mvn site -DskipTests=true after mvn clean install
I dot not get proper reports after mvn site. Cobertura related test coverage report does not shows correct data i.e. all packages as 0% test coverage. It seems cobertura wants to run tests itself to determine coverage.
But when I run
mvn site
This makes my all tests run twice once each for cobertura and surefire reports.
I want to run tests only once and generate the required reports. Please let me know the correct way to achieve this using above reportPlugins in maven.

Cobertura and surefire report (for site generation) do two different things. Hence you cannot avoid running the tests twice - once for cobertura and one for site.
What you could look at is moving the cobertura run out of maven default life-cycle and run it separately as required. This way, mvn site will only run surefire for site generation.

Related

A single Junit test is running twice in Maven build

I am running junit tests on a project with mvn clean test command in Jenkins
Strangely only of the tests of a particular test class is running twice.
Other tests are only running once.
maven surefire configuration in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
</configuration>
</plugin>
i am totally clueless
Best Regards,
Saurav

SonarQube 4.4 not displaying cobertura unit tests or code coverage for grails 2.4.3 with spock tests

I have SonarQube 4.4 installed on my machine. I am trying to generate metrics for a grails (2.4.3) project that has tests written in Spock framework. However, sonar does not show unit tests results or unit tests coverage.
I verified that cobertura coverage.xml gets generated in the path provided below
target/test-reports/cobertura/coverage.xml
And I have
sonar.groovy.cobertura.reportPath
property in my pom.xml mapped to the above path.
Running
mvn sonar:sonar (maven version 3.2.3)
on this project completes successfully and the console log shows the coverage.xml being parsed.
However when I view the project in sonar, I only see the static code analysis metrics but no unit tests or unit tests coverage.
I also tried to run the analysis using sonar-runner and get the same result.
I tried sonar-groovy-plugin (versions: 0.5, 0.6, and 1.0.1) and sonar-cobertura-plugin-1.6.3
Here is the pom.xml build section to run the groovy spock tests
<plugin>
<groupId>org.grails</groupId>
<artifactId>grails-maven-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<grailsVersion>${grails.version}</grailsVersion>
<fork>false</fork>
</configuration>
<extensions>true</extensions>
<executions>
<execution>
<id>grails-tests</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<command>test-app</command>
<args>--unit:spock</args>
</configuration>
</execution>
</executions>
</plugin>
Anyone used a similar configuration and able to generate code coverage metrics using sonarqube and cobertura?
For anyone else facing a similar problem, here is the solution. It turns out we need to set the "sonar.sources" property in the pom.xml for coverage to show up. Here is the sample (you need to specify the individual directories)
<sonar.sources>src/java,src/groovy,grails-app/services,grails-app/controllers,grails-app/domain,grails-app/jobs</sonar.sources>

Checkstyle working in Maven, but not working in Jenkins

I am using Maven, Maven checkstyle plugin, Jenkins and Jenkins checkstyle plugin.
I have 7 projects total. 1 parent and 6 child projects.
I have the following 3 scenarios :
Checkstyle report for all project.n parent pom i declared checkstyle plugin in tag:
I run mvn clean site command
It working both ways in eclipse with maven. and in Jenkins also.
Giving checkstyle reports for all 7 projects.
Checkstyle report for all project except 2 project.
I deleted part in parent pom and i added
I added in remaining 4 projects, i declared checkstyle plugin in tag.
It working in eclipse but not working in Jenkins.
Please give solution
My code:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.10</version>
<configuration>
<configLocation>../parent-project/checkstyle.xml</configLocation>
</configuration>
</plugin>
</plugins>
3.
I deleted checkstyle plugin in parent
I added checkstyle plugin in 4 needed projects same above code.
In remaining 2 child projects I added like this.
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
I run 'mvn clean site'
It working in Maven as expected but not working in Jenkins.
What I mean by 'not working in Jenkins' is that I installed checkstyle plugin in Jenkins, In the Jenkins job i selected 'Publish Checkstyle analysis results' check box. In scenario-1 i am able to see the checkstyle link in the jenkins job page. And if i click on that all code violations reports are appearing. In other scenarios (2,3) the Checkstyle link also not appearing.
Just found the solution.
In the project configure menu, in the "Goals and options" field you need to add 'site' without the ' characters. In my "Goals and options" field I had: clean install. This did not run checkstyle. But then I changed it to: clean install site. And now checkstyle runs!
Hope this solves your problem.
Edit:
My pom.xml has been extended with the tag:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.10</version>
</plugin>
</plugins>
to run checkstyle. Try using only that for checkstyle and then run the project again.

NoClassDefFoundError with Spring 3.1 Controller Unit Test

I have a simple autowired Controller and a test. When the project is built with mvn and code coverage goals emma:emma and emma:check, the test case fails with following error.
Simple "mvn test" command succeeds.
"mvn test emma:emma emma:check" fails.
java.lang.NoClassDefFoundError: org/springframework/web/bind/support/WebDataBinderFactory
Try using cobertura, add this to your report section:
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<check>
<lineRate>120</lineRate>
</check>
</configuration>
</plugin>
</plugins>
</reporting>
then do:
mvn site
then open in browser:
target/site/index.html
You will see the cobertura report there.
Best to add these kind of plugins to your parent pom.
Do you have dependency of spring-web in test. If not, then try adding dependency on org.springframework:spring-web

Maven sonar plugin configurations for findbugs pmd checkstyle cobertura

I need some help in setting up code quality plugin for maven project.
I have a multi module project. While I have configured pmd, checkstyle, findbugs and cobertura in my build process, and I can generate xml reports for each plugin, I am facing some challenges configuring the sonar plugin in my project.
I am not sure how to approach this problem:
Should I reuse the reports generated by these plugins while executing sonar? if so what should my sonar plugin configuration be?
If I run sonar with embedded pmd, checkstyle, findbugs and cobertura plugins, how do I configure them to run only for specific packages or make findbugs analyze on com.mycompany.- structure.
Lastly I cannot get coverage report in sonar, either running cobertura external to sonar or within sonar.
I have my pom below for review. Any help will be tremendously appreciated.
This is in the plugins section of the build section in my root pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<instrumentation>
<includes>
<include>com/mycompany/**/*.class</include>
</includes>
</instrumentation>
<formats>
<format>xml</format>
</formats>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.6</targetJdk>
<includes>
<include>com/mycompany/**/*.java</include>
</includes>
<excludeRoots>
<excludeRoot>target/generated-sources/*</excludeRoot>
</excludeRoots>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<onlyAnalyze>com.mycompany.-</onlyAnalyze>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<includes>com/mycompany/**/*.java</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
1) should I reuse the reports generated by these plugins while
executing sonar? if so what should my sonar plugin configuration be?
Sonar does not provide a mechansim to reuse reports generated by these plugins. You could configure the rules through the quality profiles.
2) if I run sonar with embedded pmd, checkstyle, findbugs and cobertura plugins, how do I configure them to run only for specific packages or make findbugs analyze on "com.mycompany.-" structure.
Sonar web UI allows you to specify the exclusion for your findbugs filters. Likewise for cobertura. Not sure about pmd, checkstyle.
3) lastly i cannot get coverage report in sonar, either running cobertura external to sonar or within sonar.
It could be possible due to jacoco being the default code coverage engine. You could run mvn clean verify/install sonar:sonar as instructed and see if it works.

Resources