Maven发布:批处理模式下的下一个开发版本

时间:2013-03-18 14:55:48

标签: maven maven-release-plugin

我已配置Jenkins作业以自动释放我的maven项目。这可以通过以下方式完成:mvn --batch-mode clean release:prepare release:perform 在批处理模式下,将自动确定发行版本和开发版本。这正是我想要的。

问题是我想增加第二个版本而不是第三个版本。因此,当我发布1.2.0版本时,下一个开发版本必须是1.3.0-SNAPSHOT。不是1.2.1-SNAPSHOT。 添加命令行参数不是一个选项,因为这会强制我不断编辑构建作业。

有关如何更改用于确定下一个开发版本的算法的任何建议吗?

7 个答案:

答案 0 :(得分:4)

我知道这是一篇有点旧的帖子,但我没有找到一个我真正喜欢在线的答案,而且我能够想出一些可能对其他人有用的东西......

我想在OP状态下增加minorVersion,我可以通过在我的项目POM中使用构建帮助程序插件(解析版本)和发布插件的组合来实现。注意"初始化"在POM和maven run属性中引用的阶段......

这里是POM的摘录,我们使用构建帮助器插件来解析我们可以在发布插件中引用的版本...

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${maven.build.helper.plugin.version}</version>
                <executions>
                    <execution>
                        <id>parse-versions-for-release</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>parse-version</goal>
                        </goals>
                        <configuration>
                            <propertyPrefix>parsedVersion</propertyPrefix>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>${maven.release.plugin.version}</version>
                <configuration>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                    <tagNameFormat>@{project.artifactId}-@{project.version}</tagNameFormat>
                    <useReleaseProfile>false</useReleaseProfile>
                    <developmentVersion>${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0-SNAPSHOT</developmentVersion>
                </configuration>
            </plugin>

现在我们可以运行一个非常正常的版本,但添加&#34;初始化&#34;阶段触发版本解析(并确保在查找解析版本之前发生)...

mvn initialize release:clean release:prepare release:perform

答案 1 :(得分:0)

正如Khmarbaise建议的那样,我也认为没有解决问题的方法。

是否有任何规则可以自动告诉您是否必须更改第二个或第三个数字?的确,我不这么认为。这么说,你不能要求Maven / Jenkins为你选择它,一次是主要版本数字,另一个是小版本数字。

您必须通过参数更改它,或者让用户通过Jenkins M2 Release插件对其进行配置,如willome所建议的那样。 只能是手动操作。

答案 2 :(得分:0)

如果在Jenkins中使用参数化构建,则可以使用命令行参数而不编辑作业。检查作业配置页面中的“此构建参数化”选项。

这不会让Jenkins完全独立完成发布(这很好;我们不希望机器人接受我们的工作!) - 当你从Jenkins手动启动构建时,你将成为能够设置您配置的任何参数。

答案 3 :(得分:0)

您可以使用自定义groovy脚本自动为releaseVersion和developmentVersion提供maven-release-plugin。然后maven命令会看起来像:

mvn clean release:clean release:prepare release:perform -DreleaseVersion = $ {releaseVersion} -DdevelopmentVersion = $ {developmentVersion}

按照this answer中的步骤更改groovy脚本的部分以适应您的用例(例如此部分):

def newFixVersion = 0;
if (hasSnapshotPart) {  
    newMinorRelVersion = minorVersion;  
    newMinorDevVersion = minorVersion + 1;  
} else {  
    //TODO: either throw an exception here or change the newMinorRelVersion newMinorDevVersion appropriately to suite your use-cases: 
        //throw new IllegalArgumentException("The pom at location " + POM_LOCATION + " contains the version " + projectVersion + " which is not a snapshot version (missing " + SNAPSHOT_PART + "). This is a released version and nothing should happen to it!");  
}  

答案 4 :(得分:0)

我遇到了同样的问题,我希望在不运行多个命令或手动插入版本的情况下解决它。

这是我对y(或次要)增量的解决方案:

我在初始化阶段运行Groovy脚本。此脚本创建 release.properties 。将其添加到 pom.xml 中的 project / build / plugins 部分:

        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.5</version>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                    <version>2.4.6</version>
                </dependency>
            </dependencies>
            <executions>
                <!-- Force maven-release-plugin to increase MINOR, not PATCH, and create tag as vX.Y.Z -->
                <execution>
                    <id>release-parameters</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <scripts>
                            <script>
                                <![CDATA[
                                    final String SNAPSHOT = '-SNAPSHOT'

                                    Properties releaseProps = new Properties()
                                    File releasePropsFile = new File('release.properties')
                                    String releaseVersion = '${project.version}'.replace('-SNAPSHOT', '')
                                    String[] vNumbers = releaseVersion.split('\\.')
                                    String snapshotVersion = vNumbers[0] + '.' + (Integer.parseInt(vNumbers[1]) + 1) + '.' + '0' + SNAPSHOT

                                    releaseProps.setProperty('scm.tag', 'v' + releaseVersion)
                                    releaseProps.setProperty('project.rel.${project.groupId}:${project.artifactId}', releaseVersion)
                                    releaseProps.setProperty('project.dev.${project.groupId}:${project.artifactId}', snapshotVersion)
                                    releaseProps.store(releasePropsFile.newWriter(), null)
                                ]]>
                            </script>
                        </scripts>
                    </configuration>
                </execution>
            </executions>
        </plugin>

此脚本还会更改SCM中 vX.Y.Z 标记名称。 初始化阶段未在发布:准备阶段执行。要解决此问题,您可以运行&#34; mvn install&#34;在发布之前,或将发布命令更改为:

mvn --batch-mode initialize clean release:prepare release:perform

关于release.properties:https://maven.apache.org/maven-release/maven-release-plugin/examples/non-interactive-release.html

答案 5 :(得分:0)

您可以使用build-helper-maven-plugin。只需将以下内容添加到pom.xml:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>${maven.build.helper.plugin.version}</version>
    </plugin>

并将命令更改为

mvn --batch-mode clean build-helper:parse-version release:prepare release:perform -DdevelopmentVersion=${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0-SNAPSHOT

(请注意,根据运行此命令的环境,您可能需要使用$\\$进行转义)

答案 6 :(得分:0)

projectVersionPolicyId mojo中提供了一个参数release:preparehttp://maven.apache.org/maven-release/maven-release-plugin/prepare-mojo.html#projectVersionPolicyId

可能没有内置的版本策略可以满足您的需求,但是您可以通过实现接口VersionPolicy来开发自己的版本策略。您可以看到maven-release-yearly-policy作为参考,它在版本号中使用年份来提供版本策略。

相关问题