Multi engineering construction
Maven realizes the combination of multiple modules by using modules. In Gradle, it can be realized by aggregating multiple project s. This article mainly introduces the specific usage.
Multi engineering structure
Create a three-tier structure as follows
graph TD helloProject --> subProjectA helloProject --> subProjectB subProjectA --> subProjectAA
Each project has the following four tasks:
graph LR Compile: compile -- > test: Test Test: Test > packaging Packaging: packaging > installation: install
Preparation beforehand
Based on the previous examples, the related settings and build information is as follows:
- settings.gradle
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$
- Executive confirmation
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$
Preparation for multi project construction
Create three directories of subProjectA subProjectA/subProjectAA subProjectB, and then copy build.gradle into them respectively to finish the demonstration preparation.
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$
In this way, four tasks under each project have been prepared:
graph LR Compile: compile -- > test: Test Test: Test > packaging Packaging: packaging > installation: install
- Constitution structure
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
The only setting that needs to be set is settings.xml under rootProject. The statement used is include. Add the following line of information:
include 'subProjectA', 'subProjectB','subProjectA:subProjectAA'
Of course, there are many ways to write. For example, when introducing a three-tier structure, you can use the following methods: subjecta: subjectaa. There is no additional development here. In this way, only one line has been added to complete the three-tier structure association:
graph TD helloProject --> subProjectA helloProject --> subProjectB subProjectA --> subProjectAA
Result confirmation
Using gradle projects, you can clearly see the project composition of the three layers:
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$ ]
You can also view the details of a sub project
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$
Execution Construction
- Perform a rootProject build You can see that when you build the root directory, all the other parts will be built together
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$
- Construction of execution sub project You can also use gradle to specify subprojects to build, such as performing the tasks of the subprojects in layer 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$
summary
In gradle, the structure association is realized by a simple include statement. Of course, compared with the introduction of parent-child relationship, the implementation of this method may have advantages and disadvantages in the aspects of integration relationship and influence range of modification. This paper is mainly used to introduce the composition of multi-layer modules under gradle. Although the N-layer can be used for composition, whether to use it carefully is recommended Users can experience it in the project.