spring boot always using the same profile

2019-07-09 05:07发布

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: enter image description here

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.

enter image description here

but when I try to run the jar, it always using dev profile, although application.yml shows we now using test or prod profile.

enter image description here

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 !

5条回答
Deceive 欺骗
2楼-- · 2019-07-09 05:13

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!

查看更多
老娘就宠你
3楼-- · 2019-07-09 05:17

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.

查看更多
手持菜刀,她持情操
4楼-- · 2019-07-09 05:21

I would personally do the following:

1) remove this from your application.yaml (because you can just set a JVM property for this)

spring:
  profiles:
    active: @profileActive@

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:

spring:
  profiles: dev

3) As Oleg said, just run your task with Dspring.profiles.active=Whatever

查看更多
Juvenile、少年°
5楼-- · 2019-07-09 05:32

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 the test profile will be activated. Same with the other profiles. I put all profiles together into one yml 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 than dev it shouldn't be possible that any application-dev.yml is in the produced JAR. Without clean 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.

查看更多
三岁会撩人
6楼-- · 2019-07-09 05:36

Try to run your jar like

java -jar -Dspring.profiles.active=test yourapp.jar

or replace @profileActive@ on the name of the profile you need.

查看更多
登录 后发表回答