在pom.xml中自动增量的版本号,并在应用中展示它 [英] Autoincrement version number in pom.xml and display it in the app

查看:3195
本文介绍了在pom.xml中自动增量的版本号,并在应用中展示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个问题问了多次,但没有令人满意的答案:让我们presume你有Maven项目生产一些罐子(Java桌面应用程序)。
一个人怎么可以定义pom.xml的版本号,这将自动增加(甚至是手动,无所谓)合适的(例如与每一个版本)时,但可能加载该版本到应用程序?
我们的目标是为用户显示他目前正在使用的应用程序的版本。

I saw this question asked multiple times, however without satisfying answer: let's presume you have maven project producing some jar (java desktop app). How can one define version number in pom.xml, which would be automatically incremented (or even manually, does not matter) when appropriate (e.g. with every build) but with the possibility to load this version into the application? The goal is to display for the user the version of the app he is currently using.

推荐答案

有特别四个选项,你可以去:

There are in particular four options you can go for:


  1. 使用的是默认的Maven所创建的pom.properties文件。

  2. 使用其由MANIFST.MF文件提供的信息。有几种方法来获得这些信息。

  3. 创建这是在构建过程中过滤,并会被你的应用程序可以读取的属性。

  4. 使用生成的类,它包含适当的信息。

第一个选项可以通过这样的Java类来处理

public class TheVersionClass
{
    ..

    public TheVersionClass()
    {
        InputStream resourceAsStream =
          this.getClass().getResourceAsStream(
            "/META-INF/maven/com.soebes.examples/version-examples-i/pom.properties"
          );
        this.prop = new Properties();
        try
        {
            this.prop.load( resourceAsStream );
        }
        ...
    }
}

借助第二个选择是使用MANIFEST.MF文件

public class TheVersionClass {
    public TheVersionClass() {
        System.out.println( "  Implementation Title:" + this.getClass().getPackage().getImplementationTitle() );
        System.out.println( " Implementation Vendor:" + this.getClass().getPackage().getImplementationVendor() );
        System.out.println( "Implementation Version:" + this.getClass().getPackage().getImplementationVersion() );
        System.out.println( "    Specification Tile:" + this.getClass().getPackage().getSpecificationTitle() );
        System.out.println( "  Specification Vendor:" + this.getClass().getPackage().getSpecificationVendor() );
        System.out.println( " Specification Version:" + this.getClass().getPackage().getSpecificationVersion() );
    }
}

不幸的是,这些信息通常不被放入MANIFEST.MF文件,所以你必须改变你的配置。

Unfortunately these informations are usually not be put into the MANIFEST.MF file so you have to change your configuration.

第三个选择是创建过滤作为在生成过程中的资源文件,第四选择是使用的模板 - Maven的插件以创建适当的类。所有上面可以进去看了 GitHub的项目

The third option is to create a file which is filtered as a resourced during the build process and the forth option is to use templating-maven-plugin to create appropriate classes. All the above can be looked into the github project.

当然,你可以提高任何的例子你需要使用下面的代码片段应该放置到你的根使用buildnumber - Maven的插件从您的版本控制系统的信息添加到您的MANIFEST.MF文件您的模块或更好的在其中添加此为每次构建执行公司POM文件:

And of course you can enhance any of the examples to your need to use buildnumber-maven-plugin to add information from your version control system into your MANIFEST.MF file by using the following snippet which should be located into your root of your modules or much better into a company pom file which add this to be executed for every build:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
      <revisionOnScmFailure>UNKNOWN</revisionOnScmFailure>
      <getRevisionOnlyOnce>true</getRevisionOnlyOnce>
      <providerImplementations>
        <svn>javasvn</svn>
      </providerImplementations>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>create</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

如果你只是一个新的版本,我不会更改版本。这可能是有用的补充,从詹金斯或任何CI解决方案,您使用到MANIFEST.MF文件中的buildnumber,但我会使用Maven的版本,在发布的情况下,将从 1.0-改变快照 1.0

I wouldn't change the version if you have just a new build. It might be usefull to add the buildnumber from Jenkins or whatever CI solution you are using into the MANIFEST.MF file, but i would use the version of Maven which in case of a release will be changed from 1.0-SNAPSHOT into 1.0.

这篇关于在pom.xml中自动增量的版本号,并在应用中展示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆