gradle构建springboot 多模块项目

一、根项目创建

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'
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值