2017-05-09 35 views
1

我使用等級v3.4。我提出以下屬性到一個文件名爲local.properties(同一目錄中的build.gradle):設置屬性讀取從local.properties裏面build.gradle

local.properties

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. 

回答

1

您收到的錯誤是正確的。要設置屬性,您需要使用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