指定和检索项目版本的正确方法(Gradle)

时间:2018-11-14 20:13:59

标签: java gradle properties

我正在使用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****); 
}

我已经阅读了各种文章,但是没有找到解决方法。

是否有执行此操作的标准方法?

1 个答案:

答案 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); 
}