A sample program has two modules:
The core module contains some common components that can be used by other modules of the program. Example, contains only one class: The Messageservice class returns ' Hello world! ' String. The module has two dependencies: Junit 4.11 and Commons-lang3.
The app module contains the HelloWorld class, which is the beginning of the program, which obtains information from the Messageservice object and writes the received information to a log file. The module has two dependencies: it requires a core module, and log4j is used as a log.
Below, we will create a multi-project Gradle build, including two sub-projects: app and Core. The first thing must be to create the project Multiproject root directory.
1. Create a directory of modules in the root directory and configure the Build.gradle file
The first thing you need to do is create a directory of two modules: app, core. Create the Build.gradle file in the corresponding directory and configure it separately:
Apply plugin: ' java '
repositories {
mavencentral ()
}
Task ' Mkdirs ' << {
sourcesets*. Java.srcdirs*.each {it.mkdirs ()}
Sourcesets*.resources.srcdirs*.each {it.mkdirs ()}
}
Running the Gradle mkdirs task automatically creates a good src directory.
2. Configuring projects that are included in a multi-project build
You can configure projects that are included in a multi-project build by following these steps: Create a Settings.gradle file at the root of the root project, which indicates which projects are included in a multi-project build.
The Settings.gradle configuration is as follows:
Rootproject.name = ' multiproject '
include ' app ', ' core '
3. Configuring the Build.gradle of the core module
Apply plugin: ' java '
repositories {
mavencentral ()
}
dependencies {
compile ' org.apache.commons : commons-lang3:3.4 '
testcompile group: ' JUnit ', Name: ' JUnit ', Version: ' 4.11 '
}
Task ' Mkdirs ' << { C11/>sourcesets*.java.srcdirs*.each {it.mkdirs ()}
Sourcesets*.resources.srcdirs*.each {it.mkdirs ()}
}
4. Configure the Build.gradle of the app module
First of all, if a multi-project build has projects A and B, and Project B is compiled with Project A, you can rely on configuration by adding the following dependency declarations to the Build.gradle file in Project B. dependencies {Compile project (: A)}
So the Build.gradle file for the app project is as follows:
Apply plugin: ' java '
repositories {
mavencentral ()
}
dependencies {
compile ' log4j:log4j : 1.2.17 '
Compile project (': Core ')
testcompile Group: ' JUnit ', Name: ' JUnit ', Version: ' 4.11 '
}
jar { From
{
configurations.compile.collect {
it.isdirectory ()? It:ziptree (IT)
}
}
manifest {
attributes ' Main-class ': ' Org.zero.HelloWorld '
}
}
Run the task Gradle build directly, in the app directory build/libs/will generate App.jar executable jar file, Java–jar App.jar can run the app.
5. Remove duplicate Configurations
When you configure a subproject in a multi-project build, a duplicate configuration is added to the build script for the core and app projects. Both use the Java plugin, use the Maven2 central repository, and so on.
This configuration can be transferred to the root project's Build.gradle file, before which you must first learn how to configure the subproject in the root project's Build.gradle file.
If you want to add a configuration to a subproject called core, you must add the following fragment to the root project's Build.gradle file:
Project (: Core) {
//add core specific configuration here
}
In other words, if you want to transfer the duplicate configuration to the build script of the root project, you must add the following configuration to the Build.gradle file:
Project (: app) {
apply Plugin:java;
repositories {
mavencentral ()
}
}
project (: Core) {
apply Plugin:java;
repositories {
mavencentral ()
}
}
However, this practice does not change anything in essence, there is still a duplicate configuration in the build script, the only difference being that the duplicate configuration is now transferred to the root project's Build.gradle file.
If you want to add a common configuration to the subproject of the root project, you need to add the following fragment to the root project's Build.gradle file:
subprojects {
//add common configuration here
}
So the Build.gradle file in the root project can be modified like this:
subprojects {
Apply plugin: ' java '
repositories {
mavencentral ()
}
dependencies {
Testcompile group: ' JUnit ', Name: ' JUnit ', Version: ' 4.11 '
}
Task ' Mkdirs ' << {
sourcesets*. Java.srcdirs*.each {it.mkdirs ()}
Sourcesets*.resources.srcdirs*.each {it.mkdirs ()}
}
}
At this point, the app and core will have the same configuration as the subprojects content.
If a configuration item is shared by all projects in a multi-project build, you can also add the following fragment to the root project's Build.gradle file:
allprojects {
//add configuration here
}
Reference: http://blog.jobbole.com/84471/