java - Gradle 多模块。使用来自另一个模块的 EventListener 检查第一个模块的上下文

标签 java spring spring-boot gradle

我有两个模块,第一个运行 Spring boot 应用程序,第二个是 EventListener,它在上下文启动时从资源加载文件。所有这些模块单独运行良好,但我想将事件监听器模块包含到我的第一个模块(Spring boot 模块)中,以便在运行上下文时从我的第一个模块的资源中获取所有文件。

我的主模块与setting.gradle:

allprojects {
    buildDir = file("${rootDir}/build")
    group = 'com.example'
    version =  "0.1.1"
}
subprojects {
    apply plugin: 'java'
    apply plugin: 'maven'
}

设置.gradle

rootProject.name = 'test-application'

include 'bootApplication'
include 'eventListener'
project(":eventListener").projectDir = file("C:/examples/eventListener")

我的bootApplication.gradle:

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

group 'com.example.bootApplication'
version =  "0.1.1"
sourceCompatibility = '11'
targetCompatibility = '11'

repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
}

bootJar {
    baseName("bootApplication")
}

jar {
    enabled = true
}

dependencies {
    compile project(":eventListnere")
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.springfox:springfox-swagger2:+'
    implementation 'io.springfox:springfox-swagger-ui:+'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

还有我的事件监听器:

plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'`enter code here`
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group 'com.example.eventlistener'
version '0.0.1-SNAPSHOT'

sourceCompatibility = '11'
targetCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    ext {
        spring_boot_version = '2.2.1.RELEASE'
    }
    implementation "org.springframework.boot:spring-boot-starter:$spring_boot_version"
    compileOnly 'org.projectlombok:lombok:1.18.8'
    annotationProcessor 'org.projectlombok:lombok:1.18.8'

    testImplementation "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"


    testCompile group: 'junit', name: 'junit', version: '4.12'
}


jar.enabled = true

当我运行 bootApplication 主类时,它会在根构建目录中创建一个 eventlistener-.jar 文件。但事件监听器模块不会检查资源文件夹,我猜它没有看到 bootApplication 上下文。也许应该将其收集到一个 jar 文件中?看来我错过了 gradle 构建文件中的某些内容。

最佳答案

我只是在前面说我不知道​​下面的内容是否是您问题的实际原因。但无论如何,您可能应该更改一些与 jar 配置相关的内容。

Spring Boot Gradle 插件用于在项目之外创建一个 fat jar。默认情况下,它会禁用正常的 jar 任务。

您正在通过 jar.enabled = true 重新启用正常的 jar 任务,这很好。但您还需要为其指定另一个名称,否则一个名称将覆盖另一个名称。例如,对于您的 eventListener 项目,您可以执行以下操作:

// eventListener/build.gradle
bootJar {
    classifier = 'boot'
}

但是,如果 eventListener 实际上不是独立的可执行文件,则无需从中创建启动 jar。因此,除非您将该插件用于其他用途,否则我会将其从 eventListener 中完全删除:

// eventListener/build.gradle
plugins {
    // id 'org.springframework.boot' version '2.2.1.RELEASE'`enter code here` <-- Remove this
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

您仍然可以在项目中使用 Spring Boot starters,只是不需要重新打包 jar 的插件。

同样的事情也适用于您的 bootApplication 项目:您都试图在创建普通 jar 的同时创建一个 fat 可执行 jar。其中一个将凌驾于另一个之上。在这种情况下,您可能不需要普通的 jar,因此您应该再次禁用 jar 任务:

// eventListener/build.gradle
// jar.enabled = true <-- Remove this

最后,将 compile project(":eventListnere") 替换为 implementation project(":eventListener") 并将 testCompile 替换为 testImplementation 以避免出现一些弃用警告。 maven 插件也已被弃用,取而代之的是 maven-publish。您或许还可以摆脱 mavenLocal() ,除非您要与使用 mvn install 自己构建的本地 Maven 项目集成。

如果将 eventListener 正确打包为 bootApplication 的 fat jar 内的普通 jar,则应该能够访问其自己的 resource 文件夹中的资源以及运行后者时 bootApplication 中的资源。

关于java - Gradle 多模块。使用来自另一个模块的 EventListener 检查第一个模块的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59194735/

相关文章:

java - thymeleaf : Getting value from a map while iterating through a list

java - 2 字节 UTF-8 序列的无效字节 2

java - 启动 Tomcat 时 Web 模块没有从核心模块中找到类

java - Spring Boot 扩展 CrudRepository

java - Spring 启动 : Disable security for Spring Boot Unit Test

java - 分割字符串以获取坐标/点

java - cordapp-template-java "Cannot resolve symbol ' 文档'"

Java Hibernate/Spring,做部分匹配查询 ("contains")?

java - Spring mvc @RequestBody 字符串格式

spring-boot - 无论如何我们可以使用 Spring Cloud Functions + AWS Lambda 适配器来处理 http 文件上传吗?