在build.gradle内设置从local.properties读取的属性
我使用的是3.4级。我已将以下属性移动到名为local.properties的文件中(与build.gradle目录相同): local.properties在build.gradle内设置从local.properties读取的属性,gradle,build.gradle,Gradle,Build.gradle,我使用的是3.4级。我已将以下属性移动到名为local.properties的文件中(与build.gradle目录相同): local.properties nexusUsername=someuser nexusPassword=somepassword build.gradle File secretPropsFile = file('./local.properties') if (secretPropsFile.exists()) { Properties p = new P
nexusUsername=someuser
nexusPassword=somepassword
build.gradle
File secretPropsFile = file('./local.properties')
if (secretPropsFile.exists()) {
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
project.set name, value
}
} else {
throw new IllegalStateException("secret.properties could not be located for build process")
}
我得到以下例外情况:
Could not find method set() for arguments [nexusUsername, someuser] on root project 'some-java-project of type org.gradle.api.Project.
您得到的错误是正确的。要设置属性,需要使用
ext
。请看一下这本书
因此,以下代码将完成这项工作:
File secretPropsFile = file('./local.properties')
if (secretPropsFile.exists()) {
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
ext[name] = value
}
} else {
throw new IllegalStateException("secret.properties could not be located for build process")
}
println project.nexusPassword
println project.nexusUsername //property is set in project's scope via ext