我正在使用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****);
}
我已经阅读了各种文章,但是没有找到解决方法。
是否有执行此操作的标准方法?
答案 0 :(得分:0)
我认为,将属性放入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);
}