I am using spring boot 1.5.2, and using profiles but I found a very strange thing.
my spring boot resources folder like this:
configs in application.yml
spring:
profiles:
active: @profileActive@
application-dev.yml
spring:
profiles: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1
username: root
password:
server:
port: 8080
application-test.yml
spring:
profiles: test
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2
username: root
password:
server:
port: 8081
my pom.xml, just only include resources part and profile part.
<!-- profile -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<build.profile.id>test</build.profile.id>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application-${profileActive}.yml</include>
<include>application.yml</include>
<include>templates/*</include>
</includes>
</resource>
</resources>
I am now trying to use test profile , found that everything is oK , @profileActive@
has been replace to test
;
mvn clean package -Dmaven.test.skip=true -Ptest
It looks like everything is Ok.
but when I try to run the jar, it always using dev profile, although application.yml
shows we now using test or prod
profile.
I don't know where is wrong in my yml configs. And I try to include all the profile configs in just one application.yml file. But the application still using dev
profile.
fully configs in one application.yml file
spring:
profiles:
active: @profileActive@
---
spring:
profiles: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1
username: root
password:
server:
port: 8080
---
spring:
profiles: test
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2
username: root
password:
server:
port: 8081
---
spring:
profiles: prod
server:
port: 9000
finally, I try to using properties files, all of my configs works fine, when I run my application can use the right profile.
And Now, I just want to know what's wrong with my yml configs.
Thanks in advance !
finally, I found the reason.
my project is a multiple module maven project。
one module I am using
yml
format,and other is
property
format.when I make the two module both the yml format, everything ok.
thanks all of you! thanks!
The issue may be, that when spring boot reads your application.yml, it already made the decision to fall back to the default profile. From there on, i think, it is to late to switch profiles.
What you can try, is to define your spring.profiles.active property in a configuration file called "bootstrap.yml". This may do the trick for you.
I would personally do the following:
1) remove this from your application.yaml (because you can just set a JVM property for this)
2) remove the profile specific header from each of your profile specific yaml files. You don't need it as the profile is based on the file suffix e.g. remove this:
3) As Oleg said, just run your task with Dspring.profiles.active=Whatever
I reproduced your description with
spring-boot-starter-web
as an additional dependency to be able to start a small application.Everything works as expected. If I run
mvn clean package -Ptest
thetest
profile will be activated. Same with the other profiles. I put all profiles together into oneyml
file, comment out<include>application-${profileActive}.yml</include>
and it works, too.Everything work as expected, too, when I execute the JAR file like you from a parent file system path. When I execute another Maven build with another profile at the same time and restart the application the new profile will be used.
If you'r using
mvn clean package
with another profile thandev
it shouldn't be possible that anyapplication-dev.yml
is in the produced JAR. Withoutclean
and different builds with different Maven profiles they will be all in the JAR.Which OS do you use? Is it possible that something holds a file handle on your JAR file and it won't be replaced? But this should be signaled in your Maven build.
Try to run your jar like
or replace @profileActive@ on the name of the profile you need.