Use custom SpotBugs plugin in multi-module maven project - java

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?

Related

Maven compiler plugin vs maven default plugin?

Maven is a powerful build tool for Java software projects.
I was reading about Maven plugins. (maven-compiler-plugin)_
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
For any project to run using maven, it definitely need to compile the code.
But i can see many sample projects online which doesn't have this plugin and still work.
Can anybody explain what is the difference in this plugin and by default compiling?
If you do nothing, Maven will call the maven compiler plugin which is called in the maven default lifecycle.
If you want to change the configuration, you need to add the plugin to <plugins> and then change the configuration. Often people do this to change the Java version (which can also be done through properties) or to fix the version of the plugin (instead of always using the latest version).

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.

Cobertura coverage ignore annotation in maven project?

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

Categories

Resources