简体   繁体   中英

Correct way to specify and retrieve project version (Gradle)

I am using Gradle to build my application and I want to know the best way to specify and retrieve the application version.

I've seen suggestions to put it in either gradle.properties :

version=0.0.1-SNAPSHOT

or build.gradle :

allprojects {
    group = 'com.my-app'
    version = '0.0.1-SNAPSHOT'
}

Which of these two options is the best?

How do I then retrieve the version at runtime?

public static void main(String[] args)
{
    System.out.println(****retrieve version number here****); 
}

I've read around various posts but I'm not seeing a solution.

Is there a standard way of doing this?

I think, that put property into gradle.properties is the best way, because it is configuration file for your project designed to store variables. You can get property in runtime if you know path to gradle.properties file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM