Java 通过 Maven 在 SpringBoot 中配置活动配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42390860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configure active profile in SpringBoot via Maven
提问by Micha? Szewczyk
I'm trying to set an active profile in Spring Boot application using Maven 3.
In my pom.xml I set default active profileand property spring.profiles.activeto development:
我正在尝试使用 Maven 3
在Spring Boot 应用程序中设置活动配置文件。在我的 pom.xml 中,我将默认活动配置文件和属性spring.profiles.active 设置为development:
<profiles>
<profile>
<id>development</id>
<properties>
<spring.profiles.active>development</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
but every time I run my application, I receive the following message in logs:
但是每次运行我的应用程序时,我都会在日志中收到以下消息:
No active profile set, falling back to default profiles: default
and the SpringBoot profile is set to default (reads application.properties instead application-development.properties)
What else should I do to have my SpringBoot active profile set using Maven profile?
Any help highly appreciated.
并且 SpringBoot 配置文件设置为默认值(读取 application.properties 而不是 application-development.properties)
我还应该做些什么来使用 Maven 配置文件设置我的 SpringBoot 活动配置文件?
任何帮助高度赞赏。
采纳答案by Daniel Olszewski
The Maven profile and the Spring profile are two completely different things. Your pom.xml defines spring.profiles.active
variable which is available in the build process, but not at runtime. That is why only the default profile is activated.
Maven 配置文件和 Spring 配置文件是两个完全不同的东西。您的 pom.xml 定义spring.profiles.active
了在构建过程中可用但在运行时不可用的变量。这就是为什么只激活默认配置文件的原因。
How to bind Maven profile with Spring?
如何将 Maven 配置文件与 Spring 绑定?
You need to pass the build variable to your application so that it is available when it is started.
您需要将构建变量传递给您的应用程序,以便它在启动时可用。
Define a placeholder in your
application.properties
:[email protected]@
The
@spring.profiles.active@
variable must match the declared property from the Maven profile.Enable resource filtering in you pom.xml:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> … </build>
When the build is executed, all files in the
src/main/resources
directory will be processed by Maven and the placeholder in yourapplication.properties
will be replaced with the variable you defined in your Maven profile.
在您的 中定义一个占位符
application.properties
:[email protected]@
该
@spring.profiles.active@
变量必须与 Maven 配置文件中声明的属性匹配。在 pom.xml 中启用资源过滤:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> … </build>
执行构建时,目录中的所有文件
src/main/resources
都将由 Maven 处理,并且您的占位符application.properties
将替换为您在 Maven 配置文件中定义的变量。
For more details you can go to my postwhere I described this use case.
回答by SaWo
You should use the Spring Boot Maven Plugin:
您应该使用Spring Boot Maven 插件:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
<configuration>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>
回答by Justin Joseph
You can run using the following command. Here I want to run using spring profile local
:
您可以使用以下命令运行。在这里,我想使用 spring 配置文件运行local
:
spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"
spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"
回答by Thomas Escolan
Or rather easily:
或者更容易:
mvn spring-boot:run -Dspring-boot.run.profiles={profile_name}
回答by Pankaj Jaiswal
There are multiple ways to set profiles for your springboot application.
有多种方法可以为您的 springboot 应用程序设置配置文件。
You can add this in your property file:
spring.profiles.active=dev
Programmatic way:
SpringApplication.setAdditionalProfiles("dev");
Tests make it very easy to specify what profiles are active
@ActiveProfiles("dev")
In a Unix environment
export spring_profiles_active=dev
JVM System Parameter
-Dspring.profiles.active=dev
您可以将其添加到您的属性文件中:
spring.profiles.active=dev
程序化方式:
SpringApplication.setAdditionalProfiles("dev");
测试使指定哪些配置文件处于活动状态变得非常容易
@ActiveProfiles("dev")
在 Unix 环境中
export spring_profiles_active=dev
JVM系统参数
-Dspring.profiles.active=dev
回答by davidxxx
In development, activating a Spring Boot profile when a specific Maven profile is activate is straight. You should use the profiles
propertyof the spring-boot-maven-pluginin the Maven profile such as :
在开发中,当一个特定的 Maven 配置文件被激活时激活一个 Spring Boot 配置文件是直接的。您应该使用的profiles
物业的的弹簧引导Maven的插件在Maven配置文件,如:
<project>
<...>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>development</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profiles>
</...>
</project>
You can run the following command to use both the Spring Boot and the Maven development
profile :
您可以运行以下命令来同时使用 Spring Boot 和 Mavendevelopment
配置文件:
mvn spring-boot:run -Pdevelopment
If you want to be able to map any Spring Boot profiles to a Maven profile with the same profile name, you could define a single Maven profile and enabling that as the presence of a Maven property is detected. This property would be the single thing that you need to specify as you run the mvn
command.
The profile would look like :
如果您希望能够将任何 Spring Boot 配置文件映射到具有相同配置文件名称的 Maven 配置文件,您可以定义一个 Maven 配置文件并在检测到 Maven 属性的存在时启用它。此属性将是您在运行mvn
命令时需要指定的唯一内容。
配置文件如下所示:
<profile>
<id>spring-profile-active</id>
<activation>
<property>
<name>my.active.spring.profiles</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>${my.active.spring.profiles}</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
And you can run the following command to use both the Spring Boot and the Maven development
profile :
您可以运行以下命令来同时使用 Spring Boot 和 Mavendevelopment
配置文件:
mvn spring-boot:run -Dmy.active.spring.profiles=development
or :
或者 :
mvn spring-boot:run -Dmy.active.spring.profiles=integration
or :
或者 :
mvn spring-boot:run -Dmy.active.spring.profiles=production
And so for...
所以对于...
This kind of configuration makes sense as in the generic Maven profile you rely on the my.active.spring.profiles
property that is passed to perform some tasks or value some things.
For example I use this way to configure a generic Maven profile that packages the application and build a docker image specific to the environment selected.
这种配置是有意义的,因为在通用 Maven 配置文件中,您依赖于my.active.spring.profiles
传递来执行某些任务或评估某些事情的属性。
例如,我使用这种方式来配置一个通用的 Maven 配置文件,该配置文件打包应用程序并构建特定于所选环境的 docker 映像。