[英]Access parent's application.properties from dependent project in spring boot
I am developing a multi-module project in Spring Boot where the project structure is as follows: 我正在Spring Boot中开发一个多模块项目,其项目结构如下:
com.app.parent <- parent pom with version numbers and common dependencies (POM)
com.app.core <- repository and service layer, models, DTOs (JAR)
com.app.rest <- rest API (WAR)
com.app.soap <- soap API (WAR)
The pom.xml
file for the parent project is: 父项目的
pom.xml
文件是:
<artifactId>app-parent</artifactId>
<packaging>pom</packaging>
<name>app-parent</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The pom.xml
file for the core project is: 核心项目的
pom.xml
文件是:
<artifactId>app-core</artifactId>
<packaging>jar</packaging>
<name>app-core</name>
<parent>
<groupId>com.app</groupId>
<artifactId>app-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../app-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
The pom.xml
for the rest project is: 其余项目的
pom.xml
是:
<artifactId>app-rest</artifactId>
<packaging>war</packaging>
<name>app-rest</name>
<parent>
<groupId>com.app</groupId>
<artifactId>app-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../app-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.app</groupId>
<artifactId>app-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
The entry point for the app-core project is: app-core项目的切入点是:
@SpringBootApplication
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
The entry point for the app-rest project looks exactly the same. app-rest项目的入口点看起来完全一样。
I have created an application.properties
file within core/src/main/resources/
which contains database config etc. and I can compile/test the app-core project just fine. 我在
core/src/main/resources/
创建了一个application.properties
文件,其中包含数据库配置等,我可以编译/测试app-core项目。 However, when I try to run or test the app-rest project I get errors related to the absence of application.properties
file 但是,当我尝试运行或测试app-rest项目时,我会收到与缺少
application.properties
文件相关的错误
Cannot determine embedded database driver class for database type NONE.
无法确定数据库类型为NONE的嵌入式数据库驱动程序类。 If you want an embedded database please put a supported one on the classpath.
如果你想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。
How do I get the child project to load the parent's application.properties
file? 如何让子项目加载父的
application.properties
文件? Do I have to create a symlink to the parent file, or do I expressly load the parent's property file via @PropertySource
? 我是否必须创建父文件的符号链接,还是通过
@PropertySource
明确加载父的属性文件? What do others do in this scenario? 其他人在这种情况下做了什么?
Create another module for configurations (let's say config) and put your application.properties in config/src/main/resources/. 为配置创建另一个模块(比如说config)并将你的application.properties放在config / src / main / resources /中。
Add the config module as dependency to any module which use the same configs and there you go. 将配置模块作为依赖项添加到任何使用相同配置的模块,然后就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.