java 获取maven版本号,在Java文件Maven的版本号

"在Maven项目中,可以通过将版本信息写入`pom.xml`和`pom.properties`文件来实现应用内部获取版本号。此外,可以利用资源过滤特性创建一个`version.properties`模板文件,Maven在构建时会替换`${project.version}
摘要由CSDN通过智能技术生成

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.

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

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

解决方案

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.

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

If you put the following in your POM:

src/main/resources

true

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

version = ${project.version}

Maven will substitute in the version read out of the POM, and your application then just needs to read the version.properties resource and query the version key.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值