Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

Is it possible to load Spring-Boot properties from config folder within parent module of a Maven multi-module project?

Is it possible to load multiple Spring-Boot .yml config files from a config folder within parent module of a multi-module project?

So, structure looks like this:

parent-module/
  pom.xml
  config/
    application-prd.yml
    application-dev.yml
  module1
    pom.xml
    src/main/resources/
      logback-spring.xml
      bootstrap.yml

Is this possible? How can it be done?

So, if I execute from root folder of multi-module project, I would use this command:

mvn -pl module1 spring-boot:run
   OR
mvn spring-boot:run

And I would hope that the config folder would be included in the classpath? I am trying to do this but not getting it to work. Am I missing something?

We know this to be true: Child POMs inherit properties, dependencies, and plugin configurations from the parent. But shouldn't that mean that {parent}/config/application.yml is in the classpath already?

Example project to use for proving: https://github.com/djangofan/spring-boot-maven-multi-module-example . Clone it and modify if you think you can solve it.

question from:https://stackoverflow.com/questions/65943201/is-it-possible-to-load-spring-boot-properties-from-config-folder-within-parent-m

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No need to write code. You can use Exec Maven Plugin for this. Add the plugin to the parent module:

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

then run mvn install once and then mvn exec whenever you want to start the application.

mvn exec:java -pl module1

For more on Maven goals in a multi-module project, check this answer https://stackoverflow.com/a/11091569/512667 .

Another way to configure this is like so, which requires the 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>

In this case, execute:

mvn spring-boot:run -pl module1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...