2016-11-09 72 views
1

我有一個包含三個android項目的目錄。 的MAINDIR看起來像這樣:Gradle/Jenkins:創建一個Gradle文件來指導子項目

/.gradle 
/.git 
/project1 
/project2 
/project3 
.gitignore 
.Jenkinsfile 
.README.md 

在詹金斯說launchs gradle這個任務eauch這些項目,因爲他不知道這些項目的構建過程中,我不能運行shell腳本(他說「不分項目「)。

在項目目錄,它看起來像:

/.gradle 
/app 
/build 
/gradle 
.gitignore 
.build.gradle 
.gradle.properties 
.gradlew 

有沒有辦法讓詹金斯瞭解這些三個項目,他可以發動gradle這個TAKS?就像在主目錄中創建一個build.gradle文件那樣做? 或者我應該創建3個Jenkins物品?

回答

0

您可以在jenkins中創建三個版本,但除非需要單獨構建庫,否則可能會導致額外的工作量。聽起來像你真正想要的是一個多項目構建[1]。一個簡單的例子可以坐在你的lib項目上方的文件夾中作爲兩個文件:build.gradlesettings.gradle

settings.gradle將定義你的構建範圍中包含哪些項目。

例如,假設你的project1,project2project3例如你的settings.gradle可能看起來像這樣。

rootProject.name = 'myRootProjectName' 

// note the name is not required to match the actual path 
include ":project1" 
// but if the name is not the same as the path then we can just 
// let gradle know where the project is expected 
project(":project1").projectDir = new File(settingsDir, "pathToProject1") 

include ":project2" 
project(":project2").projectDir = new File(settingsDir, "pathToProject2") 

include ":project3" 
project(":project3").projectDir = new File(settingsDir, "pathToProject3") 

//##### below would be instead of the code above, same thing just manual 
// project setup vs letting gradle find the subprojects 
// note sometimes you have lots of subprojects in that case it's sometimes 
// easier to just use a little logic for finding and setting up the subprojects. 
// don't use the code above ##### and below only use one or the other 
// or you will have errors. The method below is the most scaleable since 
// adding projects requires zero modifications to the root project 
rootProject.name = 'myRootProjectName' 

// set up a couple file filters to find the dirs we consider subprojects 
FileFilter projectFilter = { File pathname -> 
    FileFilter gradleProjectFilter = { File file -> file.name == 'build.gradle' } 
    // add this folder if is a directory and that directory contains a build.gradle file 
    // here note `File#listFiles` is true if it's `size() > 0` due to 
    // groovy's concept of truth (details: http://groovy-lang.org/semantics.html#Groovy-Truth) 
    return pathname.isDirectory() && pathname.listFiles(gradleProjectFilter) 
} 

settingsDir.listFiles(projectFilter).each { dir -> 
    include ":$dir.name" 
    project(":$dir.name").projectDir = dir 
} 

現在正在運行gradle projects任務應顯示三個子模塊。

至於你的build.gradle文件,你可以爲所有的模塊指定一些共同的屬性,如果需要的話,或只是將文件留空,它必須存在,但可以是空的。如果你想分享一些配置,那麼你可以用這樣的東西來設置build.gradle

project.subprojects { Project subproject -> 
    // anything that is defined here will be executed before the subproject's build.gradle file 
    subproject.buildscript { 
     repositories { 
      jcenter() 
      // your private maven repo if needed 
      maven { url 'http://1.2.3.4:8081/nexus/content/repositories/release' } 
     } 
     dependencies { 
      // some plugin that is now available to be applied in any subproject 
      classpath 'my.sweet.gradle:plugin:0.1' 
     } 
    } 
    subproject.afterEvaluate { 
     // this block is executed after the subproject's build.gradle file 
     if (project.tasks.withType(org.gradle.jvm.tasks.Jar)) { 
      // for example you might want to set the manifest for each subproject 
      manifest { 
       attributes 'Implementation-Title': "Lib $subproject.name", 
         'Implementation-Version': version 
      } 
     } 
    } 
} 

[1] https://docs.gradle.org/current/userguide/multi_project_builds.html

+0

真棒,THX的幫助。 –

相關問題