0

We are trying to get the application version from maven pom.xml file and dynamically deciding where some documents are located. For example, if the application version is 1.5, the documents may be in version1.5/application/ and so on.

The problem with getting version directly from pom.xml is , if application version is a snapshot, it will return 1.5.0-SNAPSHOT. This is a problem. For now we have solved it by taking a substring of the first three characters. This way of solving the problem might create problems in the future if, say, application version is 1.11.

Is there any way we can get just the major and minor version, without the snapshot thing in maven ?

For example,

  • if the maven version is 1.5.0-SNAPSHOT, I want 1.5
  • if the maven version is 1.5.1, I still want just 1.5
  • if the maven version is 1.12.1, I want just 1.12
user2434
  • 6,339
  • 18
  • 63
  • 87
  • Define `variable` and set desired value. So, in the `.properties` you can get this value. I mean this [http://stackoverflow.com/a/3697482/3710490](http://stackoverflow.com/a/3697482/3710490). Where this logic is done? – Valijon Dec 17 '15 at 15:16
  • Could you split on "." (note that ```String.split``` takes a regex, so you'd have to use ```String.split("\\.")```, take the first two substrings, and join them with a "."? **EDIT:** I made an answer for this below. – Eric Galluzzo Dec 17 '15 at 15:22
  • 1
    Possible duplicate of [Get Maven artifact version at runtime](http://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime) – Koshinae Dec 17 '15 at 15:34
  • @Valijon This will result in the full version but not into separated parts for major, minor version etc. – khmarbaise Dec 18 '15 at 19:27

2 Answers2

1

I would suggest to use the build-helper-maven-plugin which offers a goal parse-version-mojo which provides the parsed version information as properties.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
0

Try this:

public String getSanitizedVersion(String version) {
    // Note that String.split() takes a regexp, so we need to escape the ".".
    String[] versionComponents = String.split(version, "\\.");
    if (versionComponents != null && versionComponents.length >= 2) {
        return versionComponents[0] + "." + versionComponents[1];
    }
    return null;
}

Hope that helps!

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20