2016-09-13 44 views
3

我在Gradle中有多模塊項目,其中父build.gradle僅僅是沒有任何源的子項目的聚合器。使用Gradle中的一個版本和標籤釋放多模塊項目

結構:

parent with version in gradle.properties 
- child1 inheriting version 
- child2 inheriting version 

兩個孩子產生與文物的JAR,而母公司生產,我不希望在任何地方都上傳空罐。

現在我想發佈這樣的項目。在Git中應該有一個提交版本更新和一個標籤。但是,應該構建所有子項目並將其上載到存儲庫。 我該如何實現它?

我試過gradle-release plugin,但我努力正確配置它。我得到以下任一操作:

  • 應用插件的父母,得到應有的承諾和標籤,但只能得到根項目上傳
  • 適用於子項目的插件,讓項目正常上傳,但有獨立的承諾,並標記爲每子項目
+0

還有一個插件示例項目,其中包含具有單個版本示例的多項目。 https://github.com/researchgate/gradle-release-examples/tree/master/multi-project-single-version – Hillkorn

回答

1

我遇到了同樣的問題,最後在gradle-svn-tools插件(https://github.com/martoe/gradle-svntools-plugin)的幫助下寫了Gradle中的必要步驟。請注意,我不是專家的Groovy,所以我想一些事情,可以更容易地完成:)

我在父項目的build.gradle:

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'at.bxm.gradleplugins:gradle-svntools-plugin:1.6.2' 
    } 
} 

allprojects { 
    apply plugin: 'at.bxm.svntools' 

    //subversion login 
    svntools { 
     username = svnUsername 
     password = credentials.svnpwd 
    } 
} 

//task to write the version parameter given via command line into the "gradle.properties" files. 
// Call with: gradle writeProjectVersion -PnewVersion=1.0.1-SNAPSHOT 
task('writeProjectVersion') << { 
    if (project.hasProperty('newVersion')) { 
     //set version in gradle settings 
     project.version = project.newVersion 
     project.subprojects?.each {it.version = project.version } 
     //set version in all the gradle.properties files 
     new File(".").traverse(type : groovy.io.FileType.FILES, nameFilter: 'gradle.properties') { 
      project.ant.replaceregexp(file: it, byline: true) { 
       key = 'version' 
       version = project.version 
       regexp(pattern: "^(\\s*)$key((\\s*[=|:]\\s*)|(\\s+)).+\$") 
       substitution(expression: "\\1$key\\2$version") 
      } 
     } 
     println 'Successfully changed project version in gradle.properties to \'' + project.version + '\'' 
    } else { 
     println 'No parameter \'newVersion\' provided, so not changing the project version.' 
    } 
} 


//task to commit the gradle.properties changes into SVN - also needs the new version as parameter for the commit message 
// Call with: gradle svnCommit -PnewVersion=1.0.1-SNAPSHOT 
task('svnCommit', type: at.bxm.gradleplugins.svntools.tasks.SvnCommit) { 
    if (project.hasProperty('newVersion')) { 
     def files = [] 
     new File(".").traverse(type : groovy.io.FileType.FILES, nameFilter: 'gradle.properties') { files << it } //appends all matching files to the list 
     source = files 
     commitMessage = "[Jenkins Release Build] Changing project version to '" + project.newVersion + "'" 
    } else { 
     println 'No parameter \'newVersion\' provided, so SVN commit was not executed.' 
    } 
} 

//task to tag the current head - also needs the new version as parameter for the commit message 
// Call with: gradle svnTag -PnewVersion=1.0.1-SNAPSHOT 
task('svnTag', type: at.bxm.gradleplugins.svntools.tasks.SvnTag) { 
    if (project.hasProperty('newVersion')) { 
     tagName = "$project.newVersion" 
     localChanges = true 
     commitMessage = "[Jenkins Release Build] Tagging Release version $project.newVersion" 
    } else { 
     println 'No parameter \'newVersion\' provided, so SVN tagging was not executed.' 
    } 
} 

我用詹金斯執行和運行下面的shell腳本:

# execute gradle tests 
gradle test 
# set version in gradle.properties and tag the current code (gradle.properties changes are commited in the tag) 
gradle writeProjectVersion -PnewVersion=${releaseVersion} 
gradle svnTag -PnewVersion=${releaseVersion} 
# build the artifacts and upload them to Nexus without running the test cases again 
gradle clean build uploadArchives -x test 
# set version in gradle.properties and commit the files 
gradle writeProjectVersion -PnewVersion=${newSnapshotVersion} 
gradle svnCommit -PnewVersion=${newSnapshotVersion} 

希望這會有所幫助。

相關問題