简体   繁体   中英

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM