Remove -SNAPSHOT from project version in pom

夙愿已清 提交于 2019-11-29 11:33:09

问题


I have a pom with the following GAV

<groupId>com.company.services</groupId>
<artifactId>test-branch-2</artifactId>
<version>1.0.21-SNAPSHOT</version>

I want to remove -SNAPSHOT from this using maven in batch mode, so I can do it with Jenkins and not have to specify anything manually.

I've looked at the documentation for version:set but all the options offer me an interactive prompt and ask me to type a name for the version.

I would prefer the versions plugin, not the release plugin.


回答1:


Since version 2.10 of the Versions Maven Plugin you can simply do:

mvn versions:set -DremoveSnapshot



回答2:


If you really don't want to use the Maven Release Plugin (for whatever reason), here is how I succeed on dropping the SNAPSHOT suffix (hanbdled as a classifier) from a maven POM in a standard way (that is, no scripting, no custom maven plugin).

Given the following profile:

<profile>
    <id>drop-snapshot</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.11</version>
                <executions>
                    <execution>
                        <id>parse-version</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>parse-version</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>versions-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>set-version</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>set</goal>
                        </goals>
                        <configuration>
                            <newVersion>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}</newVersion>
                        </configuration>
                    </execution>
                    <execution>
                        <id>upgrade-pom</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>commit</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

And simply executing: mvn validate -Pdrop-snapshot
The version of an example pom passed from 0.0.1-SNAPSHOT to 0.0.1.

How it actually works:

  • The build-helper-maven-plugin, parse-version goal, will parse the current version of the POM and set it in a set of properties having by default parsedVersion as a prefix and majorVersion, minorVersion, incrementalVersion as suffixes (check the documentation, you will also have classifier and buildNumber). Hence, after its execution we can then use in our POM the properties like ${parsedVersion.majorVersion} and so on.
  • The versions-maven-plugin, set goal, will then use these properties to build the new version you actually want (in this case dropping the SNAPSHOT, because we excluded the ${parsedVersion.classifier} property).
  • Lastly, the versions-maven-plugin, commit goal, will make these changes effective.



回答3:


Similar to A_Di-Matteo's approach with build-helper, but without the need for additional plugins configuration:

 mvn build-helper:parse-version versions:set \
     -DnewVersion=\${parsedVersion.majorVersion} \
         .\${parsedVersion.minorVersion} \
         .\${parsedVersion.incrementalVersion \
         .\${parsedVersion.buildNumber} \
      versions:commit

This will replace your 1.0.0.0-SNAPSHOT with 1.0.0.0 in the pom.xml.




回答4:


Add the following to your POM:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.11</version>
            <configuration>
                <name>newVersion</name>
                <value>${project.version}</value>
                <regex>-SNAPSHOT</regex>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
    </plugins>
</build>

You can now remove the -SNAPSHOT part of your project's version with:

mvn build-helper:regex-property versions:set -N

The -N tells Maven to only proces the root project in case you have modules defined in your POM. This is not strictly necessary but prevents the build-helper plugin from running unnecessarily against the submodules. The versions plugin runs only on the root project in any case, and traverses all modules automatically. Consider using the reactorModuleConvergence rule of the maven-enforcer plugin to make sure multi-module projects are handled correctly.

You can run mvn versions:commit to remove the backup POM(s) generated by versions:set. Alternatively you can add <generateBackupPoms>false</generateBackupPoms> to the configuration of the versions plugin.



来源:https://stackoverflow.com/questions/23501119/remove-snapshot-from-project-version-in-pom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!