在没有父pom的情况下,如何在pom.xml中外部定义jar项目的版本标签?
我已经使用了properties-maven-plugin和flatten-maven-plugin来实现真正的亲近。最终部署的pom.xml有一个不可用的版本,因为属性-maven-plugin使用值替换来设置版本。flatten-maven-plugin解决了依赖项版本的问题,但似乎不能解决主要工件的版本。代码似乎可以正确地构建和命名jar。
我查看了https://maven.apache.org/maven-ci-friendly.html并了解到${revision},但那里的示例似乎包括父pom。
我希望有人能在不使用父pom的情况下找到解决方案。如果没有解决方案,我会认为这是一个插件的功能请求,但在提交这样的请求之前,我想先在这里尝试一下。
这是我能提供的最简单的配置来演示我的问题。
pom.xml
<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>testgroup</groupId>
<artifactId>test</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lang>3.9</lang>
<revision>${major}.${minor}</revision>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${lang}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>pre</id>
<phase>pre-clean</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>test.properties</file>
</files>
</configuration>
</execution>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>test.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<flattenMode>defaults</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>test.properties
major=1
minor=2src/main/java/testgroup/test/App.java
package testgroup.test;
import org.apache.commons.lang3.StringUtils;
public class App
{
public static void main( String[] args )
{
System.out.println(StringUtils.chop("boo"));
}
}生成的.fltened-pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>testgroup</groupId>
<artifactId>test</artifactId>
<version>${major}.${minor}</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>我希望.fltened-pom.xml中的版本是1.2,而不是${major}.${minor}
所以,我不确定这是不是我做错了什么,或者是某个插件中的某种bug。例如,甚至连Maven都奇怪地显示了以下内容:
C:\Users\Robert\eclipse-workspace\test>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< testgroup:test >---------------------------
[INFO] Building test ${major}.${minor}
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (pre) @ test ---< snip >
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test ---
[INFO] Building jar: C:\Users\Robert\eclipse-workspace\test\target\test-1.2.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------发布于 2020-02-15 19:17:26
我不知道您是否找到了解决此问题的方法,但在我看来,您正确地将$revision属性设置为version元素。
但是你会运行测试吗?为了构建/测试应用程序,您需要像这样运行它:
mvn clean flatten:flatten test或
mvn clean flatten:flatten package我发现有一点不对劲的是,您正在使用$major和$minor,而没有将它们作为属性在pom.xml中初始化,也没有使用属性。
我知道您希望使用属性文件,我假设您以某种方式将其注入到您的测试环境中。相反,您可以使用属性<major>和<minor>来初始化它们,当您想要更改它们时,可以使用
mvn clean -Dmajor=1 -Dminor=3 flatten:flatten test如果你找到了解决这个问题的其他方法,请与我们分享:)
发布于 2020-11-16 17:01:44
<phase>process-resources</phase> to maven它会解决这个问题。
https://stackoverflow.com/questions/56160448
复制相似问题