【问题标题】:Maven install-plugin does not use versions-plugin updated versionmaven install-plugin 不使用versions-plugin更新版本
【发布时间】:2023-04-09 03:36:02
【问题描述】:

我使用versions-maven-plugin 来更新项目版本。我想使用此更新版本安装工件,但 maven-install 插件使用旧版本。我必须如何配置项目以实现所描述的行为?

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>parse-version</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <propertyPrefix>parsedVersion</propertyPrefix>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>update-version</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>set</goal>
                        <goal>commit</goal>
                    </goals>
                    <configuration>
                        <newVersion>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}-${SVN_REVISION}</newVersion>
                    </configuration>
                </execution>
            </executions>
        </plugin>

通常我希望关于 maven 生命周期,安装插件应该采用新版本,因为版本插件是在验证阶段执行的。

【问题讨论】:

  • versions-maven-plugin:set 目标仅用于从命令行运行,而不是在生命周期内......这永远不会起作用,因为 versions-maven-plugin:set 目标会改变pom.xml 文件,但在运行期间 pom.xml 已经加载到内存中,因此您将始终安装旧版本。你使用哪个 Maven 版本?
  • 您好,谢谢您的回答,我使用的是 Maven 3.3.x。从命令行使用版本插件后,它起作用了!也许有人有相同的用例: build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}-${SVN_REVISION}

标签: maven maven-install-plugin


【解决方案1】:

Maven 3.2.1 there is a possibility to use properties as versions 开头,这意味着您可以在 pom 文件中提供类似的内容:

<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>

  <parent>
    <groupId>com.soebes.smpp</groupId>
    <artifactId>smpp</artifactId>
    <version>2.2.1</version>
  </parent>

  <groupId>com.soebes.examples.j2ee</groupId>
  <artifactId>parent</artifactId>
  <version>${revision}</version>
  <packaging>pom</packaging>

此外,您当然也可以在这样的多模块构建的父元素中使用它:

<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>

  <parent>
    <groupId>com.soebes.examples.j2ee</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>
  </parent>

  <artifactId>service</artifactId>
  <packaging>ejb</packaging>

这意味着您可以通过以下方式运行 Maven:

mvn -Drevision=1.2.3-SNAPSHOT install

或者简单地发布:

mvn -Drevision=1.2.3 install

这里的缺点是您需要始终在命令行或 CI 作业中进行修订。除此之外,还可以使用其他东西,例如${changelist}${sha1},当然可以组合使用它们。所以你可以采纳这样的建议:

<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>

  <parent>
    <groupId>com.soebes.smpp</groupId>
    <artifactId>smpp</artifactId>
    <version>2.2.1</version>
  </parent>

  <groupId>com.soebes.examples.j2ee</groupId>
  <artifactId>parent</artifactId>
  <version>${revision}-${sha1}</version>
  <packaging>pom</packaging>

所以你可以通过这个调用maven:

mvn -Drevision=1.2.3 -Dsha1=SVNRevision-SNAPSHOT clean package

【讨论】:

  • 谢谢,但是对于我的用例,构建助手插件就足够了,它读取当前版本并附加 svn 修订号,我不想在命令行上给出版本。
最近更新 更多