How to get the semver Major part of a Maven version?

不羁岁月 提交于 2019-11-30 06:38:48
Yuriy Nemtsov

Found it. The build-helper-maven-plugin has the ability to parse-out the components of the version.

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

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>[version] ${project.version}</echo>
                <echo>[majorVersion] ${parsedVersion.majorVersion}</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

This works on Maven 3.3.9:

${project.artifact.selectedVersion.majorVersion}

Versions don't necessarily come in the structure you describe.

Maven has conventions for trailing numbers, but you don't have to use them.

If you have a convention that you like that you want to disassemble, you can write your own maven plugin that sets several properties to the several pieces as you define them.

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