2016-03-08 118 views
0

我希望能夠定義設置(理想用戶gradle.properties)的倉庫我可以將自定義存儲庫添加到gradle.properties中嗎?

的最終目標是這樣的:

repositories { 
    mavenCentral() // Can't/don't want to use this 
    nexusCentral() // Can use these - on network Nexus server 
    nexusSnapshot() 
} 

我怎麼會去這樣做呢?再一次,這將在理想情況下進入用戶級gradle.properties文件,所以我們不必在每個模塊中都引用它。

這僅僅是Maven的提供了一個簡單的行家風格神器庫,手工的方式將是:

maven { 
     url "http://path/to/nexus" 
    } 

另外一個要求是使用「發佈」的任務,這對一個存儲庫中定義的憑據(即詹金斯用於發佈模塊):

publishing { 
... 
maven { 
      url "http://path/to/nexus" 
      // Jenkins provides these as -P Gradle parameters. 
      credentials { 
       username = "${uploaderUser}" 
       password = "${uploaderPassword}" 
      } 
     } 

這些憑證將不知道普通用戶,而是將理想中詹的gradle.properties進行配置。我們不希望用戶構建失敗,因爲他們無法解析憑據 - 他們甚至不會使用「發佈」任務。

回答

2

您可以使用somenthing這樣的:

maven { 
      credentials { 
       username getCredentialsMavenUsername() 
       password getCredentialsMavenPassword() 
      } 
      url 'xxxxx' 
    } 

/** 
* Returns the credential username used by Maven repository 
* Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_USERNAME key 
* @return 
*/ 
def getCredentialsMavenUsername() { 
    return hasProperty('CREDENTIALS_USERNAME') ? CREDENTIALS_USERNAME : "" 
} 

/** 
* Returns the credential password used by Maven repository 
* Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_PASSWORD key 
* @return 
*/ 
def getCredentialsMavenPassword() { 
    return hasProperty('CREDENTIALS_PASSWORD') ? CREDENTIALS_PASSWORD : "" 
} 

如果用戶沒有憑據的腳本不會失敗。

+0

謝謝,這可能就是我最終的結果。 – Dan

2

不知道這回答你的問題,但你可以把這個在gradle.properties文件:

nexusUrl=http://path/to/nexus 

,併爲此在的build.gradle:

maven { 
    url project.property(nexusUrl) 
} 

編輯:

關於您的憑據,您應該需要的全部內容類似於

if (project.hasProperty('uploaderUser') && project.hasProperty('uploaderPassword')) { 
    credentials { 
     username = project.property('uploaderUser') 
     password = project.property('uploaderPassword') 
    } 
} 
+0

我認爲應該*主要*覆蓋它。唯一剩下的就是其中一個存儲庫具有設置的憑據(所以Jenkins可以發佈),例如: 我希望隱藏所有這些: * Jenkins從用戶/密碼獲取屬性 *普通用戶從不關心用戶/密碼(並且在未設置值時不會失敗) 用該位添加了另一個原始答案的片段。謝謝! – Dan

+0

我編輯了我的答案。這個想法是一樣的。 –

相關問題