build.gradle
task readProperties {
def localProperties = new Properties()
def dis = null
try {
def localFile = rootProject.file('app.properties')
if (localFile.exists()) {
dis = localFile.newDataInputStream();
localProperties.load(dis)
}
} catch (Exception ignored) {
println ignored
} finally {
// 一定要关闭输入流 否则文件会一直被java占用
if (dis != null) {
dis.close()
}
}
def version = localProperties.getProperty("version")
String name = localProperties.getProperty("name")
println version
println name
}
执行命令:
gradle -q readProperties