一、根项目创建
1.创建空项目root
2.执行gradle init 初始化gradle配置
settings.gradle
rootProject.name = 'hotel'
include 'core', 'web'
build.gradle
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.ting'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
repositories {
mavenLocal()
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
mavenCentral()
}
jar {
manifest {
attributes("Implementation-Title": "Gradle")
}
}
// dependencies {
// ext.jarTree = fileTree(dir: 'libs', include: '**/*.jar')
// ext.rootProjectLibs = new File(rootProject.rootDir, 'libs').getAbsolutePath()
// ext.jarTree += fileTree(dir: rootProjectLibs, include: '**/*.jar')
// compile jarTree
// }
//
// task listJars(description: 'Display all compile jars.') << {
// configurations.compile.each { File file -> println file.name }
// }
}
二、创建core模块
只保留gradle的build.gradle配置
archivesBaseName = 'core'
jar.enabled = true
bootJar.enabled = false
dependencies {
implementation('org.springframework.boot:spring-boot-starter')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
三、创建web模块
只保留gradle的build.gradle配置
archivesBaseName='web'
//apply plugin:"war"
dependencies {
compile project(':core')
implementation('org.springframework.boot:spring-boot-starter-cache')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.session:spring-session-core')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
//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()+";"
// }
//}
四、问题梳理
1.build时,web编译不通过,错误:web:compileJava FAILED,原因是找不到依赖项目“core”中的package和class
问题分析:
查看build执行过程
> Task :core:compileJava
> Task :core:processResources
> Task :core:classes
> Task :core:bootJar
> Task :core:jar SKIPPED
> Task ........
可以发现前置任务::core:jar 被跳过了。
原因分析:
跟项目的build文件中,buildscript中依赖的类路径为:
org.springframework.boot:spring-boot-gradle-plugin
该插件默认的jar的enabled属性值为false,bootjar.enabled为true,此时任务::core:jar会跳过不执行,而是执行bootjar,bootjar打包的是一个可执行的文件,目录结构和jar不同,造成web编译时未获取到core的package和class就成为无效引用。

解决方案:
在core的build.gradle文件中,配置jar.enabled = true bootJar.enabled = false
关于bootjar和jar请参考springboot官方文档:
https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/gradle-plugin/reference/html/
4.3. Packaging executable and normal archives
By default, when the bootJar or bootWar tasks are configured, the jar or war tasks are disabled. A project can be configured to build both an executable archive and a normal archive at the same time by enabling the jar or war task:
jar {
enabled = true
}
To avoid the executable archive and the normal archive from being written to the same location, one or the other should be configured to use a different location. One way to do so is by configuring a classifier:
bootJar {
classifier = 'boot'
}
|