建立multi-project,須要在rootProject的目錄下新建settings.gradle.java
目錄: I:\gretty\hello-gradle\multi-Project Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2014/11/8 14:04 .gradle d---- 2014/11/8 14:00 flower d---- 2014/11/8 14:00 tree -a--- 2014/11/8 14:11 267 build.gradle -a--- 2014/11/8 14:05 23 settings.gradle
根據上面的目錄咱們能夠像這樣設置settings.gradlegradle
include 'tree','flower'
執行gradle projectsui
PS I:\gretty\hello-gradle\multi-Project> gradle projects :projects ------------------------------------------------------------ Root project ------------------------------------------------------------ Root project 'multi-Project' +--- Project ':flower' \--- Project ':tree' To see a list of the tasks of a project, run gradle <project-path>:tasks For example, try running gradle :flower:tasks BUILD SUCCESSFUL Total time: 4.414 secs
這樣咱們就新建了一個gradle 的multi-project的項目。lua
涉及到multi-project的設置,有allProjects subProjects,能夠對其進行配置。code
1 allProjects,包括rootProject和全部的subProject均可以根據allProject中的配置構建項目。it
allprojects { task printInfo << { println "This is ${project.name}" } 咱們執行gradle printInfo PS I:\gretty\hello-gradle\multi-Project> gradle printInfo :printInfo This is multi-Project :flower:printInfo This is flower :tree:printInfo This is tree BUILD SUCCESSFUL Total time: 6.189 secs
能夠看到rootProject和subProject都執行task printInfo。ast
2 subprojects,只有子項目執行的任務class
subprojects { task printInfo << { println "Can be planted" } } PS I:\gretty\hello-gradle\multi-Project> gradle printInfo :flower:printInfo Can be planted :tree:printInfo Can be planted BUILD SUCCESSFUL
3 咱們還可使用project(":tree"),來指定項目運行任務配置
allprojects { task printInfo << { println "This is ${project.name}" } } subprojects { printInfo << { println "Can be planted" } } project(':tree').printInfo << { println "Has leaves" } project(':flower') { printInfo.doLast { println 'Smells nice' } } 執行 gradle printInfo PS I:\gretty\hello-gradle\multi-Project> gradle printInfo :printInfo This is multi-Project :flower:printInfo This is flower Can be planted Smells nice :tree:printInfo This is tree Can be planted Has leaves BUILD SUCCESSFUL Total time: 8.444 secs
從上面的輸出咱們也能夠看得出任務的執行順序。im
4 過濾配置
allprojects { task printInfo << { println "This is ${project.name}" } } configure(allprojects.findAll { it.name.startsWith('f') }) { printInfo << { println 'Smells nice' } } PS I:\gretty\hello-gradle\multi-Project> gradle printInfo :printInfo This is multi-Project :flower:printInfo This is flower Smells nice :tree:printInfo This is tree BUILD SUCCESSFUL Total time: 8.195 secs
使用afterEvaluate 進行過濾
allprojects { task printInfo << { println "This is ${project.name}" } } subprojects { afterEvaluate { project -> if (project.hasLeaves) { project.printInfo << { println 'Has leaves' } } } } project(':flower') { ext.hasLeaves = false } project(':tree') { ext.hasLeaves = true } 執行gradle printInfo PS I:\gretty\hello-gradle\multi-Project> gradle printInfo :printInfo This is multi-Project :flower:printInfo This is flower :tree:printInfo This is tree Has leaves BUILD SUCCESSFUL