How to configure FindBugs to run only on one project in a multi-module maven project - maven

I use the findbugs-maven-plugin to check for bugs with maven. My maven project is a multi-module project that roughly looks as follows:
java-module
pom.xml
src/ ...
pom.xml
scala-module
pom.xml
src/ ...
I use Jenkins to build and test the project, and Jenkins runs goal findbugs:findbugs in the top-most directory. Since FindBugs reports many spurious warnings for code that is generated by the Scala compiler, I would like to tell FindBugs not to analyze the code in scala-module. However, when I run findbugs:findbugs in the top-most directory, it always analyzes all classes in java-module and scala-module. How can I tell maven to ignore scala-module as a whole? I know about FindBugs exclude filters but I would to have a configuration option for FindBugs that tells it to simply not analyze the code in a certain submodule.
FindBugs is configured in pom.xml in subdirectory java-module as follows:
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${version.plugin.codehaus.findbugs}</version>
<configuration>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
</plugins>
</reporting>
Despite the configuration being done only for the java-module, FindBugs will always also analyze scala-module.

Add a configuration the scala-module pom.xml that explicitly instructs findbugs to skip the module, i.e.
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</reporting>
Note that Maven often requires you to repeat boilerplate XML for cases like this.

Noahlz's answer did not work for me, but adding the following snippet to the sub-module's POM.xml did the trick.
<properties>
<findbugs.skip>true</findbugs.skip>
</properties>

Related

Maven Findbugs plugin not executing with mvn site command

I'm trying to get Findbugs working with an existing/mature Maven project.
I added the following to the <plugins> tag in my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<effort>Max</effort>
<threshold>Low</threshold>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
I then see that the Findbugs plugin runs when you run mvn site. Since the build invocation for my project is aleady:
mvn clean jacoco:prepare-agent test site jxr:jxr -Dkey1=123 -Dkey2=456 -Denvironment=DEV
...I just run it like I normally do. The build succeeds and I go to my normal site summary in my browser:
No where from here can I find any "Findbugs" reports or anything that mentions Findbugs at all. When I do a grep for Findbugs (grep -rl "findbugs" .) I do see that many of my ./target/surefire-reports/TEST-*.xml files have the term "findbugs" mentioned in them.
Worth mentioning that I do not see a target/site/findbugs.html file after the successful build...
Any ideas as to where I can find HTML Findbugs output under my Site summary (or anywhere else)? Looking for a nice HTML report showing which (if any) Findbugs checks failed.
You should add the FindBugs plugin in the <plugins> section of the <reporting> section of your pom.xml:
<project>
...
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
</plugin>
</plugins>
</reporting>
...
</project>
The question just states <plugins>, not sure if that could be the issue.

Maven plugins without <executions> tag

I am studying Maven plugins from the official references. I read:
You can also configure a mojo using the executions tag. This is most
commonly used for mojos that are intended to participate in some
phases of the build lifecycle.
but If I do not use the <executions> tag, like this:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-myquery-plugin</artifactId>
<version>1.0</version>
<configuration>
<url>http://www.foobar.com/query</url>
<timeout>10</timeout>
<options>
<option>one</option>
<option>two</option>
<option>three</option>
</options>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
what happens?
Will we be able to run a goal from maven-myquery-plugin only in isolation giving a command like mvn myquery:myquery or does the plugin come up bundled in some phase/life cycle by default?
I also have been wondering if it's possible to skip the executions tag. As there is no direct answer on the official website I tried to make such a plugin with default phase bindings by myself and the answer is: no goals will be executed if they weren't specified in the executions tag. Such a plugin (goal) could be executed only by direct command: <plugin>:<goal>

Should pluginManagement be used in submodules?

I am reading sonatype's tutorial on multimodule projects and I see that in submodule they use the <pluginManagement> configuration like this:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
This confuses me as I thought that <pluginManagement> is to be used in parent POM to provide common plugin configuration for submodules. What are the reasons for using plugin managment in children poms ?
Most of the time <pluginManagement> is used in parent pom files.
It configures plugins. But makes them not active part of the build. Therefor you need to add them to the <plugins> part of the Maven pom file. Most not done in parent pom files, but done in the (sub) modules, using / refering to that (parent) pom.
I think this is special to the use of the surefire plugin.
Since you execute the plugin with mvn test the surefire plugin will be executed with the configuration specified in the pluginManagement block. Other plugins are not executed directly but by binding them to another lifecycle phase and thus must be specified in the plugin block.
See the usage page of the surefire plugin.

Why would a maven-war-plugin generate a JAR instead of a WAR?

I am following this Contract first using CXF tutorial and while the resulting pom.xml generates sources and even completes build successfully, it fails to create a WAR file.
Instead, it creates a JAR file.
My understanding is that the part in the pom.xml that's responsible for creating the WAR is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<outputDirectory>D:/path/to/profile/autodeploy</outputDirectory>
</configuration>
</plugin>
I don't see any <goal> or <execution> element there (unlike in the build-helper-maven-plugin one), but I also understand that with this plugin this is implied as even the official usage page demonstrates:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
So... what am I missing?
What could possibly explain a maven-war-plugin that behaves in unexpected way like this and produces a JAR instead of a WAR by default?
Is there a way to force it to produce a WAR?
packaging should be as below.
<packaging>war</packaging>
if it won't help then try binding your plug-in configuration with a lifecycle phase.
in your project definition , please check if packaging is missing or not , it should be some thing like this .
<groupId>some.groupid</groupId>
<artifactId>My Web Application</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<description>My First Web Application</description>
By default maven war plugin binds to package phase of the lifecycle ,so its important that we should mention the type of packaging we want once the build is done.
I would like to suggest to have a look at the Maven specs for war plugin.

Skipping tests in some modules in Maven

I would like my Maven builds to run most unit tests. But there are unit tests in one project which are slower and I'd like to generally exclude them; and occasionally turn them on.
Question: How do I do this?
I know about -Dmaven.test.skip=true, but that turns off all unit tests.
I also know about skipping integration tests, described here. But I do not have integration tests, just unit tests, and I don't have any explicit calls to the maven-surefire-plugin. (I am using Maven 2 with the Eclipse-Maven plugin).
What about skipping tests only in this module ?
In the pom.xml of this module:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
Eventually, you can create a profile that will disable the tests (still the pom.xml of the module) :
<project>
[...]
<profiles>
<profile>
<id>noTest</id>
<activation>
<property>
<name>noTest</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
[...]
</project>
With the latter solution, if you run mvn clean package, it will run all tests. If you run mvn clean package -DnoTest=true, it will not run the tests for this module.
I think this is easier, and also has the benefit of working for non-surefire tests (in my case, FlexUnitTests)
<profile>
<id>noTest</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
If you have a large multi-module project and you would like to skip tests only in certain modules without the need to change each of the module pom.xml file with custom configuration and profiling, you could add the following to the parent pom.xml file:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>maven.test.skip</name>
<value>${project.artifactId}</value>
<regex>(module1)|(module3)</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
Thanks to the build-helper-maven-plugin you would actually dynamically check whether you are in a certain module or not during the build, via the project.artifactId property (pointing at each artifactId module during the build), the regex would then seek matching for certain values (the module names for which you want to skip tests) and populated the maven.test.skip property accordingly (setting it to true).
In this case, tests will be skipped for module1 and module3 while running properly for module2, that is, as expressed by the regex.
The advantage of this approach is to have it dynamic and centralized (in the parent pom.xml) hence better for maintenance: you could add or remove modules at any time simply by changing the simple regex above.
Obviously, if this is not the default behavior of the build (recommended case), you could always wrap the snippet above in a maven profile.
You could also go further and have dynamic behavior based on your input:
<properties>
<test.regex>none</test.regex>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>maven.test.skip</name>
<value>${project.artifactId}</value>
<regex>${test.regex}</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here we are actually replacing the regex value with a property, test.regex, with default value to none (or whatever would not match any module name or, also, the default skipping matchings required).
Then from command line we could have
mvn clean test -Dtest.regex="(module1)" > will skip tests only for module1
mvn clean test -Dtest.regex="(module1)|(module2)" > will skip tests on module1 and module2
mvn clean test -Dtest.regex="(module1)|(module2)|(module3)" > will skip the three module tests
mvn clean test -Dtest.regex=".+" > will skip all module tests
mvn clean test > would not skip anything (or fall back on default behavior)
That is, then at runtime you decide, without any need to change the pom.xml file or activating any profile.
Using Surefire Plugin 2.19 you can simply exclude the tests you don't want using regular expressions:
mvn '-Dtest=!%regex[.*excludedString.*]' test
The above command will exclude all the tests that contain excludedString.
NB1 If double quotation mark(") is used instead of apostrophe(') the command will not be interpreted properly and will produce unexpected results. (Tested using bash 3.2.57)
NB2 Particular attention should be paid to projects in which multiple version of the surefire plugin is used. Versions of surefire older than 2.19 will not execute any tests because they do not support regular expressions.
Version management(it might be a good idea to add this in the parent pom file):
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Examples of build commands that skip tests: https://artbcode.wordpress.com/2016/11/28/how-to-skip-a-subset-of-the-unit-tests/
I had a slightly different need from this question that may prove helpful. I wanted to exclude from the command line a few different tests from different packages, so a single wildcard would not do it.
I found in the Maven Failsafe documentation rules for exclusions that you can specify a comma-separated list of either regex or wildcard exclusions:
https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html
So my pomfile looked like this:
<excludes>
<exclude>${exclude.slow.tests}</exclude>
</excludes>
and my command line included this:
mvn install "-Dexclude.slow.tests=**/SlowTest1.java, **/package/ofslowtests/*.java, **/OtherSlowTest.java"
For me the key ingredient was getting a bunch of tests into one maven property in a single exclude statement.

Resources