Home > Software design >  Spring Boot ignores resources in multi module project
Spring Boot ignores resources in multi module project

Time:07-14

I have a repository with two modules (search and usermanagement). I want both modules to have their own application.properties. Currently I am just trying to activate a profile, but it is not applied. Project structure:

├───pom.xml
├───search
│   ├───src
│   │   ├───main
│   │   │   ├───java
│   │   │   │   └───com
│   │   │   │       └───example
│   │   │   │           └───projectname
│   │   │   │               └───search
│   │   │   │                   ├───controller
│   │   │   │                   ├───model
│   │   │   │                   ├───query
│   │   │   │                   └───Main.class
│   │   │   └───resources
│   │   └───test
│   │       └───src
│   ├───pom.xml
│   └───target
├───target
└───usermanagement
    ├───src
    │   └───com
    │       └───example
    │           └───projectname
    │               └───usermanagement
    ├───test
    │   └───src
    └───pom.xml

The resources directory of search has an application.properties with following content:

spring.profiles.active=dev

This is not used. I have to manually update my run profile and pass spring.config.additional-location with the absolute path to application.properties.

pom of parent

    <dependencyManagement>
        <dependencies>
            <!-- Springboot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.7.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.7.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.1</version>
            <type>pom</type>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test</testSourceDirectory>
        <defaultGoal>clean test install</defaultGoal>
    </build>
    <modules>
        <module>search</module>
        <module>usermanagement</module>
    </modules>

pom of search child

    <dependencies>
        <!-- Springboot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Build jar with dependencies -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>main.java.com.example.projectname.search.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

I've tried adding src/main/resources to parent and @PropertyScan.

CodePudding user response:

I think the issue could be resolved by making a third module, and naming it "Core", this module would contain all code that both extending modules (search and user management) would share.

This "Core" module

  1. DOES NOT have the capability to build independently
  2. DOES NOT contain a Spring Boot application.run command of any sort
  3. DOES NOT have a application.properties file

The "Core" Module is then imported into both subprojects where any linked component and/or services can be loaded through the

@SpringBootApplication(scanBasePackages = "*insert package name here*")

annotation. Both extending modules would have their own application.properties file, have their own independent app starters and compile "core" and themselves.

It is important to note that all modules should have a similar package structure for the easiest implementation.

Example:

Core = "com.epicapp.core"

Search = "com.epicapp.search"

User Management = "com.epicapp.usermanagement"

Base Package = "com.epicapp"

CodePudding user response:

I was able to resolve my problem by adding this to the parent pom

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  • Related