我有两个模块,第一个运行Spring boot Application,第二个是EventListener,它在上下文启动时从资源中加载文件。所有这些模块都单独工作得很好,但我希望在第一个模块(Spring boot模块)中包含事件侦听器模块,以便在第一个模块运行上下文时从第一个模块的资源中获取所有文件。
我使用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()
}
和我的eventListener:
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构建文件中遗漏了一些东西。
发布于 2019-12-06 01:21:39
我只会在前面说我不知道下面的东西是不是你问题的真正原因。但是,无论如何,您可能应该更改一些与jar配置相关的内容。
Spring Boot Gradle插件用于在项目之外创建一个胖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,同时创建一个普通的jar。其中一个将覆盖另一个。在这种情况下,您可能不需要普通的jar,因此您应该再次禁用jar
任务:
// eventListener/build.gradle
// jar.enabled = true <-- Remove this
最后,将compile project(":eventListnere")
替换为implementation project(":eventListener")
,将testCompile
替换为testImplementation
,以避免出现一些不推荐使用的警告。为了支持maven-publish
,maven
插件也遭到了抨击。除非您正在与您自己用mvn install
构建的本地mavenLocal()
项目集成,否则您可能也可以摆脱Maven。
如果将eventListener
正确打包为bootApplication
的fat jar中的普通jar,则在运行resource
文件夹时,它应该能够访问自己的bootApplication
文件夹中的资源。
https://stackoverflow.com/questions/59194735
复制相似问题