We have a multi module maven project. We are using jacoco for code coverage analysis. I prepared the pom file to run the test cases and pick up the code coverage. When i build the project on my local i can see the code coverage on the sonar dashboard.
After pushing the changes to the github and when jenkins build got triggered the code coverage is not available on the sonar dashboard. I found the following error in the jenkins build logs:
Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?
How can I make the code coverage available after the successful jenkins build?
You have to make sure that while compiling Java or Groovy etc code, the debug option is turned on. I use Gradle (instead of Maven) and to enable debug option, I do something like this in Gradle. You can put the following code in project's build.gradle OR top level $GRADLE_HOME/init.d/some-global.gradle file (inside allprojects { ... } section).
tasks.withType(JavaCompile) {
options.debug = true
options.compilerArgs = ["-g"]
}
tasks.withType(GroovyCompile) {
options.debug = true
}
Do the same in Maven (how to turn debug option on using -g for Java and true for Groovy based projects).
After this, all you need is make sure your sonar.xx.yyy variables are set correctly while calling sonar runner (i.e. sonarRunner task in Gradle).
I set those variables as (UT for Unit tests, IT for Integration Tests). IT code coverage will be generated only if you attach jacocoagent.jar to the Tomcat/external JVM (instead of what Gradle/Maven JVM is) which runs your application's .war/.ear file.
-Dsonar.jacoco.itReportPath=build/jacoco/IT/jacocoIT.exec
-Dsonar.jacoco.reportPath=build/jacoco/UT/jacocoUT.exec
There are other sonar.xx.yy variables you can see online/docs.
In Maven, you can turn Java debug on using this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
</configuration>
</plugin>
Related
My maven project executes maven-surefire-plugin v2.22.0 TEST while building the application. By default the log level of surefire execution is INFO, and this plugin uses [org.apache.logging.slf4j.Log4jLoggerFactory].
I don't want my build process to log these traces while executing : maven-surefire-plugin:2.22.0:test (default-test)
Please could anyone help how to skip logging in this plugin execution?
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.22.0</version>
I have tried exclusions and verbose=false, but no luck :(
Put alog4j.properties under src/test/resources with the following content:
log4j.rootLogger=OFF
In my maven project I have a number of modules. Is it possible to turn off running unit test for some modules via command line options?
My project takes about 15 mins to run through all unit tests. I would like to speed up the overall build by running just the unit tests in the module I am working on. I do not want to go in and edit each individual pom.xml to achieve this.
I have tried a solution outlined here: Can I run a specific testng test group via maven? However the result is a lot of test failures in modules that I want to skip. I suppose 'group' is not the same concept of module?
To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.
If you need more fine grain control of running a subset of tests for a module, look into using the Maven Surefire Plugin's test inclusion and exclusion capabilities.
To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. Take for example the following POM segment:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude>${someModule.test.excludes}</exclude>
</excludes>
<includes>
<include>${someModule.test.includes}</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<someModule.skip.tests>false</someModule.skip.tests>
<skipTests>${someModule.skip.tests}</skipTests>
<someModule.test.includes>**/*Test.java</someModule.test.includes>
<someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
</properties>
With a POM like the above you can execute tests in a variety of ways.
Run all tests (the above configuration includes all **/*Test.java test source files)
mvn test
Skip all tests across all modules
mvn -DskipTests=true test
Skip all tests for a particular module
mvn -DsomeModule.skip.tests=true test
Only run certain tests for a particular module (this example includes all **/*IncludeTest.java test source files)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" test
Exclude certain tests for a particular module (this example excludes all **/*ExcludeTest.java source files)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test
Found a way to exclude on command line:
# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*
This is from: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/
…and if you like to pass the parameter to maven release plugin in Hudson/Jenkins you have to use
-Darguments=-DskipTests
to get it work.
If you want to use Maven profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
you might want to make it work doing something like this:
Skipping tests in some modules in Maven
I don't know if there is a supported command line option that does the same.
You also might try using environment properties directly, something as per this doc page:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html
i.e. something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${moduleA.skipTests}</skipTests>
</configuration>
</plugin>
then using mvn -DmoduleA.skipTests=false test to test that one module.
I'm using the JBoss and WildFly Maven plugin to deploy my applications.
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.0.Beta1</version>
</plugin>
</plugins>
The problem that I have found is that if I have some Junit test in my project they are executed before application deployment, leading to a test with an inconsistent application state. Is it possible to configure somehow this plugin to kick-in before the test phase ?
Thanks
This plugin by default executes alongside the package phase and you'll probably have issues attempting to run the plugin before your application is packaged. As an alternative, you could override the maven-surefire-plugin to run your tests in a later phase, such as integration-test, which would be executed after your application has been packaged.
A practical example can be found on this Maven tutorial where it's show how to include failsafe plugin and bind it to the executions of the integration-test and verify phase. This way tests which are engineered as integration test (e.g ending in *IT) will execute only during the integration-test phase.
Please let me know how to configure sonar to use cobertura instead of jacoco which is the default.
i have this in my parent pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
Apart from this i dont know how to add configuration properties.Cobertura setting is there in child poms which will build n show coverage with cobertura locally.
I problem is configuring the right setting in parent pom.and what are the changes i should make in the sonar web interface ->General Settings.
Help Appreciated.Thank you.
There is documentation about the properties. I suspect you want to redefine the sonar.java.coveragePlugin property as a starter.
You'll have to configure that in Sonar directly. Besides, in order to use Cobertura for Java 7 projects you need Cobertura >= 2.0 which was only released a few days ago.
As a sidenote, here a two somewhat older comparisons of the various coverage tools for Sonar:
http://java.dzone.com/articles/code-coverage-tools-comparison
http://www.sonarsource.org/pick-your-code-coverage-tool-in-sonar-2-2/
I have added the dependency but for some reason Cobertura decided against loading any of my test classes from src/test/. It just loads the classes from src/java/ and hence doesn't show the code coverage or anything. I checked the packages (all the classes, tests are in the same package) and the dependencies. Any help, pointers??
Here is my cobertura dependency plugin under maven-shade configurations:
<cobertura.version>2.5.2</cobertura.version>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
</plugin>
I also have the same thing under maven-site plugin
Your structure is not mavenized, unless you have overridden the default maven configuration. It should be src/main/java and src/test/java.
Also check that you have followed surefire plugin (the plugin that runs tests) conventions (e.g. *Test.java) or have overridden configuration see surefire inclusion-exclusion for all of the default patterns accepted.
Verify that mvn clean test has run your tests, i.e. look for "Tests run: 52, Failures: 0, Errors: 1, Skipped: 0" in the output.
Verify that mvn cobertura:cobertura also runs your tests and produces a index.html in /target/site/cobertura/ (open this file to ensure that it includes all of your classes). Also check for the presence of /target/cobertura/cobertura.ser.