javaspringmavenspring-bootmaven-profiles

Spring Boot from Maven Profile in POM


I have a spring boot application that has dev and prod settings. Following multiple instructions found during searches I have an application.properties file which has:

#-----------this will load the prod or dev according to the @activatedProperties@ value which is set from the pom when built
spring.profiles.active=@activatedProperties@

Then there are two files:

I then have a pom which has the spring-boot pom as a parent

<parent>
    <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

I then set the profiles up as follows:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <activatedProperties>prod</activatedProperties>
        </properties>
    </profile>
</profiles>

If I then execute a build as follows:

mvn -DskipTests -Pprod clean compile package

After doing this the application.properties file shows:

#-----------this will load the prod or dev according to the environment
spring.profiles.active=dev

Note that it did not use prod as requested but instead dev. In fact it simply execute the activeByDefault profile not matter what I do. Has anybody else seen this and have any ideas as to what is happening. As you can imagine it is really annoying to have the deploy instructions say 'edit the POM file to move the activeByDefault property from the dev to prod profile'!


Solution

  • Solved!

    Eclipse is getting in the way; as the build happens eclipse is reinforcing the default profile; this happens in :

    Version: 2019-09 R (4.13.0) Build id: 20190917-1200

    If you close eclipse and run from the command line it works just fine.

    If you use eclipse then unchecking the Refresh on access under Preferences -> General -> Workspace then it works. The clue was from another stack overflow question : Eclipse + maven : profile ignored

    Thanks to all who took the time to reply.