如何解决是否可以从 Maven 多模块项目的父模块内的 config 文件夹加载 Spring-Boot 属性?
是否可以从多模块项目的父模块内的 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。如果您认为可以解决,请克隆并修改。
解决方法
是的,这是可能的。
当您使用 -pl
时,Maven 会将工作目录更改为模块的目录。所以它不再是具有 config/
目录的根。
或者您可以重构您的 maven 多模块设置,以便您可以打包常见的 application.yml
文件并使用它们。我不建议这样做,因为它有很多陷阱。
可能更容易使用 --spring.config-location
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
我目前无法对其进行测试,但如果它不起作用,您可以随时尝试使用 -Dspring.config.location
或 SPRING_CONFIG_LOCATION
环境变量。
我发现了一个 customized way of doing it,我仍然认为它是一个 hack,如下所示,但我仍在寻找我的问题的实际答案(如果存在)。
@Slf4j
@SpringBootApplication
public class Application {
private static String DEV_PROPS = "application-dev.properties";
private static String PRD_PROPS = "application-prd.properties";
private static String DEFAULT_PROPS = "application.properties";
private static Path CONFIG = Paths.get(System.getProperty("user.dir"))
.resolve(Paths.get(".."))
.resolve("config");
private static final String EXT_CONFIG_DIR = CONFIG.toString() + File.separator;
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new Resource[] {
new FileSystemResource(EXT_CONFIG_DIR + PRD_PROPS),new FileSystemResource(EXT_CONFIG_DIR + DEV_PROPS),new ClassPathResource(DEFAULT_PROPS)
};
log.info("Properties: " + Arrays.deepToString(resources));
properties.setIgnoreResourceNotFound(true);
properties.setLocations(resources);
return properties;
}
}
此方法的问题在于必须更改配置以支持额外的 environment profile names
。
无需编写代码。您可以为此使用 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。