如何在 Spring 引导应用程序中包含其他模块的资源
How to include resources of other modules in a Spring Boot application
我正在使用 Spring Boot 创建一个多模块项目。项目的结构是这样的:
-configuration
|--- src
|--- main
|--- java
|--- resources
|--- build.gradle
-module1
|--- src
|--- main
|--- java
|--- resources
|--- build.gradle
-module2
|--- src
|--- main
|--- java
|--- resources
|--- build.gradle
build.gradle
settings.gradle
module1是为了应用的持久化,它的resources目录下有文件application.properties包含了配置数据库以及 data.sql 和 schema.sql
当我 运行 应用程序时,资源仅从“configuration”模块加载,而不是从 module1 加载。
我的目标是加载所有模块的所有资源文件夹(因为每个模块都有不同的职责)
你能post你收到的错误吗?
我的猜测是您需要一个 @componentScan
,但看到错误会很有帮助。
我找到了解决我的问题的方法:
在配置模块的build.gradle中我添加了这个任务:
task copyResources(type: Copy) {
println "Copying resource"
from "${project(':module1').buildDir}/resources/main/schema.sql"
into "${buildDir}/resources/test"
from "${project(':module1').buildDir}/resources/main/data.sql"
into "${buildDir}/resources/test"
}
processResources.dependsOn copyResources
这将在构建时将 data.sql 和 schema.sql 从 module1 复制到配置模块。然后当应用程序启动时(从配置模块)它将使用那些复制的文件,因为它们位于其资源文件夹中。
我正在使用 Spring Boot 创建一个多模块项目。项目的结构是这样的:
-configuration
|--- src
|--- main
|--- java
|--- resources
|--- build.gradle
-module1
|--- src
|--- main
|--- java
|--- resources
|--- build.gradle
-module2
|--- src
|--- main
|--- java
|--- resources
|--- build.gradle
build.gradle
settings.gradle
module1是为了应用的持久化,它的resources目录下有文件application.properties包含了配置数据库以及 data.sql 和 schema.sql
当我 运行 应用程序时,资源仅从“configuration”模块加载,而不是从 module1 加载。 我的目标是加载所有模块的所有资源文件夹(因为每个模块都有不同的职责)
你能post你收到的错误吗?
我的猜测是您需要一个 @componentScan
,但看到错误会很有帮助。
我找到了解决我的问题的方法:
在配置模块的build.gradle中我添加了这个任务:
task copyResources(type: Copy) {
println "Copying resource"
from "${project(':module1').buildDir}/resources/main/schema.sql"
into "${buildDir}/resources/test"
from "${project(':module1').buildDir}/resources/main/data.sql"
into "${buildDir}/resources/test"
}
processResources.dependsOn copyResources
这将在构建时将 data.sql 和 schema.sql 从 module1 复制到配置模块。然后当应用程序启动时(从配置模块)它将使用那些复制的文件,因为它们位于其资源文件夹中。