如何修复由 maven 3.3.9 的构建顺序引起的意外清理阶段并使用 ${revision}



我有一个多模块的maven项目,它是一个springboot项目。

由于我们公司的jenkins脚本,我需要将可运行模块(带有spring主类)复制到根pom菜单,我使用Maven CI友好版本的占位符${revision}来简化版本,但它不起作用,因为构建命令在复制后删除了Maven 3.3.9中的.jar文件,当Maven版本为3.8.2时它可以工作。

我写了一个演示项目来重现这个问题,根节点叫做demo-root, web模块叫做demo-web。

这是我的根歌

<?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',所以拷贝是在干净的删除之后,我们可以得到。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] ------------------------------------------------------------------------

所以,问题发生在3个先决条件:1、使用maven-dependency-plugin将模块的.jar文件拷贝到根目录下。2、使用${revision}占位符来标记版本。3、在maven 3.3.9中执行mvn clean package命令

当我无法更改maven版本时,我该如何解决这个问题(因为3.3.9是公司版本,更改它会产生很多影响)。并且仍然希望将.jar复制到根目录(因为jenkins脚本是公司规范)。并且保持${revision}对多版本控制的好处。

如果您确实不能更改Maven版本,我将不使用${revision},而是直接添加版本。

你可以通过调用mvn versions:set -DnewVersion=1.2.3-SNAPSHOT

来更新项目中的所有版本

最新更新