intellij-idea - 有没有办法从 kotlin 项目的 build.gradle 获取版本?
问题描述
我想在我在 build.gradle 中设置的项目中获取我的项目版本。有什么方法可以在我的项目中获取版本,因为我需要在我的软件中显示版本并用于比较更新信息,这样我每次发布更新时都不需要更改两次。有什么办法可以做到吗?
group 'ProjectName.group'
version "ProjectVersion"
解决方案
您通常通过加载属性文件并配置 gradle 来过滤processResources
任务中的属性文件来做到这一点。
例子:
构建.gradle:
version = '1.5.0'
processResources {
def properties = ['version': project.version]
inputs.properties(properties)
filesMatching('version.properties') {
expand(properties)
}
}
版本属性:
version=${version}
应用程序.java:
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load(App.class.getResourceAsStream("/version.properties"));
System.out.println(properties.getProperty("version"));
}
推荐阅读
- file-extension - 打开 .grp .mps 和 .rsc .rsr
- html - Html 文件没有与我的 css 文件一起下沉?
- node.js - YAML 多行正则表达式修剪注释和空格
- html - 引导表单下拉验证
- bash - bash 在字符串后读取拆分文件
- ruby-on-rails - 如何使用 Arel 跨多态 has_one 关系进行查询
- angular - Angular 8 路由与子模块中的子路由
- r - 一个向量上的多个股票
- python - 当“ObjectId 不是 JSON 可序列化”时,如何正确 jsonify Mongo DB 文档?
- pandas - 如何使用 pandas 处理包含多个标签(索引词)的字符串