Mic*_*zyk 32 java spring maven spring-boot
我正在尝试使用Maven 3
在Spring Boot应用程序中设置一个活动的配置文件.在我的pom.xml中,我将默认的活动配置文件和属性spring.profiles.active设置 为开发:
<profiles>
<profile>
<id>development</id>
<properties>
<spring.profiles.active>development</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)
但每次运行我的应用程序时,我都会在日志中收到以下消息:
No active profile set, falling back to default profiles: default
Run Code Online (Sandbox Code Playgroud)
并且SpringBoot配置文件设置为default(读取application.properties而不是application-development.properties)
我还应该怎样做才能使用Maven配置文件设置SpringBoot活动配置文件?
任何帮助高度赞赏.
Dan*_*ski 56
Maven配置文件和Spring配置文件是两个完全不同的东西.您的pom.xml定义spring.profiles.active
了构建过程中可用的变量,但不是在运行时.这就是为什么只激活默认配置文件的原因.
如何用Spring绑定Maven配置文件?
您需要将构建变量传递给应用程序,以便在启动时可用.
在您的application.properties
:中定义占位符:
spring.profiles.active=@spring.profiles.active@
Run Code Online (Sandbox Code Playgroud)
该@spring.profiles.active@
变量必须与Maven配置文件中声明的属性匹配.
在你的pom.xml中启用资源过滤:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
…
</build>
Run Code Online (Sandbox Code Playgroud)
执行构建时src/main/resources
,Maven将处理目录中的所有文件,并且您的占位符application.properties
将替换为您在Maven配置文件中定义的变量.
Tho*_*lan 19
或者更容易:
mvn spring-boot:run -Dspring-boot.run.profiles={profile_name}
Run Code Online (Sandbox Code Playgroud)
小智 8
您可以使用以下命令运行.在这里,我想使用spring profile运行local
:
spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"
在开发中,直接激活特定的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>
Run Code Online (Sandbox Code Playgroud)
您可以运行以下命令以同时使用Spring Boot和Maven development
配置文件:
mvn spring-boot:run -Pdevelopment
Run Code Online (Sandbox Code Playgroud)
如果您希望能够将任何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>
Run Code Online (Sandbox Code Playgroud)
您可以运行以下命令以同时使用Spring Boot和Maven development
配置文件:
mvn spring-boot:run -Dmy.active.spring.profiles=development
Run Code Online (Sandbox Code Playgroud)
要么 :
mvn spring-boot:run -Dmy.active.spring.profiles=integration
Run Code Online (Sandbox Code Playgroud)
要么 :
mvn spring-boot:run -Dmy.active.spring.profiles=production
Run Code Online (Sandbox Code Playgroud)
所以……
这种配置很有意义,因为在通用Maven概要文件中,您依赖my.active.spring.profiles
传递的属性来执行某些任务或对某些事物进行估价。
例如,我使用这种方式来配置一个通用的Maven配置文件,该配置文件打包了应用程序并构建了一个针对所选环境的docker映像。
您应该使用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>
Run Code Online (Sandbox Code Playgroud)
小智 5
有多种方法为您的springboot应用程序设置配置文件。
您可以将其添加到属性文件中:
spring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud)编程方式:
SpringApplication.setAdditionalProfiles("dev");
Run Code Online (Sandbox Code Playgroud)通过测试,可以轻松指定哪些配置文件处于活动状态
@ActiveProfiles("dev")
Run Code Online (Sandbox Code Playgroud)在Unix环境中
export spring_profiles_active=dev
Run Code Online (Sandbox Code Playgroud)JVM系统参数
-Dspring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
69852 次 |
最近记录: |