繁体   English   中英

如何在没有为依赖项生成可用 pom.xml 的父 pom 的情况下为我的 jar 外部设置版本

[英]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

如果您找到其他方法来解决此问题,请与我们其他人分享:)

  1. 升级 Maven >= 3.5.0
  2. 将目标<phase>process-resources</phase>更改为<phase>package</phase>

它将解决问题。

另请参阅: Maven CI 友好版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM