首页 > 解决方案 > Spring Boot gradle 多模块应用

问题描述

我有一个带有多个模块的 Spring Boot 应用程序(gradle),但是在构建应用程序时遇到了问题。下面是我的gradle

build.gradle(主应用程序)

plugins {
    id 'org.springframework.boot' version '2.2.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'net.sagati'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compile project(':my-pet-clinic-data')
    compile project(':my-pet-clinic-web')
}

test {
    useJUnitPlatform()
}

build.gradle - 数据层。第一个模块

plugins {
    id 'java'
}

group 'net.sagati'
version '0.0.1-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
}
test {
    useJUnitPlatform()
}
bootJar {
    enabled = false
}

jar {
    enabled = true
}

build.gradle - web 层第二个模块

plugins {
    id 'java'
}

group 'net.sagati'
version '0.0.1-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
}
test {
    useJUnitPlatform()
}
bootJar {
    enabled = false
}

jar {
    enabled = true
}

********************错误信息 **************************** ************

A problem occurred evaluating project ':my-pet-clinic-data'.
> Could not find method bootJar() for arguments [build_iptt1011gxtkxqsop7ht18yc$_run_closure4@45139c47] on project ':my-pet-clinic-data' of type org.gradle.api.Project.

任何帮助,将不胜感激。

更新

删除后

bootJar {
    enabled = false
}

我得到以下错误

Execution failed for task ':my-pet-clinic-data:compileJava'.
> Could not resolve all files for configuration ':my-pet-clinic-data:compileClasspath'.
   > Could not find org.projectlombok:lombok:.
     Required by:
         project :my-pet-clinic-data
   > Could not find org.springframework.boot:spring-boot-starter-data-jpa:.
     Required by:
         project :my-pet-clinic-data

标签: javaspring-bootgradle

解决方案


更新的问题是由于没有任何版本的依赖项引起的。

在应用 Spring boot Gradle 插件时,它会应用 Spring 依赖管理插件并配置 Spring boot BOM。这在文档中进行了解释。

但是,您的数据和 Web 项目仅应用java插件,因此不支持 BOM。我建议查看原生 Gradle BOM 支持。可以在上面链接的 Spring 文档第 3.2 节中找到另一种具有性能成本的替代方法。


推荐阅读