Multi-project Build

Gradle provides support for modulized projects. Because every module in Gradle are called project, we can call this multi-project build.

Modularizing a project

Identify and refactor to modules

The former project structure is shown below.

The original project structure

As we can see, there are model, repository and web modules, we can modularize this project into 3 modules.

  • model: Data representation of To-do items
  • repository: Storage and retrieval of To-do items
  • web: Web components to handle HTTP requests


    Multi-project build

Assembling build

By running:

$ gradle projects

in the root path, you can get

 ------------------------------------------------------------
       Root project
       ------------------------------------------------------------
        Root project 'todo'
        No sub-projects

This report shows that you are dealing with a single project build.
We can declare sub-projects by settings.gradle

Introducing settings file

The settings file declares the configuration required to instantiate the project’s hierarchy. By default, this file is named settings.gradle and is created alongside the build.gradle file of the root project.
You can call the include in settings.gradle to add sub-projects.

include 'model'
include 'repository','web'

Settings API

Before Gradle assembles its build, it creates an instance of type settings. The interface Settings is a direct representation of settings.gradle. Its main purpose is to add Project instance that is supposed to participate in the multiproject build.

Settings execution

What phase does the code of the settings file is evaluated and executed? During the initialization before any Project instance is configured.
How to find a settings file? There are two steps.

  1. Gradle searches for a settings file in a directory called master with the same nesting level as the current directory.
  2. If no settings file is found in step 1, Gradle searches for a settings file in the parent directories, starting from the current directory. In the case of subproject2, the search would be suproject1 > root.
    Step2 is useful for a hierarchical project.
Hierachical VS Flat

It is recommended that you use the hierarchical project.

Configuring sub-projects

You can define common behavior for all projects or all sub-projects.
You can define the order of projects while evaluation, whose default order is alphanumeric.
You can declare project-specific code.

Define specific behavior

Project-specific behavior is defined with the project.

ext.projectIds = ['group': 'com.manning.gia', 'version': '0.1']
// Declaration of extra property projectIds as a map that holds the key-value pairs for group and version; property can be used in subprojects
group = projectIds.group
version = projectIds.version
project(':model') {
   group = projectIds.group
   version = projectIds.version
   apply plugin: 'java'
}
project(':repository') {
   group = projectIds.group
   version = projectIds.version
   apply plugin: 'java'
}
project(':web') {
   group = projectIds.group
   version = projectIds.version
   apply plugin: 'java'
   apply plugin: 'war'
   apply plugin: 'jetty'
   repositories {
      mavenCentral()
}
// Configures each subproject by project path; actual configuration happens in the closure
   dependencies {
      providedCompile 'javax.servlet:servlet-api:2.5'
      runtime 'javax.servlet:jstl:1.1.2'
   } 
}

If you run gradle build for repository, you will end up with a compile error, for repository project depends on model project. You need to declare the dependation between them.

Define project dependencies

project(':model') {
   ...
}
project(':repository') {
   ...
   dependencies {
     compile project(':model')
   }
}
project(':web') {
   ...
   dependencies {
      compile project(':repository')
      providedCompile 'javax.servlet:servlet-api:2.5'
      runtime 'javax.servlet:jstl:1.1.2'
} }

Partial builds

If you execute a task from the root project, it will run all the build code.
If you want to run a partial build, you can use a command-line option -a or --no-rebuild. With the parameter -a, you will avoid the cost of checking dependency projects.
There are some other arguments.

Declare cross-project task dependencies

Default task execution order: the task on the root level of the multi projects build is always executed first. For the subprojects, the execution order is solely determined by the alphanumeric order of the names of the projects: model comes before repository.
You can use dependsOn to change the order.

Define common behaviors

You can use all projects method to define common behaviors for all projects, you can use subprojects to define behaviors for all sub projects.
/ROOT_PATH/build.gradle

allprojects {
   group = 'com.manning.gia'
   version = '0.1'
}
subprojects {
   apply plugin: 'java'
}
project(':repository') {
   dependencies {
      compile project(':model')
   }
}
project(':web') {
   apply plugin: 'war'
   apply plugin: 'jetty'
   repositories {
      mavenCentral()
}
   dependencies {
      compile project(':repository')
      providedCompile 'javax.servlet:servlet-api:2.5'
      runtime 'javax.servlet:jstl:1.1.2'
} }

You can also add specific project configurations in the project's own build.gradle file.

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子拗小,更是在濱河造成了極大的恐慌削咆,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件欧聘,死亡現(xiàn)場離奇詭異忍捡,居然都是意外死亡形葬,警方通過查閱死者的電腦和手機擦秽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人感挥,你說我怎么就攤上這事缩搅。” “怎么了触幼?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵硼瓣,是天一觀的道長。 經(jīng)常有香客問我置谦,道長堂鲤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任媒峡,我火速辦了婚禮瘟栖,結果婚禮上,老公的妹妹穿的比我還像新娘谅阿。我一直安慰自己半哟,他們只是感情好,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布签餐。 她就那樣靜靜地躺著寓涨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪氯檐。 梳的紋絲不亂的頭發(fā)上戒良,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天,我揣著相機與錄音冠摄,去河邊找鬼糯崎。 笑死,一個胖子當著我的面吹牛耗拓,可吹牛的內(nèi)容都是我干的拇颅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼乔询,長吁一口氣:“原來是場噩夢啊……” “哼樟插!你這毒婦竟也來了?” 一聲冷哼從身側響起竿刁,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤黄锤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后食拜,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鸵熟,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年负甸,在試婚紗的時候發(fā)現(xiàn)自己被綠了流强。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痹届。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖打月,靈堂內(nèi)的尸體忽然破棺而出队腐,到底是詐尸還是另有隱情,我是刑警寧澤奏篙,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布柴淘,位于F島的核電站,受9級特大地震影響秘通,放射性物質(zhì)發(fā)生泄漏为严。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一肺稀、第九天 我趴在偏房一處隱蔽的房頂上張望第股。 院中可真熱鬧,春花似錦盹靴、人聲如沸炸茧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽梭冠。三九已至,卻和暖如春改备,著一層夾襖步出監(jiān)牢的瞬間控漠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工悬钳, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留盐捷,地道東北人。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓默勾,卻偏偏與公主長得像碉渡,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子母剥,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容