maven中实现packagingExcludes后出现问题
我有使用弹簧配置文件构建不同配置文件的应用程序。我必须实现打包排除以排除 websocket-*.jar,因为它在部署后与 tomcat 发生冲突。 所以,我添加一些代码来实现打包排除,请参阅此站点
<configuration>
<packagingExcludes>WEB-INF/lib/websocket-*.jar</packagingExcludes>
</configuration>
- maven版本:apache-maven-3.6.3
- maven-war-plugin:3.3.1
- spring-boot-maven-plugin : 2.5.3
在终端中运行 mvn package -P test
后,我在目标文件夹中有很多文件
包括war文件和原始文件。
检查这些文件后,我意识到两件事:
- 在war文件中,websocket jar仍然存在并且尚未删除
- 在原始文件中,websocket jar无法找到/删除,这意味着打包排除成功。但问题org文件夹也无法在这些文件中找到,并导致该文件无法在本地或Web服务器中运行。 原始文件
我的目标是:制作war文件/不是原始文件,里面没有websocket jar。
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正试图变得比 Spring Boot 已经提供的更聪明。
maven-war-plugin
。scope
设置为provided
spring-boot-maven-plugin
中删除version
(您是混合版本,2.2.4 和 2.5.3,不要混合版本spring-boot-starter-web
的一部分,这将使您的
pom.xml
生效。代码> 看起来像下面这样甚至可以通过删除
log4j2
依赖项并使用spring-boot-starter-log4j2
并包含在那么您将在生产中运行未经测试的工件,然后重命名您的环境特定属性。文件至
application-.properties
Spring Boot 将自动加载它,您可以放弃文件的复制(并生成一个可以通过您的环境进行推广的工件。dev.properties
->application-dev.properties
test.properties
->应用程序测试.properties
uat.properties
->application-uat.properties
生产.properties
->application.properties
。现在,当在环境中运行应用程序时,请确保在环境中设置 SPRING_PROFILES_ACTIVE 属性或将其设置为 Tomcat 上下文变量。 (对于生产来说不需要)。
这样做时,您最终会得到一个非常干净且简单的“pom.xml”。
You are trying to be smarter then what is already provided by Spring Boot.
maven-war-plugin
.scope
toprovided
for the tomcat dependencyversion
from 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.xml
look something like the followingYou can even make it better by removing the
log4j2
dependencies and instead use thespring-boot-starter-log4j2
and 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>.properties
Spring 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.properties
test.properties
->application-test.properties
uat.properties
->application-uat.properties
production.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.