Manipulate project version property to remove SNAPSHOT?
Asked Answered
K

3

26

I've got a project at version 0.0.1-SNAPSHOT, and when we build it via TeamCity, we also get a build.vcs.number property, which is the Subversion revision that triggered the build.

In our assemblies, we create a zip file called something like foo-distribution-0.0.1-SNAPSHOT.zip, but I was wondering whether there's a way I can insert the build.vcs.number property into the artifact name to give foo-distribution-0.0.1.12345-SNAPSHOT.zip?

Is there a built-in property that is just the numeric part of the version number, or some other way of splitting off the -SNAPSHOT part?

EDIT: I have already tried setting the pom.xml version as ${my.version}-SNAPSHOT, and then defining my.version in the properties - this works for ever case except for the Maven Release Plugin, which complains that it cannot parse the version (understandably, it can't auto-guess the next development version either).

Kagera answered 12/11, 2012 at 16:48 Comment(2)
The simplest solution to get rid of the "-SNAPSHOT" is to release the artifact. The best thing is to put the revision number into the manifest file and not as part of the filename. Using a property to define the version number will not work and does not make sense either. You can use a property which is filtered during the build and which can contain the version number as well.Sola
possible duplicate of Properties lost during Maven release:performMayhem
S
60

I realize this question is a tad dated, but I just ran into a similar situation, and this is how I resolved it:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <id>parse-version</id>
        <goals>
          <goal>parse-version</goal>
        </goals>
      </execution>
    </executions>
</plugin>

What this build-helper plugin's "parse-version" mojo will do is give you the following properties that you can use as you see fit:

parsedVersion.majorVersion
parsedVersion.minorVersion
parsedVersion.incrementalVersion
parsedVersion.qualifier
parsedVersion.buildNumber

That should cover all of your desired version "parts." I'm currently using this to build chrome extensions in which the manifest version cannot include "-SNAPSHOT" and must be at most 4 numbers separated by dots. In my use case, I use this combination to produce the desired result:

"version":"${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}"

So, I'm essentially stripping of "-SNAPSHOT" so my local dev copies of my extension will install properly for testing. You can build whatever you want with the pieces. =)

Schlessinger answered 14/8, 2013 at 14:40 Comment(1)
To increase that version the following string can be used: newVersion=\${parsedVersion.majorVersion}${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}Solander
G
4

For others who are looking to do more than this or would like to remove SNAPSHOT from build number, this plugin is pretty helpful http://www.mojohaus.org/build-helper-maven-plugin/usage.html

I particularly found this useful

Set a property by applying a regex replacement to a value

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.9.1</version>
        <executions>
          <execution>
            <id>regex-property</id>
            <goals>
              <goal>regex-property</goal>
            </goals>
            <configuration>
              <name>human.version</name>
              <value>$\{project.version}</value>
              <regex>-SNAPSHOT</regex>
              <replacement> pre-release development version</replacement>
              <failIfNoMatch>false</failIfNoMatch>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
Gemina answered 19/10, 2015 at 13:58 Comment(0)
T
0

For those wanting to use 4 numbers (x.y.z.a) in their version, here is how you could configure the Build Helper Maven Plugin. The idea is to replace the x.y.z.a-SNAPSHOT to x.y.z-a which will allow the build-helper plugin to parse the buildNumber.

The antrun plugin is only here for debug purpose.

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>regex-property</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>modified.version</name>
                        <value>${project.version}</value>
                        <regex>\.([0-9]+)-SNAPSHOT</regex>
                        <replacement>-$1</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                    </configuration>
                </execution>

                <execution>
                    <id>parse-version</id>
                    <goals>
                        <goal>parse-version</goal>
                    </goals>
                    <configuration>
                    <versionString>${modified.version}</versionString>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>Major: ${parsedVersion.majorVersion}</echo>
                            <echo>Minor: ${parsedVersion.minorVersion}</echo>
                            <echo>Incremental: ${parsedVersion.incrementalVersion}</echo>
                            <echo>Qualifier: ${parsedVersion.qualifier}</echo>
                            <echo>BuildNumber: ${parsedVersion.buildNumber}</echo>
                            <echo>----------------</echo>
                            <echo>Next Major: ${parsedVersion.nextMajorVersion}</echo>
                            <echo>Next Minor: ${parsedVersion.nextMinorVersion}</echo>
                            <echo>Next Incremental: ${parsedVersion.nextIncrementalVersion}</echo>
                            <echo>Next BuildNumber: ${parsedVersion.nextBuildNumber}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Running mvn validate on a project with version 7.0.0.29-SNAPSHOT:

[INFO] --- build-helper-maven-plugin:3.0.0:regex-property (regex-property) @
[INFO] --- build-helper-maven-plugin:3.0.0:parse-version (parse-version) @ 
[INFO] --- maven-antrun-plugin:1.1:run (default) @ 
[INFO] Executing tasks
 [echo] Major: 7
 [echo] Minor: 0
 [echo] Incremental: 0
 [echo] Qualifier:
 [echo] BuildNumber: 29
 [echo] ----------------
 [echo] Next Major: 8
 [echo] Next Minor: 1
 [echo] Next Incremental: 1
 [echo] Next BuildNumber: 30
Teazel answered 13/9, 2019 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.