Spring boot 可以从Maven多模块项目的父模块内的config文件夹加载Spring引导属性吗?

Spring boot 可以从Maven多模块项目的父模块内的config文件夹加载Spring引导属性吗?,spring-boot,maven,maven-3,multi-module,Spring Boot,Maven,Maven 3,Multi Module,是否可以从多模块项目父模块内的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 这可能吗?怎样才能做到呢 因此,如果我

是否可以从多模块项目父模块内的
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
文件夹会包含在类路径中?我正试图这样做,但没有让它发挥作用。我错过什么了吗

我们知道这是真的:
子POM从父级继承属性、依赖项和插件配置。
但这不意味着
{parent}/config/application.yml
已经在类路径中了吗

用于验证的示例项目:。如果您认为可以解决它,请克隆并修改它。

是的,这是可能的

当您使用
-pl
时,Maven将工作目录更改为模块的目录。因此,不再是根目录拥有
config/
dir

或者,您可以重构maven多模块设置,使其能够打包常见的
应用程序.yml
文件,然后使用它们。我不建议这样做,因为它有很多陷阱

可能更容易使用
--spring.config位置

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

我现在不能测试它,但是如果它不起作用,你可以一直尝试<代码> -dSun.COFIG.List< <代码>或<代码> SpRungOpthiLoosix环境变量。

我发现了一个,我仍然认为它是一个黑客,正如你可以看到的,但是我仍然在寻找我的问题的一个实际答案,如果它存在的话。

@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;
    }

}

此方法的问题在于,必须更改配置以支持附加的
环境配置文件名称

无需编写代码。您可以使用execmaven插件来实现这一点。将插件添加到父模块:

<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>
有关多模块项目中Maven目标的更多信息,请查看此答案

另一种配置方法如下,需要
workingDirectory
arg:

<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>

寻找一个不需要以任何方式重写类路径的答案。最简单的命令行。我以前见过这样做,但我不记得他们是怎么做的。根据我的答案定义环境变量或指定系统变量不会覆盖类路径。我的观点是,我相信有一种方法可以做到这一点,而无需在命令行上指定spring.config.location。我知道,因为我看到了它的工作;我只是不知道具体情况。我将致力于制作一个Github示例项目,如果我能自己解决它,我将与大家分享。问题可能是你在哪里看到它起作用的。Afaik intelij idea默认会这样做。发现将
workingDirectory
arg传递到
spring boot maven plugin
修复了我的问题。Spring现在自动扫描我设置工作目录的
/config
文件夹。如果您使用
maven resources plugin
package
阶段将文件从
parent
复制到
module1
,会怎么样?谢谢您的建议。我试过了,但没用。下面是我尝试的分支:@djangofan github URL不可访问性很差,只需将profile参数附加到mvn命令:mvn exec:java-pl spring boot module-Dspring.profiles.active=devYep,就行了。我想我会给你赏金的。非常感谢。此外,spring的“默认概要文件”可以放在属性文件中,而不是命令行中,从而简化了执行。我宁愿使用SpringBootMaven插件,但很高兴知道这个解决方案可以工作。没有问题。你的橡皮鸭子不值得你答应的赏金吗?;-)
<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