java - 如何修复由 maven 3.3.9 的构建顺序引起的意外清理阶段并使用 ${revision}
问题描述
我有一个多模块的 maven 项目,它是一个 springboot 项目。
由于我们公司的jenkins脚本,我需要将可运行模块(带有spring主类)复制到根pom菜单,并使用Maven CI Friendly Versions的占位符${revision}来简化版本,但它不起作用,因为build order 复制后删除了maven 3.3.9中的.jar文件,在maven版本为3.8.2时有效。
我写了一个demo项目复现问题,根pom叫demo-root,web模块叫demo-web。
这是我的根 pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo-root</artifactId>
<version>${revision}</version>
<modules>
<module>demo-web</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<revision>1.0-SNAPSHOT</revision>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>demo-web</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
</configuration>
<executions>
<!-- enable flattening -->
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<!-- ensure proper cleanup -->
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这是我的模块pom:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>demo-root</artifactId>
<groupId>org.example</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-web</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>demo-project</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.example</groupId>
<artifactId>demo-web</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<destFileName>demo-project.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>../target</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后,我mvn clean package
在根目录下运行(使用 .pom 文件)。
这是构建日志,我们可以看到构建顺序是'demo-web' -> 'demo-root'。在日志下,我们可以看到构建命令在复制后进行清理,删除了 .jar 文件。
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] demo-web
[INFO] demo-root
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo-web 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo-web ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/demo-web/target
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:clean (flatten.clean) @ demo-web ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/demo-web/.flattened-pom.xml
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:flatten (flatten) @ demo-web ---
[INFO] Generating flattened POM of project org.example:demo-web:jar:1.0-SNAPSHOT...
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/admin/repo@jin/demo-root/demo-web/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/admin/repo@jin/demo-root/demo-web/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo-web ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo-web ---
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ demo-web ---
[INFO] Building jar: /Users/admin/repo@jin/demo-root/demo-web/target/demo-project.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.5.6:repackage (repackage) @ demo-web ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:copy (copy) @ demo-web ---
[INFO] Configured Artifact: org.example:demo-web:?:jar
[INFO] Copying demo-project.jar to /Users/admin/repo@jin/demo-root/target/demo-project.jar
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo-root 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo-root ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/target
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:clean (flatten.clean) @ demo-root ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/.flattened-pom.xml
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:flatten (flatten) @ demo-root ---
[INFO] Generating flattened POM of project org.example:demo-root:pom:1.0-SNAPSHOT...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] demo-web ........................................... SUCCESS [ 4.592 s]
[INFO] demo-root .......................................... SUCCESS [ 0.095 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.940 s
[INFO] Finished at: 2021-10-26T00:44:10+08:00
[INFO] Final Memory: 34M/120M
[INFO] ------------------------------------------------------------------------
如果我将 maven 版本更改为 3.8.2,然后运行mvn clean package
. 它运作良好。
这里是构建日志,在日志下我们可以看到构建顺序是'demo-root' -> 'demo-web',所以copy是在clean delete之后,我们可以得到.jar文件。
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] demo-root [pom]
[INFO] demo-web [jar]
[INFO]
[INFO] -----------------------< org.example:demo-root >------------------------
[INFO] Building demo-root 1.0-SNAPSHOT [1/2]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo-root ---
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:clean (flatten.clean) @ demo-root ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/.flattened-pom.xml
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:flatten (flatten) @ demo-root ---
[INFO] Generating flattened POM of project org.example:demo-root:pom:1.0-SNAPSHOT...
[INFO]
[INFO] ------------------------< org.example:demo-web >------------------------
[INFO] Building demo-web 1.0-SNAPSHOT [2/2]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo-web ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/demo-web/target
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:clean (flatten.clean) @ demo-web ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/demo-web/.flattened-pom.xml
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:flatten (flatten) @ demo-web ---
[INFO] Generating flattened POM of project org.example:demo-web:jar:1.0-SNAPSHOT...
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/admin/repo@jin/demo-root/demo-web/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/admin/repo@jin/demo-root/demo-web/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo-web ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo-web ---
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ demo-web ---
[INFO] Building jar: /Users/admin/repo@jin/demo-root/demo-web/target/demo-project.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.5.6:repackage (repackage) @ demo-web ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:copy (copy) @ demo-web ---
[INFO] Configured Artifact: org.example:demo-web:?:jar
[INFO] Copying demo-project.jar to /Users/admin/repo@jin/demo-root/target/demo-project.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for demo-root 1.0-SNAPSHOT:
[INFO]
[INFO] demo-root .......................................... SUCCESS [ 1.366 s]
[INFO] demo-web ........................................... SUCCESS [ 3.800 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.653 s
[INFO] Finished at: 2021-10-26T00:48:13+08:00
[INFO] ------------------------------------------------------------------------
如果我将 ${revision} 替换为“1.0-SNAPSHOT”,它运行良好,并且我可以在根目录的目标目录下获得一个 .jar 文件。
这是构建日志:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] demo-root
[INFO] demo-web
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo-root 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo-root ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/target
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:clean (flatten.clean) @ demo-root ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/.flattened-pom.xml
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:flatten (flatten) @ demo-root ---
[INFO] Generating flattened POM of project org.example:demo-root:pom:1.0-SNAPSHOT...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo-web 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo-web ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/demo-web/target
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:clean (flatten.clean) @ demo-web ---
[INFO] Deleting /Users/admin/repo@jin/demo-root/demo-web/.flattened-pom.xml
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- flatten-maven-plugin:1.2.2:flatten (flatten) @ demo-web ---
[INFO] Generating flattened POM of project org.example:demo-web:jar:1.0-SNAPSHOT...
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/admin/repo@jin/demo-root/demo-web/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/admin/repo@jin/demo-root/demo-web/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo-web ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo-web ---
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ demo-web ---
[INFO] Building jar: /Users/admin/repo@jin/demo-root/demo-web/target/demo-project.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.5.6:repackage (repackage) @ demo-web ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:copy (copy) @ demo-web ---
[INFO] Configured Artifact: org.example:demo-web:?:jar
[INFO] Copying demo-project.jar to /Users/admin/repo@jin/demo-root/target/demo-project.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] demo-root .......................................... SUCCESS [ 1.110 s]
[INFO] demo-web ........................................... SUCCESS [ 3.545 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.562 s
[INFO] Finished at: 2021-10-26T00:51:36+08:00
[INFO] Final Memory: 29M/104M
[INFO] ------------------------------------------------------------------------
所以,问题出现在三个前提条件下: 1、使用 maven-dependency-plugin 将模块 .jar 文件复制到根目录。2、对版本标签使用${revision}占位符。3、mvn clean package
在maven 3.3.9中运行命令
当我无法更改 maven 版本时如何解决此问题(因为 3.3.9 是公司版本,更改它会产生很大的影响)。并且仍然想将 .jar 复制到根目录(因为 jenkins 脚本是公司规范)。并保持多版本控制的 ${revision} 好处。
解决方案
如果您真的无法更改 Maven 版本,我不会使用${revision}
而是直接添加版本。
然后,您可以通过调用更新项目中的所有版本mvn versions:set -DnewVersion=1.2.3-SNAPSHOT
推荐阅读
- git - Git 不会将我的项目推送到我的远程 git 仓库
- html - 设置 className 并使用 styledComponents 创建一个动作
- kubernetes - 如何查找 k8s 命名空间删除历史记录
- function - 包含列表和范围的函数:
- xamarin - Xamarin Realm 退出时没有错误但没有结果
- python - seaborn 热图上的次 x 轴
- mysql - 如何编写 SQL 查询来拆分文本并添加计数
- terraform - 如何将地图列表转换为集合?
- java - 在 Spring Boot 中配置数据库
- angular - 无法读取 Angular 中未定义的属性“indexOf”