是否可以从 config
加载多个 Spring-Boot .yml 配置文件?多模块项目的父模块中的文件夹?
因此,结构如下所示:
parent-module/
pom.xml
config/
application-prd.yml
application-dev.yml
module1
pom.xml
src/main/resources/
logback-spring.xml
bootstrap.yml
这可能吗?如何做呢?所以,如果我从多模块项目的根文件夹执行,我会使用这个命令:
mvn -pl module1 spring-boot:run
OR
mvn spring-boot:run
我希望 config
文件夹将包含在类路径中?我正在尝试这样做,但没有让它发挥作用。我错过了什么吗?我们知道这是真的:
Child POMs inherit properties, dependencies, and plugin configurations from the parent.
但这不应该意味着{parent}/config/application.yml
已经在类路径中了吗?用于证明的示例项目:https://github.com/djangofan/spring-boot-maven-multi-module-example .如果您认为可以解决,请克隆并修改。
最佳答案
无需编写代码。您可以为此使用 Exec Maven 插件。将插件添加到父模块:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>PACKAGE.MODULE_MAIN_CLASS</mainClass>
<arguments>
<argument>--spring.profiles.active=dev</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
然后运行 mvn install
一次又一次mvn exec
每当您想启动应用程序时。mvn exec:java -pl module1
有关多模块项目中 Maven 目标的更多信息,请查看此答案 https://stackoverflow.com/a/11091569/512667 .另一种配置方法是这样,它需要
workingDirectory
参数:<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.Application</mainClass>
<workingDirectory>${maven.multiModuleProjectDirectory}</workingDirectory>
<arguments>
<argument>--spring.profiles.active=dev</argument>
</arguments>
</configuration>
</plugin>
在这种情况下,执行:mvn spring-boot:run -pl module1
关于spring-boot - 是否可以从 Maven 多模块项目的父模块中的配置文件夹加载 Spring-Boot 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65943201/