Roberto
Roberto

Reputation: 1395

Can't start with profiles in spring + maven + tomcat

I need to use profiles with spring. I use local Tomcat. There is maven project, so, in pom.xml I added:

<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <property>
                    <name>spring.profiles.active</name>
                    <value>dev</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.11</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>at1</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>spring.profiles.active</name>
                    <value>at1</value>
                </property>
            </activation>
        </profile>
    </profiles>

and in application.properties added spring.profiles.active=${activatedProperties} note: spring.profiles.active=@activatedProperties@ tried already too

and there is two files application-at1.properties and application-dev.properties

When build war with -Dspring.profiles.active=dev there is error message - params from this files not found.

My tomcat customizations are:

enter image description here

Upvotes: 0

Views: 1106

Answers (1)

shinjw
shinjw

Reputation: 3431

Can't tell exactly where you are but seems to me that you're using a property place holder, Spring Boot is not picking up a profile because the placeholder actually has no value.

You can configure this in the following way:

Using Property Placeholders:

application.properties

spring.profiles.active=${activatedProperties}

pom.xml

<property>
   <name>activatedProperties</name>
   <value>dev</value>
</property>

Just specify the runtime argument

Remove property from pom.xml and adjust your application.properties with some default value or don't specify it at all

spring.profiles.active=at1 #you can remove this line if you want.

Then run-war with argument -Dspring.profiles.active=dev

Working with Maven Profiles

You can run maven with a -P dev to make sure that the goal is executed with the correct profile.

Upvotes: 2

Related Questions