首页 > 解决方案 > 指定和检索项目版本的正确方法(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****); 
}

我已经阅读了各种帖子,但我没有看到解决方案。

有这样做的标准方法吗?

标签: javagradleproperties

解决方案


我认为,将属性放入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); 
}

推荐阅读