在Java文件Maven的版本号 [英] Maven Version number in java file

查看:212
本文介绍了在Java文件Maven的版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自哪里ANT可以调用一个.java文件通过执行一些方法获得的版本号。我愿做我的新的pom.xml自动获得正确的版本号类似的东西。我需要的应用程序,以了解自己的版本,并在同一时间的Maven了解的版本得到了做出正确的基础之上。保持翻番的版本infomormation没有选项。

I come from ANT where you can invoke a .java file to obtain the version number through executing some method. I would like to do something similar in my new pom.xml to automatically obtain the correct version number. I need the app to know about its own version and at the same time Maven to know about the version got make the right builds. Holding the version infomormation doubled is no option.

还是可以的Maven构建前创建过程中/一个新的java文件?

Or can Maven create a new .java file during/before building?

我可以像了不少办法才达到这一点,但有一个最佳实践方式Maven项目做到这一点?

I can image quite a few ways to achive this, but is there a best practise way to do this in Maven project?

推荐答案

在构建一个JAR,Maven的写入的pom.xml POM的.properties 文件到 META-INF /行家/ [组ID] / [神器ID] 目录。然后,您可以阅读版出来其中一个在你的应用程序。

When building a JAR, Maven writes the pom.xml and a pom.properties file into META-INF/maven/[group ID]/[artifact ID] directory. You can then read the version out of one of these in your application.

另外,你可以明确地使用<一个写一个新的version.properties文件href=\"http://www.sonatype.com/books/mvnref-book/reference/resource-filtering-sect-description.html\">resource过滤。

Alternatively, you can explicitly write a new version.properties file by using resource filtering.

如果你把你的POM以下内容:

If you put the following in your POM:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

然后把 version.properties 模板导入的src / main / resources目录,看起来像:

and then put a version.properties template into the src/main/resources directory that looks like:

version = ${project.version}

Maven会在读出POM的版本代替,你的应用程序,然后只需要阅读 version.properties 资源和查询版本键。

这篇关于在Java文件Maven的版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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