How to avoid maven profile duplication - maven

I have a maven project for a webapp, which repack war dependency using overlays. For two profiles, test and prod, it should exclude demo.jsp file, but for other, for example local, this file should stay. Is there a way to have only one configuration for two profiles? I don't want to repeat one configuration for two profiles.
My current solution:
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<overlays>
<overlay>
<groupId>mygroup</groupId>
<artifactId>mywebapp</artifactId>
<excludes>
<exclude>demo.jsp</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<overlays>
<overlay>
<groupId>mygroup</groupId>
<artifactId>mywebapp</artifactId>
<excludes>
<exclude>demo.jsp</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
EDIT: test and prod profiles are identical

The Plugin Management may help us as the following :-
<build>
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<overlays>
<overlay>
<groupId>mygroup</groupId>
<artifactId>mywebapp</artifactId>
<excludes>
<exclude>demo.jsp</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</pluginManagement>
</build>
Then the profiles should be as the following: -
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
...
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
...
</profile>
</profiles>
I hope this may help.

Related

Maven execute module pom using profile from parent pom

I want to build docker images using profile in parent pom, which looks like this:
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.39.1</version>
<configuration>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>image-build</id>
<modules>
<module>hot.build</module>
</modules>
</profile>
</profiles>
...
My hot.build module`s pom looks like this
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.39.1</version>
<executions>
<execution>
<phase>install</phase>
<id>build-server-image</id>
<configuration>
<images>
<image>
<name>...</name>
<build>
...
</build>
</image>
</images>
</configuration>
</execution>
<execution>
<phase>install</phase>
<id>build-emailService-image</id>
<configuration>
<images>
<image>
<name>...</name>
<build>
...
</build>
</image>
</images>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Running mvn clean install -Pimage-build executes hot.build with success, but it is not actually doing the build phase.
What am I missing here?

Springboot remove class only when deploy

I have one springboot project but I wanna to deploy in my nexus to use with component in another project, so I try to remove some classes just like this:
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>br/com/lumera/balcaoonline/api/BalcaoonlineApiApplication.class</exclude>
<exclude>br/com/lumera/balcaoonline/api/central/controller/rtdpj/*.class</exclude>
<exclude>**/application-*.yml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
I need to remove the controllers and the main class, but now when I try to run the project the springboot dont find the main class
how can I fix this?
tks
You can use profiles to create one jar for nexus and another not for nexus.
Maven command:
mvn install -DwithNexus=true
Example:
<profiles>
<profile>
<id>nexus</id>
<activation>
<property>
<name>withNexus</name>
</property>
</activation>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>br/com/lumera/balcaoonline/api/BalcaoonlineApiApplication.class</exclude>
<exclude>br/com/lumera/balcaoonline/api/central/controller/rtdpj/*.class</exclude>
<exclude>**/application-*.yml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>no-nexus</id>
<activation>
<property>
<name>withNexus</name>
<value>!true</value>
</property>
</activation>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>

Is it possible to use conditions in pom.xml

I've a maven web project and I'm using profiles in pom.xml for creating .war files for different environments. See the sample below snippet from my pom.xml
...
<profiles>
<!-- DEVELOPMENT PROFILE -->
<profile>
<id>development</id>
...
<build>
...
<plugin>
<groupId>com.google.code.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
...
<configuration>
<message>Message 1</message>
</configuration>
...
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web-development.xml</webXml>
</configuration>
</plugin>
...
</build>
</profile>
<!-- PRODUCTION PROFILE -->
<profile>
<id>production</id>
...
<build>
...
<plugin>
<groupId>com.google.code.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
...
<configuration>
<message>Message 2</message>
</configuration>
...
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web-production.xml</webXml>
</configuration>
</plugin>
</build>
</profile>
</profiles>
However, this makes my pom.xml pretty large - I've 5 different profiles. All I wanted to do is to display a custom message and use a custom web.xml file based on the profile.
Is there a way I could add conditions in the pom.xml so that I can simplify it some like below:
...
<profiles>
<profile>
<id>development</id>
...
</profile>
<profile>
<id>production</id>
...
</profile>
<profiles>
<build>
...
<plugin>
<groupId>com.google.code.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
<version>1.0.0</version>
<inherited>false</inherited>
<configuration>
<IF CONDITION TO CHECK IF development PROFILE>
<message>Message 1</message>
</IF CONDITION>
<ELSE CASE>
<message>Message 2</message>
<ELSE CASE>
</configuration>
<executions>
<execution>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<IF CONDITION TO CHECK IF development PROFILE>
<webXml>src/main/webapp/WEB-INF/web-development.xml</webXml>
</IF CONDITION>
<ELSE CASE>
<webXml>src/main/webapp/WEB-INF/web-production.xml</webXml>
<ELSE CASE>
</configuration>
</plugin>
...
</build>
No.
But you could experiment with defining properties in profiles, which might allow you to write things more concisely.

Method filter for Surefire

From this page:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
under "Fully qualified class name" section, It seems possible to specify which test methods to run in pom.xml. However, it is not very clear where to write this tag called "test". Anyone could shed some lights?
Update:
You can define it inside <properties>, as in:
<project>
<properties>
<test>TestCircle#testSlow</test>
</properties>
</project>
Also, you can use includes and excludes as hinted here, e.g.:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>Sample.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
and
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>

Wrong charset encoding using openshift

I'm trying to deploy my web app with service openshift. I set charset encoding on very jsp like that: <%# page contentType="text/html;charset=UTF-8" language="java"%>
in controllers i have: produces = "text/html;charset=UTF-8"
and my pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>TrunkSchedule</finalName>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!--some dependencies-->
</dependencies>
<profiles>
<profile>
<id>openshift</id>
<build>
<finalName>TrunkSchedule</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<outputDirectory>deployments</outputDirectory>
<warName>ROOT</warName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
locally everything works fine, but when deploying the encoding is broken.
may be the problem in openshift?

Resources