Gradle这篇文章就够用了05多工程构建

编程

多工程结构

创建如下所示3层结构

graph TD

helloProject --> subProjectA

helloProject --> subProjectB

subProjectA --> subProjectAA

每个project下均有如下四个任务:

graph LR

编译:compile --> 测试:test

测试:test --> 打包:packaging

打包:packaging --> 安装:install

事前准备

在前文的示例基础上,相关的settings和build信息如下:

orrincn:hello orrin$ cat settings.gradle 

println "[Phase: initialization] : settings executed... "

rootProject.name="helloPorject"

orrincn:hello orrin$

  • build.gradle

orrincn:hello orrin$ cat build.gradle 

println "[phase:configuration] build.gradle ..."

task compile {

group "compile"

description "compile task"

println "[phase:configuration] compile"

doFirst {

println "[phase:execution] compile :doFirst()"

}

}

tasks.create(name: "test",dependsOn: compile) {

group "test"

description "test task"

println "[phase:configuration] test"

doLast {

println "[phase:execution] test:doLast()"

}

}

tasks.create("packaging") {

group "packaging"

description "packaging task"

dependsOn test

enabled true

println "[phase:configuration] packaging"

doLast {

println "[phase:execution] packaging:doLast()"

}

}

class Install extends DefaultTask{

String installObjectName

@TaskAction

void checkObject() {

println "[phase:execution] install:checkObject (${installObjectName})"

}

@TaskAction

void installObject() {

println "[phase:execution] install:installObject (${installObjectName})"

}

}

task install(type: Install) {

group "install"

description "install task"

installObjectName "test.jar"

println "[phase:configuration] install"

doFirst {

println "[phase:execution] install:doFirst()"

}

doLast {

println "[phase:execution] install:doLast()"

}

}

install.dependsOn packaging

install.onlyIf { packaging.enabled }

orrincn:hello orrin$

  • 执行确认

orrincn:hello orrin$ gradle install

[Phase: initialization] : settings executed...

> Configure project :

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Task :compile

[phase:execution] compile :doFirst()

> Task :test

[phase:execution] test:doLast()

> Task :packaging

[phase:execution] packaging:doLast()

> Task :install

[phase:execution] install:doFirst()

[phase:execution] install:installObject (test.jar)

[phase:execution] install:checkObject (test.jar)

[phase:execution] install:doLast()

BUILD SUCCESSFUL in 0s

4 actionable tasks: 4 executed

orrincn:hello orrin$

多项目构建准备

创建subProjectA subProjectA/subProjectAA subProjectB三个目录,然后将build.gradle分别拷贝进去即做完了演示准备。

orrincn:hello orrin$ ls

build.gradle settings.gradle

orrincn:hello orrin$ mkdir -p subProjectA subProjectA/subProjectAA subProjectB

orrincn:hello orrin$ cp build.gradle subProjectA

orrincn:hello orrin$ cp build.gradle subProjectA/subProjectAA/

orrincn:hello orrin$ cp build.gradle subProjectB

orrincn:hello orrin$

这样每个project下有四个任务的准备已经完成:

graph LR

编译:compile --> 测试:test

测试:test --> 打包:packaging

打包:packaging --> 安装:install

  • 构成结构

orrincn:hello orrin$ tree .

.

├── build.gradle

├── settings.gradle

├── subProjectA

│ ├── build.gradle

│ └── subProjectAA

│ └── build.gradle

└── subProjectB

└── build.gradle

3 directories, 5 files

orrincn:hello orrin$

设定settings.gradle

唯一需要设定的是rootProject下的settings.xml,使用到的语句是include,添加如下一行信息:

include "subProjectA", "subProjectB","subProjectA:subProjectAA"

当然,写法可能还有很多种,比如引入3层结构的时候可以使用:subProjectA:subProjectAA等方式,这里就不再额外展开。这样仅仅添加了一行,就已经完成了三层结构的关联:

graph TD

helloProject --> subProjectA

helloProject --> subProjectB

subProjectA --> subProjectAA

结果确认

使用gradle projects可以清楚地看到3层的工程构成状况:

orrincn:hello orrin$ gradle projects

[Phase: initialization] : settings executed...

> Configure project :

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectB

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA:subProjectAA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Task :projects

------------------------------------------------------------

Root project

------------------------------------------------------------

Root project "helloPorject"

+--- Project ":subProjectA"

| --- Project ":subProjectA:subProjectAA"

--- Project ":subProjectB"

To see a list of the tasks of a project, run gradle <project-path>:tasks

For example, try running gradle :subProjectA:tasks

BUILD SUCCESSFUL in 0s

1 actionable task: 1 executed

orrincn:hello orrin$ ]

当然也可以查看某个子工程的详细信息

orrincn:hello orrin$ gradle subProjectA:projects

[Phase: initialization] : settings executed...

> Configure project :

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectB

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA:subProjectAA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Task :subProjectA:projects

------------------------------------------------------------

Project :subProjectA

------------------------------------------------------------

Project ":subProjectA"

--- Project ":subProjectA:subProjectAA"

To see a list of the tasks of a project, run gradle <project-path>:tasks

For example, try running gradle :subProjectA:subProjectAA:tasks

To see a list of all the projects in this build, run gradle :projects

BUILD SUCCESSFUL in 0s

1 actionable task: 1 executed

orrincn:hello orrin$

执行构建

  • 执行rootProject构建

    可以看到对根目录进行构建的时候,其他所有部分也会一同被构建

orrincn:hello orrin$ gradle install

[Phase: initialization] : settings executed...

> Configure project :

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectB

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA:subProjectAA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Task :compile

[phase:execution] compile :doFirst()

> Task :test

[phase:execution] test:doLast()

> Task :packaging

[phase:execution] packaging:doLast()

> Task :install

[phase:execution] install:doFirst()

[phase:execution] install:installObject (test.jar)

[phase:execution] install:checkObject (test.jar)

[phase:execution] install:doLast()

> Task :subProjectA:compile

[phase:execution] compile :doFirst()

> Task :subProjectA:test

[phase:execution] test:doLast()

> Task :subProjectA:packaging

[phase:execution] packaging:doLast()

> Task :subProjectA:install

[phase:execution] install:doFirst()

[phase:execution] install:installObject (test.jar)

[phase:execution] install:checkObject (test.jar)

[phase:execution] install:doLast()

> Task :subProjectB:compile

[phase:execution] compile :doFirst()

> Task :subProjectB:test

[phase:execution] test:doLast()

> Task :subProjectB:packaging

[phase:execution] packaging:doLast()

> Task :subProjectB:install

[phase:execution] install:doFirst()

[phase:execution] install:installObject (test.jar)

[phase:execution] install:checkObject (test.jar)

[phase:execution] install:doLast()

> Task :subProjectA:subProjectAA:compile

[phase:execution] compile :doFirst()

> Task :subProjectA:subProjectAA:test

[phase:execution] test:doLast()

> Task :subProjectA:subProjectAA:packaging

[phase:execution] packaging:doLast()

> Task :subProjectA:subProjectAA:install

[phase:execution] install:doFirst()

[phase:execution] install:installObject (test.jar)

[phase:execution] install:checkObject (test.jar)

[phase:execution] install:doLast()

BUILD SUCCESSFUL in 0s

16 actionable tasks: 16 executed

orrincn:hello orrin$

  • 执行子工程的构建

    也可以通过gradle来指定子工程进行构建,比如执行第3层的子工程的任务

orrincn:hello orrin$ gradle subProjectA:subProjectAA:install

[Phase: initialization] : settings executed...

> Configure project :

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectB

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Configure project :subProjectA:subProjectAA

[phase:configuration] build.gradle ...

[phase:configuration] compile

[phase:configuration] test

[phase:configuration] packaging

[phase:configuration] install

> Task :subProjectA:subProjectAA:compile

[phase:execution] compile :doFirst()

> Task :subProjectA:subProjectAA:test

[phase:execution] test:doLast()

> Task :subProjectA:subProjectAA:packaging

[phase:execution] packaging:doLast()

> Task :subProjectA:subProjectAA:install

[phase:execution] install:doFirst()

[phase:execution] install:installObject (test.jar)

[phase:execution] install:checkObject (test.jar)

[phase:execution] install:doLast()

BUILD SUCCESSFUL in 0s

4 actionable tasks: 4 executed

orrincn:hello orrin$

总结

在gradle中通过简单的include语句就实现了结构的关联,当然相较于父子关系的引入方式,在结构上这种方式的实现可能在集成关系,修改的影响范围等方面各有利弊,本文主要用于介绍gradle下多层模块的构成方式,虽然可以使用N层进行构成,是否使用建议慎重,这方面使用者可在项目中自行体会。

以上是 Gradle这篇文章就够用了05多工程构建 的全部内容, 来源链接: utcz.com/z/515155.html

回到顶部