从build.gradle获取价值并编写代码 [英] Get value from `build.gradle` to code
问题描述
我们有一个 build.gradle
,其中定义了版本
。我需要实现一个版本端点(例如 / version
)以获取项目的版本。 build.gradle
中的 version
属性已经存在很长时间了,我无法将其移至 project.properties
文件。如何从Java代码中访问此版本
的值?
We have a build.gradle
where the version
is defined in it. I need to implement a version endpoint ( like /version
) to get the version of the project. This version
property in build.gradle
has been there for a long time, I can't move it to the project.properties
file. How can I access this version
's values from my Java code?
推荐答案
有很多方法可以将信息植入代码中
There are many ways with which you can "plant" information into your code
首先,您可以使用清单文件,并从中读取文件
First you can use the manifest file, and read from it
jar {
manifest {
attributes(
"lib-version": version
}
}
另一种选择是在jar任务之前将该信息添加到您的属性文件中
The other option is to add that info the your property files before the jar task
task('addVersion') {
doLast {
//append the version here, see example
file("src/main/resources/props.properties").append("version=$version")
}
}
jar.dependsOn(addVersion)
这篇关于从build.gradle获取价值并编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!