Ami*_*ani 6 maven maven-profiles spring-boot spring-boot-maven-plugin
我对如何在 Spring Boot 应用程序中使用配置文件感到困惑。
假设我在pom.xml
文件中定义了配置文件,以下是我找到的不同方法。
mvnw -Pdev
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
mvnw spring-boot:run -Dspring.profiles.active=dev
mvnw spring-boot:run --spring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud)
1/ 它们之间有什么区别吗?
2/ 最好的使用方法是什么?
您不应该混淆 Maven 配置文件和 Spring 配置文件 - 这是完全不同的事情。
mvnw spring-boot:run -Dspring.profiles.active=dev
- 它告诉 Spring 在运行时使用哪个配置文件。
Spring Profile - 是根据活动配置文件在运行时配置 Spring 应用程序的方式。您可以在这里和这里阅读更多相关信息。
mvnw -P dev
- 它告诉 Maven 使用构建 Maven 配置文件
Maven Profile - 是如何编译/构建/打包 Java 应用程序的。它与 Spring 应用程序的编译/打包有关,而不是运行时执行,并且与 Spring 完全无关。您可以在这里和这里阅读更多相关信息。
这是对你第一个问题的回答。
关于您的第二个问题,当您需要根据配置文件设置以不同方式构建应用程序时,将使用 Maven 配置文件。例如,当您执行时,将所有依赖项打包为一个“fat jar” mvn -P fat-jar
。
当您想要将 Spring 应用程序配置为在运行时以不同方式工作时,可以使用 Spring 配置文件,例如使用development
数据库production
等。
归档时间: |
|
查看次数: |
10060 次 |
最近记录: |