Usually when I build a project using Maven, I divide the application project into multiple smaller modules.
Gradle projects also have more than one component, which we also call multi-project builds.
We first create a multi-project build:
mkdir cmdGradleProj && cd cmdGradleProjgradle init
At this time
D:/cmdGradleProj> Execute in the directory: tree /f
The project structure is as follows:
│ build.gradle│ gradlew│ gradlew.bat│ settings.gradle│ ├─.gradle│ └─3.0│ └─taskArtifacts│ cache.properties│ cache.properties.lock│ fileHashes.bin│ fileSnapshots.bin│ fileSnapshots.bin│ fileSnapshotsToTreeSnapshotsIndex.bin│ taskArtifacts.bin│ └─gradle └─wrapper gradle-wrapper.jar gradle-wrapper.properties
Then, create multiple modules. Here, take core and web modules as examples, and first create four directories (test folders are used to write test classes):
mkdir core/src/main/javamkdir core/src/main/testmkdir web/src/main/javamkdir web/src/main/resources
core module: contains some common components that can be used by other modules of the program. In the example, there is only one class: the MessageService class returns the 'Hello World!' string. This module has two dependencies: Junit 4.11 and commons-lang3.
web module: The module contains the HelloWorld class, which is the beginning of the program. It obtains information from the MessageService object and writes the received information into a log file. The module has two dependencies: it requires the core module and also uses Log4j as the log.
Now that we have created the required directory, the next step is to configure the Gradle build, first configuring the projects included in the multi-project build.
We can configure projects included in multi-project builds through the following steps:
1. Create settings.gradle file in the root directory of the root project. A multi-project Gradle build must contain this file because it indicates those projects included in the multi-project build.
2. Ensure that web and core projects are included in our multi-project builds.
Our settings.gradle file is as follows:
include 'core'
include 'web'
Abbreviation: include 'core','web'
Modify build.gradle in the root directory:
// General configuration of all subprojects subprojects { apply plugin: 'java' // apply plugin: 'eclipse' apply plugin: 'idea' version = '1.0' // JVM version number requires sourceCompatibility = 1.8 targetCompatibility = 1.8 // When java is compiled, it will fail due to Chinese characters by default [compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8' //Define version number ext { springVersion = '4.3.3.RELEASE' hibernateVersion='5.2.2.Final' } repositories { mavenCentral() } jar { manifest { attributes("Implementation-Title": "Gradle") } } configurations { // All packages that need to be ignored are defined here all*.exclude group: 'commons-httpclient' all*.exclude group: 'commons-logging' all*.exclude group: 'commons-beanutils', module: 'commons-beanutils' } dependencies { // General dependency compile( "org.springframework:spring-context:$springVersion", "org.springframework:spring-orm:$springVersion", "org.springframework:spring-tx:$springVersion", "org.springframework.data:spring-data-jpa:1.10.3.RELEASE", "org.hibernate:hibernate-entitymanager:$hibernateVersion", "c3p0:c3p0:0.9.1.2", "mysql:mysql-connector-java:6.0.4", "org.slf4j:slf4j-nop:1.7.21", "commons-fileupload:commons-fileupload:1.3.2", "com.fasterxml.jackson.core:jackson-databind:2.8.2" ) // Depend on jar that does not exist in maven ext.jarTree = fileTree(dir: 'libs', include: '**/*.jar') ext.rootProjectLibs = new File(rootProject.rootDir, 'libs').getAbsolutePath() ext.jarTree += fileTree(dir: rootProjectLibs, include: '**/*.jar') compile jarTree // Test dependency testCompile( "org.springframework:spring-test:$springVersion", "junit:junit:4.12" ) } // Display all jars for compile under the current project. task listJars(description: 'Display all compile jars.') << { configurations.compile.each { File file -> println file.name } }}Next, you can modify core/build.gradle to define the dependencies of the core module:
// The name of the jar package archivesBaseName = 'core'// Other configurations can also be defined. Here, the configuration in the parent module directly inherits the configuration of the web module needs to depend on the core module, so the web/build.gradle is defined as follows: apply plugin:"war" dependencies{ // Dependency core module compile project(":core") compile( "org.springframework:spring-webmvc:$springVersion", "org.apache.taglibs:taglibs-standard-impl:1.2.1" ) // The dependency provided by the system provides providedCompile( "javax.servlet:javax.servlet-api:3.1.0", "javax.servlet.jsp:jsp-api:2.2.1-b03", "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1" ) } task jarWithoutResources(type: Jar) { baseName project.name from("$buildDir/classes/main") } war{ dependsOn jarWithoutResources from("$projectDir/src/main/resources") { include "*.properties" into("WEB-INF/classes") } classpath=classpath - sourceSets.main.output classpath fileTree(dir:libsDir, include:"${project.name}-${version}.jar") } task('jarPath')<<{ configurations.runtime.resolve().each { print it.toString()+";" } println(); }3. Compile the project
View all jars:
> gradle listJars// View the dependencies of each module: > gradle :core:dependencies> gradle :web:dependencies//Compile all modules: > gradle build
Compare the directory at this time as follows:
│ build.gradle│ gradlew│ gradlew.bat│ settings.gradle│ ├──.gradle│ └───3.0│ └───taskArtifacts│ cache.properties│ cache.properties.lock│ fileHashes.bin│ fileSnapshots.bin│ fileSnapshotsToTreeSnapshotsIndex.bin│ taskArtifacts.bin│ taskArtifacts.bin│ ├───core│ │ build.gradle│ │ │ ├───build│ │ ├───libs│ │ │ core-1.0.jar│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └───tmp│ │ � └───jar│ │ MANIFEST.MF│ │ │ └───src│ ├───main│ │ └───java│ └───test│ └──java│ └───java│ └───java│ └───java│ └────java│ └────jar│ └────jarWithoutResources │ │ MANIFEST.MF │ │ └───war │ MANIFEST.MF │ └────src └───main ├───java └────resources
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.