[英]How do I externally set a version for my jar without a parent pom that produces a usable pom.xml for dependencies
如何在沒有父 pom 的情況下為 jar 項目在 pom.xml 中外部定義版本標記?
我使用了 properties-maven-plugin 和 flatten-maven-plugin 來真正接近。 由於 properties-maven-plugin 使用值替換來設置版本,因此部署的結果 pom.xml 具有不可用的版本。 flatten-maven-plugin 解決了依賴版本的問題,但似乎沒有解決主要工件的版本。 該代碼似乎可以正確構建和命名 jar。
我查看了https://maven.apache.org/maven-ci-friendly.html並了解了 ${revision},但那里的示例似乎包括父 poms。
我希望有人在不使用父 pom.xml 的情況下找到解決方案。 如果沒有解決方案,我想將此視為對其中一個插件的功能請求,但我想在提交此類請求之前先在這里嘗試。
這是我可以提供的最簡單的配置來演示我的問題。
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>
測試屬性
major=1
minor=2
src/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"));
}
}
結果 .flattened-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>
我希望 .flattened-pom.xml 中的版本是 1.2,而不是 ${major}.${minor}
所以,我不確定這是我做錯了什么,還是某個插件中的某種錯誤。 例如,即使 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 ---
<剪>
[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] ------------------------------------------------------------------------
我不知道您是否設法找到解決此問題的方法,但據我所知,您正確地將$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
如果您找到其他方法來解決此問題,請與我們其他人分享:)
<phase>process-resources</phase>
更改為<phase>package</phase>
它將解決問題。
另請參閱: Maven CI 友好版本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.