Groovy 如何使用gradle从txt文件中提取键值对
假设我在一个txt文件中有一堆键值对,我想使用gradle将它们拉入我可以使用的变量中。我该怎么做呢 我试图做的不是在build.gradle文件中硬编码我的storepass和其他相关签名信息,并将它们置于托管源代码管理之外 我想做的是Groovy 如何使用gradle从txt文件中提取键值对,groovy,gradle,android-studio,android-gradle-plugin,Groovy,Gradle,Android Studio,Android Gradle Plugin,假设我在一个txt文件中有一堆键值对,我想使用gradle将它们拉入我可以使用的变量中。我该怎么做呢 我试图做的不是在build.gradle文件中硬编码我的storepass和其他相关签名信息,并将它们置于托管源代码管理之外 我想做的是 propertiesFile file(/buildProperties.txt) storeFile file(propertiesFile.getProperty("myStoreFile")) storePassword propertiesFile.
propertiesFile file(/buildProperties.txt)
storeFile file(propertiesFile.getProperty("myStoreFile"))
storePassword propertiesFile.getProperty("myStorePassword")
keyAlias propertiesFile.getProperty("myKeyAlias")
keyPassword propertiesFile.getProperty("myKeyPassword")
我知道这很简单,可以做到,但一个小时的谷歌搜索还没有找到解决办法。所以我终于找到了答案
def Properties props = new Properties()
def propFile = new File('path\to\file.txt')
release {
if (propFile.canRead()) {
props.load(new FileInputStream(propFile))
storeFile file(props['key.store'])
storePassword props['key.store.password']
keyAlias props['key.alias']
keyPassword props['key.alias.password']
}
}
Gradle为此提供了一种机制。只需将这些属性放在USER_HOME/.gradle/gradle.properties中,它们就会隐式地出现在您的构建脚本中。我决定坚持我的使用方式,但如果我知道的话,这会起作用。