Call plugin when Maven profil is set - java

I have a Maven project and I have two profiles (Dev and integraiton). I use integration-profile to call two plugins, WAR and EAR maven plugin.
My question : how can I use the plugin in the pom.xml dependencies when integration-profile is set in the Maven goal (I don't Like to have a plugins call in the profile)?
<profiles>
<profile>
<id>integration-profile</id>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/jboss-web.xml</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<packagingExcludes>conf/jboss-roles-mapping.conf</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

Related

aggregating surefire reports in MultiModule project

I have multimodule maven project I want to get aggregated surefire reports . I tried below approach but I dont see aggregated surefire report generated. I could see report generated for individual modules.
I have added below plugin configuration in indvidual Modules (packaging jar)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
<configuration>
<useManifestOnlyJar>false</useManifestOnlyJar>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<aggregate>true</aggregate>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
</plugins>
</reporting>
And below in root level pom (packaging type pom)
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${version.plugin.surefire}</version>
<inherited>false</inherited>
<configuration>
<aggregate>true</aggregate>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
</plugins>
</reporting>
Could some one please help where I am going wrong ?
Try with mvn surefire-report:report -Daggregate=true

Configure maven-compiler-plugin so it works both with JDK-8 and JDK-12 (in Spring Boot project)

Is it possible to configure maven-compiler-plugin so it works both with JDK 8 and JDK 12? I am not sure if it is relevant, but it is a Spring Boot project.
1. The configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>8</release>
</configuration>
</plugin>
is compilable under JDK-12, but under JDK-8 fails:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project xxxx:
Fatal error compiling: invalid flag: --release
2. The configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
which misses the <release>8</release> parameter is compilable under JDK-8, but under JDK-12 fails:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project xxxx:
Fatal error compiling: CompilerException: NullPointerException
All the possible internet sources I found advice to use the <release>8</release> parameter to be able to avoid the error under JDK-12, but this disables the compilability under JDK-8.
Our source and target compatibility is Java 8, and we need to build the code with the good old plain mvn clean install (without providing a profile!) on the developers' machines with JDK-12, but also on Jenkins, where we still have to keep JDK-8, and we also have some conservative developers :)
first you should define the maven-compiler-plugin within the pluginManagement like this:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<...>
...
and furthermore using profile which are automatically activated which looks like this:
<profiles>
<profile>
<id>jkd-12-compile</id>
<activation>
<jdk>12</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>8</release>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<profile>
<id>jdk-8-compile</id>
<activation>
<jdk>[,8]</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
One not usually a mvn clean install is not needed. You usually only need mvn clean verify...
A tad simpler solution relies on using the properties rather than the explicit configuration entries. It won't complain for anything that it doesn't support.
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<profiles>
<profile>
<id>jdk9+</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
</profile>
</profiles>
Enjoy!

Excluding file with profiles in Eclipse - Maven

I am trying to exclude some files from a Maven war build by using profiles, and just cannot figure out to get that done.
I have created two profiles in pom.xml, created an eclipse run configuration for each, and can invoke them separately. But the resultant builds both have all the files in the eclipse project, no matter what I try.
I have simplified it to A and B, profile A is the 'default' profile.
<profile>
<id>profile-a</id>
<build>
<finalName>builda</finalName>
<plugins>
<plugin>
<artifactId>cloud</artifactId>
<version>11.0.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<classifier>profile-a</classifier>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
And profile B is the one where I want to remove some files, and have tried all sorts of ways, and have reduced it to not including anything, and all the possible exclude methods are shown also.
<profile>
<id>profile-B</id>
<build>
<finalName>buildb</finalName>
<plugins>
<plugin>
<artifactId>cloud</artifactId>
<version>11.0.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<classifier>cloud-service</classifier>
<excludes>
<exclude>**/*.txt</exclude>
</excludes>
<packagingExcludes>**/*.txt</packagingExcludes>
<warSourceExcludes>**/*.txt</warSourceExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
But both builda.war and buildb.war are identical - I compared the war files with Beyond Compare, and the only difference is the timestamp in the Manifest. I also tried removing a .jar file just to see if it was something about .txt files, but no difference at all.
So I'm thinking that the build information is coming from my eclipse project not from pom.xml. Or I completely missing something - I am still a maven newbie, so that is entirely possible.
But how to get this done?
First of all I feel that I should point out that there seem to be multiple problems with the POM you've provided. I think you should take some time to read through the Maven POM Reference, Maven Build Lifecycle, and related documentation.
Based on what you provided I think you're trying to add a classifier to your artifact based on the profile that was active during the build. I preserved this functionality and corrected the other aspects of the POM as best I could, but I made a few assumptions.
Here is the modified POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>web.example</groupId>
<artifactId>exampleWar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>profile-a</id>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<classifier>profile-a</classifier>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-b</id>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<classifier>profile-b</classifier>
<packagingExcludes>%regex[.*\.txt]</packagingExcludes>
<warSourceExcludes>%regex[.*\.txt]</warSourceExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

Maven profile and artifact version

Assume we have maven multimodule project "Foo":
Foo
|-web-module-war
|-dependency-jar
There are two profiles defined for moduleC:
<profile>
<id>poll-some-external-service</id>
<properties>
<dependency-jar.poll.configured>true</dependency-jar.poll.configured>
</properties>
</profile>
<profile>
<id>produce-some-product</id>
<properties>
<dependency-jar.poll.configured>false</dependency-jar.poll.configured>
</properties>
</profile>
Now we run two builds:
mvn clean package -P poll-some-external-service
mvn clean package -P produce-some-product
First build produce following artifacts:
web-module-war-1.0.0-poll.war
dependency-jar-1.0.0-poll.war
Second build produce following artifacts:
web-module-war-1.0.0-produce.war
dependency-jar-1.0.0-produce.war
This means that war file contains web application which works in a different way based on selected profile.
Naming is based on the following configuration in the parent pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<jarName>${project.build.finalName}${foo.build.info}</jarName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>${project.build.finalName}${foo.build.info}</warName>
</configuration>
</plugin>
How can I deploy these artifacts into Nexus? -poll/-produce part is stripped during deployment. This means we have two different applications of the same version but we can deploy only one of them
Thanks
Instead of changing the name use a classifier
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<classifier>poll</classifier>
</configuration>
</plugin>
Your profile for the pom should look similar to the following example. Note that you have to change the dependencies by using the profile too.
<profile>
<id>poll</id>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<classifier>poll</classifier>
</configuration>
</plugin>
</plugins>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>dependency-jar</artifactId>
<classifier>poll</classifier>
</dependency>
</dependencies>
</build>
</profile>

Error assembling EJB: META-INF/ejb-jar.xml is required for ejbVersion 2.x

Is there a way to build with maven an EJB project that doesn't contain deployment descriptor?
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.0</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
You need to tell Maven to use EJB 3.0.

Categories