0

I have a multi module Spring Boot Application with following folder structure

MyProject/
|-pom.xml
|-Module1/
|--pom.xml
|--src/
|---main/
|----java/
|-----module-info.java
|-----com.project.module1/
|----resources/
|-----module1-application.properties
|-----module1-application-dev.properties
|-----module1-application-prod.properties
|---test/
|----java/
|-----com.project.module1/
|----resources/
|-----module1-application-test.properties
|-Module2/
|--pom.xml
|--src/
|---main/
|----java/
|-----module-info.java
|-----com.project.module2/
|----resources/
|-----module2-application.properties
|-----module2-application-dev.properties
|-----module2-application-prod.properties
|---test/
|----java/
|-----com.project.module2/
|----resources/
|-----module2-application-test.properties
|-Main/
|--pom.xml
|--src/
|---main/
|----java/
|-----module-info.java
|-----com.project.main/
|------Main.java
|----resources/
|-----main-application.properties
|-----main-application-dev.properties
|-----main-application-prod.properties
|---test/
|----java/
|-----com.project.main/
|----resources/
|-----main-application-test.properties

And my main class looks like

@SpringBootApplication(scanBasePackages = {"com.project"})
@EntityScan(basePackages = {
        "com.project.module1.models",
        "com.project.module2.models"
})
@EnableJpaRepositories(basePackages = {
        "com.project.module1.repositories",
        "com.project.module2.repositories"
})
public class Main {
    public static void main(String[] args){
        //Setting up multiple spring profiles
        new SpringApplicationBuilder(Main.class)
                .properties(
                        "spring.config.name" +
                                ":main-application" +
                                ",module1-application" +
                                ",module2-application"
                )
                .build().run(args);
    }
}

Application runs fine and reads the properties file as expected. Issue arises when I have to run commands like flyway migration or tests which do not access Main class and hence fail to load migration related properties.

Is there any way I can define base properties class in individual module's pom.xml?

I have come across some solutions that requires defining different properties for different profiles, but that is too much of configuration, I am currently looking for a solution as simple as defining spring.config.name with base properties name in individual pom.xml

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • Stick an `application.yaml` in `src/test/resources` surely? – Boris the Spider Jul 27 '19 at 06:39
  • Can be put to main/resources too if required. But isolated in tests is preferred. And we are using `.properties` instead of `yaml` @BoristheSpider – Mohammed Atif Jul 27 '19 at 06:43
  • Any reason behind that? What I read is each has their own pros and cons. For my use case properties were better as they provide more fine tuned control in modular projects. @BoristheSpider – Mohammed Atif Jul 27 '19 at 06:47
  • 1
    Properties cannot possibly provide more "fine tuned control" as they are indetical in terms of the backing config classes and therefore configurable values. Properties are an arcane format from the dawn of time that is unstructured, impossible to read and doesn't support profiles. You have to scan down the entire file to find all the e.g. `spring.web.server.ssl` properties just in case someone didn't _manually_ group them and even then you miss one. What happens when there are duplicates? No errors of course - just one is picked at random. Properties are awful in every possible way. – Boris the Spider Jul 27 '19 at 07:08

0 Answers0