Can I get properties defined in “gradle.properties” in JAVA STATEMENT?

纵饮孤独 提交于 2019-12-22 09:15:03

问题


I defined a property in gradle.properties file like below:

user.password=mypassword

Can I use it as a variable value in my java statement?


回答1:


Yes you can, however this is not a good idea nor a good practice. gradle.properties file is meant to keep gradle's properties itself, e.g. JVM args used at build time.

If you need to store user/pass pair in properties file, it should be placed under src/main/resources or other appropriate folder and separate from gradle.properties.

Side note: Not sure if keeping properties file in mobile application is safe in general.




回答2:


You would have to read the properties file and extract the property first.

Properties prop = new Properties();
InputStream input = null;

try {

    input = new FileInputStream("gradle.properties");

    // load a properties file
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("user.password"));
} catch (IOException ex) {
    ex.printStackTrace();
}

You can find the detailed tutorial here



来源:https://stackoverflow.com/questions/31690675/can-i-get-properties-defined-in-gradle-properties-in-java-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!