Maven Multi Module insists on duplicating datasource application.properties in business module

孤街浪徒 提交于 2019-12-06 11:23:16

问题


I have spring boot maven java multi module structure. My structure is:

product (parent pom module)
..product-data (child pom module)
..product-business (has dependency for product-data) 
..product-rest (has dependency for product-business) 
..product-entities (child pom module)

product-data will return entity object to product-business and product-business will return entity object to product-rest and product-rest returns json object.

product-data runs fine. But as soon as I run product-business, I get error "Cannot determine embedded database driver class for database type NONE". Spring looks for spring.datasource.... properties under my product-business/src/main/resources/application.properties file. If I define all the properties here then error goes away and I get the data from product-data.

But!! I have already defined the properties under product-data/src/main/resources/ application.properties file. Why do I have to duplicate the same properties in my product-business module? The whole purpose is to separate layers. product-data is responsible for fetching the data and it should find the spring.datasource... properties under its own structure. Why does it forces me to duplicate the properties in business module too? I am sure I am missing something. Does someone has any clue?

I have gone through many similar questions on SO but most of them were missing the properties so that doesn't solves my problem. I assume my pom files are not the suspect because once I copy paste the properties from product-data to product-business then error goes away. But incase if you still want to see my pom:

Parent product POM

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.owner</groupId>
    <artifactId>product</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <nopeasti.version>1.0.0-SNAPSHOT</nopeasti.version>
    </properties>

    <dependencies>

    </dependencies>

    <modules>
        <module>product-data</module>
        <module>product-business</module>
        <module>product-rest</module>
        <module>product-entities</module>
    </modules>
</project>

product-data POM

<project>
    <artifactId>product-data</artifactId>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.owner</groupId>
        <artifactId>product</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <dependencyManagement>
         <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.8.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.M6</version>
            </plugin>
        </plugins>
    </build>
</project>

product-business POM

<project>
    <artifactId>product-business</artifactId>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.owner</groupId>
        <artifactId>product</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <dependencyManagement>
         <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.8.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.owner</groupId>
            <artifactId>product-data</artifactId>
            <scope>compile</scope>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.M6</version>
            </plugin>
        </plugins>
    </build>
</project>

回答1:


According to this,

It is not advisable to put application.properties in a library because there might be a clash at runtime in the application that uses it (only one application.properties is ever loaded from the classpath)

To solve this and still maintain decoupled architecture, I created another file data.properties under resources folder of product-data module and specified @PropertySource annotation in the config file. Here is the config file of product-data module. The spring.datasource properties were defined in this file. Then there was no need for spring.datasource properties in other 2 modules.

@ComponentScan
@EnableAutoConfiguration
@SpringBootConfiguration
@PropertySource(value = "data.properties")
public class NopeastiDataConfig {
    // no main needed here as this is library consumed by business layer
}

Further read here: Externalized Configuration



来源:https://stackoverflow.com/questions/47317674/maven-multi-module-insists-on-duplicating-datasource-application-properties-in-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!