I have app that build with different profiles using spring profiles. i have to implement packagingExcludes to exclude websocket-*.jar because its make conflict with tomcat after deploy. so , i add some code to implement packagingExcludes , refer to this site documentation
<configuration>
<packagingExcludes>WEB-INF/lib/websocket-*.jar</packagingExcludes>
</configuration>
- maven version : apache-maven-3.6.3
- maven-war-plugin : 3.3.1
- spring-boot-maven-plugin : 2.5.3
After run mvn package -P test in terminal , i have lot of files in target folder
including war file and original file.
After check these file, i realized 2 things :
- In war file, websocket jar still there and not deleted yet
- In original file, websocket jar can't found/deleted , which mean packagingExcludes success. But the problem org folder also cannot found in these file, and cause this file can't run in local or web server. original file
My goal is : Make war file / not original file without websocket jar inside.
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.microservices</groupId>
<artifactId>sc-stream-mep</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>stream-mep</name>
<description>Services stream data to mep mrp</description>
<repositories>
<repository>
<id>confluent</id>
<url>https://packages.confluent.io/maven/</url>
</repository>
</repositories>
<properties>
<java.version>1.8</java.version>
<jfairy.version>0.5.9</jfairy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope>-->
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>io.codearte.jfairy</groupId>
<artifactId>jfairy</artifactId>
<version>${jfairy.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.confluent/timestamp-interceptor -->
<dependency>
<groupId>io.confluent</groupId>
<artifactId>timestamp-interceptor</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- fixing CVE-2021-45105 issue , happen in log4j 2.14 version -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.32</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>stream-mep</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<packagingExcludes>WEB-INF/lib/*websocket*.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/websocket-*.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete
file="${project.build.outputDirectory}/application.properties" />
<copy file="src/main/resources/dev.properties"
tofile="${project.build.outputDirectory}/application.properties" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/websocket-*.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete
file="${project.build.outputDirectory}/application.properties" />
<copy file="src/main/resources/test.properties"
tofile="${project.build.outputDirectory}/application.properties" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>uat</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<packagingExcludes>WEB-INF/lib/websocket-*.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete
file="${project.build.outputDirectory}/application.properties" />
<copy file="src/main/resources/uat.properties"
tofile="${project.build.outputDirectory}/application.properties" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>production</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/websocket-*.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete
file="${project.build.outputDirectory}/application.properties" />
<copy file="src/main/resources/production.properties"
tofile="${project.build.outputDirectory}/application.properties" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
You are trying to be smarter then what is already provided by Spring Boot.
maven-war-plugin.scopetoprovidedfor the tomcat dependencyversionfrom thespring-boot-maven-plugin(you are mixing versions, 2.2.4 and 2.5.3, don't mix versionsspring-boot-starter-web.Which would make your
pom.xmllook something like the followingYou can even make it better by removing the
log4j2dependencies and instead use thespring-boot-starter-log4j2and include a version to use in your<properties/>section.Finally you are also building artifacts per environment, which basically means you will run untested artifacts in production. Spring can perfectly load the correct configuration if you specify which profile is active. Then by renamed your environment specific properties file to
application-<environment>.propertiesSpring Boot will automatically load it and you could ditch the copying around of files (and produce a single artifact which you can promote throuh your environment.dev.properties->application-dev.propertiestest.properties->application-test.propertiesuat.properties->application-uat.propertiesproduction.properties->application.properties.Now when running the application on the environment make sure that a SPRING_PROFILES_ACTIVE property is set in the environment or as Tomcat context variable. (For production it isn't needed).
When doing so you end up with a really clean and easy `pom.xml.