Share common configuration between different plugins in the same Maven module - java

I'm working on a Maven project which uses both the Surefire and Failsafe plugins in the same module. The configurations for both plugins are pretty much identical, except for one element (classpathDependencyExcludes), as you can see.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>${skipTests}</skipTests>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-javaagent:"${project.build.directory}/openejb-javaagent-${tomee.version}.jar"</argLine>
<workingDirectory>${project.build.directory}</workingDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>${skipTests}</skipTests>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-javaagent:"${project.build.directory}/openejb-javaagent-${tomee.version}.jar"</argLine>
<workingDirectory>${project.build.directory}</workingDirectory>
<classpathDependencyExcludes>
<classpathDependencyExclude>javax:javaee-api</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
I was wondering if there was a way to share the common section of the configuration between the two plugins by writing it down once instead of multiple times. If not, in the hypothesis that both configurations were identical, would it be possible?

First of all, you don't need to set all the properties explicitly. skipTests has already the value of ${skipTests}, no need to repeat that. forkCount has already the default value 1.
Some of the other properties can be set in the <properties> section, like reuseForks and argLine.
Then there is very little left to worry about.

Related

Configure maven-surefire-report-plugin to read from multiple source directories

I have configured surefire to use different output directories based on CPU count:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>3C</forkCount>
<reportsDirectory>target/surefire-reports-${surefire.forkNumber}</reportsDirectory>
</configuration>
</plugin>
I can see directories target/surefire-reports-[1..36] (could be more or less depending on machine build runs). How can I make the report plugin pick up the data from there?
I tried:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<reportsDirectories>
<reportsDirectory>target/surefire-reports*</reportsDirectory>
</reportsDirectories>
</configuration>
</plugin>
But nothing was picked up.

What is the difference between maven compiler plugin and maven toolchains plugin?

I had to integrate some legacy code into my maven build, so I used the maven-recommended toolchains plugin to change the java version:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.5</version>
</jdk>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<compilerArgs>
<arg>-Xmaxerrs</arg>
<arg>1000</arg>
</compilerArgs>
</configuration>
</plugin>
Then I ran into the max 100 compile errors problem which required passing special options to javac and found I was able to do it just with maven compiler:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<fork>true</fork>
<compilerVersion>1.5</compilerVersion>
<executable>C:\Java\jdk-1.5.0_22\bin\javac.exe</executable>
<source>1.5</source>
<target>1.5</target>
<compilerArgs>
<arg>-Xmaxerrs</arg>
<arg>1000</arg>
</compilerArgs>
</configuration>
</plugin>
Both snippets produce the same result: the java compiler is changed from the maven default to java 1.5. Both run in about the same amount of time, so there's no visible performance difference. I'd like to know if there are any benefits of one over the other so I know when to use each.
They do different things:
The compiler plugin specifically configures how your Java code is compiled (and only that).
The toolchains plugin just ensures that other plugins are all using the same Java tool chain (i.e. the same JDK) to compile, run, test, generate javadocs and so on.
This is explained in the respective plugins' documentation.
Note that not all plugins are "tool chain aware", but the compiler plugin is.
... are any benefits of one over the other
Well there there are things you can do with one and not the other and vice versa. For example, you can't set Java compiler options using the toolchain plugin.
However, they are not mutually exclusive. You can use both in the same POM file.

Cannot run multiple executions in maven surefire?

I want to run test classes whose name end with ResourceTest.java, so I defined following execution.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
<version>2.12.2</version>
<executions>
<execution>
<id>resource-tests</id>
<phase>resource-tests</phase>
<goals>
<goal>resource-tests</goal>
</goals>
<configuration>
<includes>**/*ResourceTest.java</includes>
<!-- <exludes>**/*.java</exludes> -->
</configuration>
</execution>
</executions>
</plugin>
But I'm not sure how to run this, I've searched a lot and I'm missing something.
I tried surefire:test, it skipped all the test cases as defined in above configuration. So, I tried surefire:resource-tests, maven is saying no goal is not defined.
I'm using eclipse to run my maven build, by passing these parameters. How can I run by the execution id?
How to select a specific execution when running with surefire:test when I've mulltiple executions defined in my pom?
What am I missing? Any help would be appreciated.
There are several problems with your current configuration :
you are forcing the maven-surefire-plugin to be executed in the resource-tests phase but this phase does not exist. You should delete that declaration to keep the default plugin binding phase, which is test.
you are invoking the goal resource-tests but maven-surefire-plugin does not define such a goal.
the <includes> element is ill-defined. There should be a <include> tag under it.
you are excluding all Java files from the plugin configuration so no test will be run
the configuration of the plugin should be done under the <configuration> element not for each <executions>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<includes>
<include>**/*ResourceTest.java</include>
</includes>
</configuration>
</plugin>
When you have multiple executions and you want "select" one of them, you can use a profile:
<profiles>
<profile>
<id>resource-tests</id>
<properties>
<test-classes>**/*ResourceTest.java</test-classes>
</properties>
</profile>
<profile>
<id>task-tests</id>
<properties>
<test-classes>**/*TaskTest.java</test-classes>
</properties>
</profile>
</profiles>
with the following plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<includes>
<include>${test-classes}</include>
</includes>
</configuration>
</plugin>
With such a configuration:
when you run mvn clean test -Presource-tests, only the classes matching **/*ResourceTest.java will be tested
when you run mvn clean test -Ptask-tests, only the classes matching **/*TaskTest.java will be tested

Cobertura excludes not working in multi-module Maven 3 project

I have a multi-module Maven 3 project. We are using Cobertura as our code coverage tool, but the excludes tag is not working. We have some bad tests from a package we inherited from another team, but need to consume.
The structure is as follows:
<module1>
.../com/aaaa/...
<module2>
.../com/aaaa/...
<module3>
.../com/aaaa/...
...
<moduleN>
packages with .../com/xx/... WE WANT TO EXCLUDE
pacakges with .../com/aaaa/... WE WANT TO STILL INCLUDE
parent-pom.xml
Our parent POM is configured as such:
<build>
...
<plugins>
<other plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<aggregate>true</aggregate>
<outputDirectory>coverageReports</outputDirectory>
<instrumentation>
<excludes>
<exclude>**/com/xx/**/*</exclude>
</excludes>
</instrumentation>
/configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<aggregate>true</aggregate>
<outputDirectory>coverageReports</outputDirectory>
</configuration>
</plugin>
</plugins>
</reporting>
I've tried a lot of various configurations, including:
Excluding the test/com/xx files as well
Adding the exclusion pattern to ignore
Setting exclude in the reporting AND build section
Multiple permutations of the exclude file pattern, including being more implicit
Any thoughts? I've had some other build engineers look at my various POM configurations and it always seems valid, but we never get accurate reports.
Put the exclude configuration into the pom.xml file of moduleN that you want to do the exclusions from:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<instrumentation>
<excludes>
<exclude>com/aaa/**/*.class</exclude>
<exclude>com/xxx/**/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>

What is the best way to avoid maven-jar?

I am using a different plugin (ant4eclipse) to jar my files. What is the best way to avoid the maven-jar plugin from executing?
I tried to remove the <plugin>maven-jar-plugin</plugin>
I tried to <exclude> ** / * < / exclude>
I tried to <skip>true</skip>
None worked
In Maven 3.0.x (I tried 3.0.2) you can disable maven-jar-plugin by binding the default-jar execution to a nonexistent phase, as #bmargulies suggested. Unfortunately that doesn't work in 2.2.1, but you can prevent it from interfering with your own jar by setting an alternative <finalName> and <classifier> for the default-jar execution; it will still create a jar, but it will be set as a secondary artifact for the project and won't overwrite the one you've created. Here's an example that should work in both Maven 2 and Maven 3:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Once you've disabled maven-jar-plugin, maven-install-plugin may give you trouble too. In Maven 3 it can be disabled the same as maven-jar-plugin: bind default-install to a nonexistent phase. However, in Maven 2 maven-install-plugin requires that the target/classes directory exist, and it will install the dummy jar when there isn't a primary artifact present.
This should do the trick - notice the use of <id>default-jar</id> and <phase/>.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase/>
</execution>
</executions>
</plugin>
</plugins>
</build>
In my case, I only wanted to disable the jar plugin because the jar was empty. You can use the skipIfEmpty option in the plugin configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</plugin>
What happens if you declare this?
<packaging>pom</packaging>
Even if it does what you're looking for, be careful. I'm not sure if there could be negative side effects -- such as other maven projects that depend on your jar not being able to locate it.
Using maven 3.3.9, the following worked for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
So in case of the maven-jar-plugin, I bound it to a non-existent phase. For the maven-install-plugin, I used the "skip" configuration parameter. The documentation about it says: "Set this to true to bypass artifact installation. Use this for artifacts that does not need to be installed in the local repository."
Explicitly bind the jar plugin to a phase that doesn't exist.
As other's have said, it's not possible to turn it off, other than using <packaging>pom</packaging>, which turns everything off and is probably not what you want.
Even though it will generate twice, a working solution is to bind your jar process to the package phase, as that is guaranteed to run after the default. By overwriting the same JAR file, you'll find that yours is used wherever the original would have been.
I am using a different plugin to jar my files. What is the best way to avoid the maven-jar plugin from executing?
First, the jar:jar goal is bound by default on the package phase for a project with a packaging of type jar. Second, there is no way to unbind a plugin bound to a phase. So, if you are using another plugin(?), either accept to produce 2 JARs or change the packaging (but I don't think this will work well).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

Categories

Resources