java - 指定和检索项目版本的正确方法(Gradle)
问题描述
我正在使用 Gradle 构建我的应用程序,我想知道指定和检索应用程序版本的最佳方法。
我已经看到将其放入其中之一的建议gradle.properties
:
version=0.0.1-SNAPSHOT
或build.gradle
:
allprojects {
group = 'com.my-app'
version = '0.0.1-SNAPSHOT'
}
这两个选项中哪个是最好的?
然后如何在运行时检索版本?
public static void main(String[] args)
{
System.out.println(****retrieve version number here****);
}
我已经阅读了各种帖子,但我没有看到解决方案。
有这样做的标准方法吗?
解决方案
我认为,将属性放入gradle.properties
是最好的方法,因为它是用于存储变量的项目的配置文件。如果您知道gradle.properties
文件的路径,则可以在运行时获取属性:
public static void main(String[] args){
Properties properties = new Properties();
File fileProps = new File("yout/path", "gradle.properties");
properties.load(new FileInputStream(fileProps));
String version = properties.getProperty("version");
System.out.println("Version = " + version);
}
推荐阅读
- flutter - 颤振:如何在 Expansiontile 中添加点击
- c# - .NET 中 OFB 模式下的 DES 加密
- blazor - 验证不成功时自定义组件不返回值
- python - 正则表达式精确指定匹配内容的一次出现
- reactjs - 有条件地在反应中呈现下载按钮
- node.js - 在 Windows 10 上从 nodejs 调用 processing-java
- javascript - 如何获取从 settimeout 调用的函数的返回值
- python - 得到
在 Python 上向 Telegram 发送包含加号“+”的字符串时 - r - 使用 R,RStudio Knit to HTML,如何包含“内联”外部 HTML 文档块?
- sql - SQL Server mm/dd/yy 或 yy/dd/mm 或其他什么的 T-SQL 日期数据类型格式?