Search code examples
mavenspring-bootmaven-profilesspring-profilesspring-properties

spring boot always using the same profile


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 !


Solution

  • 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!