【问题标题】:Gradle multi-module. Check context of first module with EventListener from another moduleGradle 多模块。使用来自另一个模块的 EventListener 检查第一个模块的上下文
【发布时间】:2019-12-05 12:02:34
【问题描述】:

我有两个模块,第一个运行 Spring boot Application,第二个是 EventListener,它在上下文启动时从资源加载文件。所有这些模块都可以单独工作,但我想将事件侦听器模块包含到我的第一个模块(Spring 引导模块)中,以便在我的第一个模块运行上下文时从资源中获取所有文件。

我的主模块与 setting.gradle:

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

setting.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 文件。但是 eventlistener 模块不检查资源文件夹,我猜它没有看到 bootApplication 上下文。也许它应该被收集到一个 jar 文件中?看起来我错过了 gradle 构建文件中的某些内容。

【问题讨论】:

    标签: java spring spring-boot gradle


    【解决方案1】:

    我会在前面说我不知道​​下面的内容是否是您的问题的真正原因。但无论如何,您可能应该更改与 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 启动器,只是不需要重新打包 jar 的插件。

    同样的事情也适用于您的bootApplication 项目:你们都试图在创建普通 jar 的同时创建一个胖可执行 jar。一个会覆盖另一个。在这种情况下,您可能不需要普通的 jar,因此您应该再次禁用 jar 任务:

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

    最后,将compile project(":eventListnere") 替换为implementation project(":eventListener"),将testCompile 替换为testImplementation,以避免出现一些弃用警告。 maven 插件也被弃用,而支持maven-publish。除非您与使用 mvn install 构建的本地 Maven 项目集成,否则您可能也可以摆脱 mavenLocal()

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

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2017-03-02
      • 2020-01-26
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      相关资源
      最近更新 更多