Cobertura coverage ignore annotation in maven project? - java

I am trying to exclude certain methods from Unit test coverage. I am using Cobertura because I found out that since version 2.0 they introduced a coverage ignore annotation for excluding methods and classes: https://github.com/cobertura/cobertura/wiki/Coverage-Annotations
I set up my project as it should be, created a #interface called "CoverageIgnore" as it is in the article and annotated some methods. I am using Cobertura 2.0.3 and when generating the report the annotations don't seem to work :(
I found this article also - https://github.com/cobertura/cobertura/wiki/Ant-Task-Reference#ignore-method-annotation which talks about some configuration of the instrument task in cobertura but it seems to be Ant-compliant:
<cobertura-instrument>
<ignoreMethodAnnotation annotationName="foo.bar.CoverageIgnore"/>
</cobertura-instrument>
Is there something like this for my maven project?
Thanks.

Try this:
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
...
<instrumentation>
<ignoreMethodAnnotations>
<ignoreMethodAnnotation>foo.bar.CoverageIgnore</ignoreMethodAnnotation>
</ignoreMethodAnnotations>
</instrumentation>
...
</configuration>
...
</plugin>
...
</plugins>
...
</build>
Notice that this is in the <build> element. I have not tried it in the <reporting> element, but I know that <ignore> and <exclude> only work in <build>, so it wouldn't surprise me if the ignore annotations don't work there either.

In order for #CoverageIgnore to work you must use it for instrumentation configuration in the build section of your pom. You might have you full specification for reporting in the parent pom (if multi-module that is) and have only something like this in your child pom:
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<instrumentation>
<ignoreMethodAnnotations>
<ignoreMethodAnnotation>where.it.is.CoverageIgnore</ignoreMethodAnnotation>
</ignoreMethodAnnotations>
</instrumentation>
</configuration>
</plugin>
</plugins>
</build>
In this way plugin will be executed in the build section and correctly instrument your annotated classes (reminder: only method annotation is supported by cobertura).

for the moment I've found that there is patch for maven-plugin which needs to be applied. hopefully will be fixed in 2.7 ?!
https://jira.codehaus.org/browse/MCOBERTURA-176

Related

Use custom SpotBugs plugin in multi-module maven project

I have create a custom SpotBugs plugin for maven like described here in a multi-module maven project. But I cannot figure out how to use it. There really is not much documentation or examples for this.
Like described here I have used pluginList and plugins configuration properties for SpotBugs. But it does not report the bug when I run spotbugs:spotbugs. Other bugs are reported. Any idea what the problem might be?
I am using Spotbugs in maven like this:
<reporting>
<plugins>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>${spotbugs.plugin.version}</version>
<configuration>
<pluginList>custom-spotbugs.jar</pluginList>
<plugins>
<plugin>
<groupId>com.example</groupId>
<artifactId>custom-spotbugs</artifactId>
<version>${custom.build.version}</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</reporting>
Edit: I noticed that I had to run mvn site for the reporting plugins to work. But the next problem is that the configuration values cannot be changed. I had <effort>Max</effort> in the configuration and even if I remove it, it is still used. Are the configuration cached or something?

maven-site plugins 3.3 java.lang.ClassNotFoundException: org.apache.maven.doxia.siterenderer.DocumentContent

Since this night, maven site 3.3 plugins stop to work.
Try to delete local repository, but no change.
Maven 3.3.9
java 1.8
No config or dependencies defined in pom for site plugins
[WARNING] Error injecting: org.apache.maven.report.projectinfo.CiManagementReport
java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentContent
I had just started to get this issue also during builds. What worked for me was to specifically define the maven-site-plugin and the maven-project-info-reports-plugin along with the version numbers in the pom.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
This is caused by maven-project-info-reports-plugin updated to 3.0.0, and rely on doxia-site-renderer 1.8 (and have org.apache.maven.doxia.siterenderer.DocumentContent this class), but maven-site-plugin:3.3 rely on doxia-site-renderer:1.4 (and do not have org.apache.maven.doxia.siterenderer.DocumentContent)
We can specific maven-project-info-reports-plugin version in reporting part:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</reporting>
Or we can specify maven-site-plugin to the latest 3.7.1 like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
in build part of pom.
Version of the maven site plugin needs to be explicitly set in the build section too. Here is the example:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
<reportSets>
<reportSet>
<reports>
<report>index</report>
<report>licenses</report>
<report>dependency-info</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<!-- Part of Maven - specified version explicitly for compatibility
with the maven-project-info-reports-plugin 3.0.0-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
Maven 3 doesn't support Doxia anymore.
Use
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.2</version>
</plugin>
Reference: https://maven.apache.org/plugins/maven-site-plugin/maven-3.html
You really need to add more information (I didn't downvote BTW).
IIRC; if you don't specify a version for a plugin bound to lifecycle phases, you'll get the latest.
Try:
Upgrading to the latest version of maven - 3.5.4 ATOW
Running mvn help:effective-pom and checking which versions are
actually being resolved - if you have an old log from CI or wherever
to compare with..
Explicity setting the maven-site-plugin version
in pluginManagement section
Adding a dependency to maven-site-plugin (see below)
org/apache/maven/doxia/siterenderer/DocumentContent can be found in doxia-site-renderer:
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
<version>1.8.1</version>
</dependency>
I suspect explicitly setting the version of maven-site-plugin to whatever you used to use (incidentally) will work.
Edit: Was chasing a similar issue in maven plugin build testing, explicitly setting maven-site-plugin version (3.7.1 ATOW) in integration pom used by maven-invoker-plugin has worked for me.
The following versions in pom.xml fixed the problem for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
I also hit this error on some of my build jobs today. The fix suggested above, adding a concrete dependency for the maven-site-plugin does work and fixes this issue.
However, what it highlighted for me was the fact I was even running the mvn site goal, which I didn't even know we were running and we don't really require.
My fix was to therefore remove the site goal from my mvn arg, as although the site it creates is actually quite useful, I never knew we were creating it, we never published it anywhere and were actually deleting it every build anyway.
I tried to follow Changhua's advice, and define maven-project-info-reports-plugin to version 3.0.0, and maven-site-plugin to 3.7.1 in my pom file, but found that the maven site still pulled in version 3.3 of the maven-site-plugin, regardless of how I set it.
I finally realized that my problem had to do with our project structure. We have a parent pom, in which we were defining the maven-site-plugin dependency, which was then inherited by the children poms. However, the build pom file was separate, and didn't define maven-site-plugin at all, which allowed maven to pull in the 3.3 version on its own. I added the maven-site-plugin dependency (version 3.7.1) to the build pom file, so that it now exists in both the build pom file and the parent pom file, and now the build is correctly using version 3.7.1, and is passing again.

How maven pom.xml identifies the testng test cases in a non-standard project structure?

I am completely new to maven and testng. I am using maven as build tool, and testng as my testing framework. I am not following the standard maven project structure. Now I want my pom.xml to execute the test cases in my project. The question is, how pom.xml knows what are the test cases to consider for execution?
If you keep this in a single place you need to set the testClassesDirectory argument of the maven-surefire-plugin configuration:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<testClassesDirectory>path/to/compiled test classes</testClassesDirectory>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
All of this is well documented in the Maven Surefire Plugin Documentation

calling Sonar analysis of Java and Javascript from Jenkins using Maven not SonarRunner

I have a maven project(POM.xml) having java and Js files.I want to call sonar analysis from Jenkins.Please let me know how can I do to analyse both Java and Javascript files.I have done it for Java using sonar runner but the problem is Javascripts files are in different locations in Project directory.Hence,
sonar.src=src/main/java will not work here.I think I need to go with Maven POM.xml to do the analysis as using POM.xml the project is getting build so analysis may be possible also?
1)I know using mvn sonar:sonar is possible but what I need to add in POM.xml for Sonar?
2)What I need to change in MAVEN settings.xml?
3)Please let me know the exact step by step if possible.
Thanks in advance!
You can try this here
<project>
...
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.5</version>
</plugin>
...
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.5</version>
</plugin>
...
</plugins>
</build>
...
</project>
the website that has information to help you is this one here.

How do I add time-stamp information to Maven artifacts?

I am upgrading a large build-system to use Maven2 instead of Ant, and we have two related requirements that I'm stuck on:
We need to generate a time-stamped artifact, so a part of the package
phase (or wherever), instead of building
project-1.0-SNAPSHOT.jar
we
should be building
project-1.0-20090803125803.jar
(where the
20090803125803 is just a YYYYMMDDHHMMSS time-stamp of when the jar is
built).
The only real requirement is that the time-stamp be a part of the
generated file's filename.
The same time-stamp has to be included within a version.properties file
inside the generated jar.
This information is included in the generated pom.properties when you run,
e.g., mvn package but is commented out:
#Generated by Maven
#Mon Aug 03 12:57:17 PDT 2009
Any ideas on where to start would be helpful! Thanks!
Maven versions 2.1.0-M1 or newer have built in special variable maven.build.timestamp.
<build>
<finalName>${project.artifactId}-${project.version}-${maven.build.timestamp}</finalName>
</build>
See Maven documentation for more details.
For older Maven versions a look at maven-timestamp-plugin or buildnumber-maven-plugin.
If you use maven-timestamp-plugin, you can use something like this to manipulate resulting artifact name.
<build>
<finalName>${project.artifactId}-${project.version}-${timestamp}</finalName>
</build>
And this configuration for buildnumber-maven-plugin should create a ${timestamp} property which contains the timestamp value. There doesn't seem to be a way to create the version.properties file directly with this plugin.
<configuration>
<format>{0,date,yyyyMMddHHmmss}</format>
<items>
<item>timestamp</item>
</items>
</configuration>
These
three sites are also worth checking out.
If you use a version of Maven >= 2.1.0-M1, then you can use the ${maven.build.timestamp} property.
For more info, see:
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
This post (especially the below part) is also very useful and practical for this issue.
Stamping Version Number and Build Time in a Properties File with Maven
The pom will look like this
...
<properties>
....
<!-- Timestamp of build -->
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy_MM_dd_HH_mm</maven.build.timestamp.format>
</properties>
...
<build>
<finalName>${project.artifactId}-${project.version}-${timestamp}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
....
</plugin>
</plugins>
</build>
....
and the package name is MyProject-1.0.0-2015_03_02_13_46.war
If you need the time in a timezone other than UTC (the default when you use ${maven.build.timestamp}) you could use the build-helper-maven-plugin. See more in Brief examples on how to use the Build Helper Maven Plugin's goals.
Anyway, this is how I've got the timestamp in GMT-5 and put it in the final name of my artifact:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>current.time</name>
<pattern>yyyyMMdd-HHmmss</pattern>
<timeZone>GMT-5</timeZone>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${project.name}-${current.time}</finalName>
</configuration>
</plugin>
</plugins>
</build>
When a SNAPSHOT project is deployed, by default a timestamp is used unless you override it in the deploy plugin. If you're not getting unique timestamps, it is probably down to a configuration of your Maven repository. As the other answer says though, use the timestamp or buildnumber plugin for releases.
We need a newer answer :)
It is build in now: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
use ${maven.build.timestamp}

Categories

Resources