34

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.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
Jepper
  • 1,092
  • 3
  • 11
  • 24
  • 2
    `mvn versions:set -DnewVersion=1.0.21` should do the trick, doesn't it? – Behe May 06 '14 at 17:53
  • Yes, but no. How did you arrive at the string "1.0.21" without having less'ed or cat'ed or grep'ed the pom? – Jepper May 06 '14 at 19:45
  • Ah, I see. Take a look at [page 123 f. of DevOps for Developers](http://books.google.de/books?id=yEqrMNX3LAgC&lpg=PA124&ots=IoIU0MYUp-&dq=huettermann%20automatic%20releasing&hl=de&pg=PA123#v=onepage&q=huettermann%20automatic%20releasing&f=false), Michael Hüttermann describes a Maven plugin that removes the -SNAPSHOT and sets the _newVersion_ property of the versions plugin accordingly. – Behe May 06 '14 at 20:59
  • I give up. I'm using the release plugin for now. – Jepper May 07 '14 at 09:50
  • 1
    And it has to be all Maven? You could get the version via the help plugin - mvn help:evaluate -Dexpression=project.version, but then you need some help of sed or something to remove the 'SNAPSHOT' suffix. Of course you might as well grep for the version directly. – Hardy May 07 '14 at 09:57
  • Coming back to this, I want to say, understanding the maven release process was the right way to go; trying to fudge snapshot and release versions was wrong. Using the maven release plugin has taught me a lot. – Jepper Feb 11 '16 at 22:42
  • @Jepper I added a new answer, which you may want to make the accepted answer (the one with `mvn versions:set -DremoveSnapshot`, not my older answer) – Rinke Sep 04 '17 at 11:38

4 Answers4

72

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

mvn versions:set -DremoveSnapshot
Rinke
  • 6,095
  • 4
  • 38
  • 55
  • 1
    Thanks for this one, that's the simplest solution ! – Pierre Sep 04 '17 at 14:03
  • @jeanMarcAssin Shouldn't this be the accepted answer? – Rinke Sep 06 '17 at 13:07
  • Yes it should, but I'm not the OP so I can't change it. @Jepper did you tried Rinke solution ? – Pierre Sep 06 '17 at 14:17
  • 1
    Works with plugin version 2.5. Doesn't work with plugin version 2.2. Don't know in between. Version 2.5 is the latest at time of writing. – Radu Mar 01 '18 at 22:19
  • 3
    Nice! Thanks so much. And if you use version control, dispense with that silly poor-person's backup mechanism: `mvn versions:set -DremoveSnapshot -DgenerateBackupPoms=false` – Garret Wilson Apr 30 '19 at 13:52
  • This is really weird, I tried to find that instruction in the documentation and I didn't find it... Do you have any reference for the documentation? – Juan Ramos Aug 26 '19 at 09:47
  • 1
    @JuanRamos https://www.mojohaus.org/versions-maven-plugin/set-mojo.html#removeSnapshot – Rinke Aug 26 '19 at 13:27
  • @Radu works since 2.10, see https://www.mojohaus.org/versions-maven-plugin/set-mojo.html#removeSnapshot – elonderin Sep 27 '22 at 12:15
13

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.
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
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.

Rinke
  • 6,095
  • 4
  • 38
  • 55
4

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.

mzc
  • 3,265
  • 1
  • 20
  • 25