Łukasz Woźniczka
Łukasz Woźniczka

Reputation: 1695

Maven how to set properties visible in java

This is my profiles in pom.xml:

<!-- Production Profile -->
    <profile>
        <id>production</id>
        <activation>
            <property>
                <name>profile</name>
                <value>prod</value>
            </property>
        </activation>
        <properties>
            <build.profile.id>prod</build.profile.id>
        </properties>

    </profile>

    <!-- Development Profile -->
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
        </properties>
    </profile>

    <!-- Integration Test Profile -->
    <!-- Use mvn -Dprofile=test -->
    <profile>
        <id>integration-test</id>
        <activation>
            <property>
                <name>profile</name>
                <value>test</value>
            </property>
        </activation>
        <properties>
            <!-- Used to locate the profile specific configuration file -->
            <build.profile.id>test</build.profile.id>
            <!-- Only integration tests are run. -->
            <skip.integration.tests>false</skip.integration.tests>
            <skip.unit.tests>true</skip.unit.tests>
        </properties>
    </profile>

Next in my spring context configuration file i have got :

@PropertySource("classpath:properties/app-${profile:dev}.properties")

So that tricks allow me run my project using different configuration. I have got also 3 configuration properties files,and in that file there is :

spring.profiles.active=integration-test
spring.profiles.active=prod
spring.profiles.active=dev

This configuration work perfect. Active profiles is change as i expect so for example when i run mvn clean install -Dprofile=test, i am using app-test.properties file and my active profile is integration-test.

But this trick have got one issue. When someone run mvn install -Pintegration-test then active profile is dev not integration-test. So it is possible run mvn install -Pintegration-test and then set property profile to test ??

Upvotes: 1

Views: 965

Answers (2)

senyor
senyor

Reputation: 352

Please try -P+integration-test option

Upvotes: 0

Robert Scholte
Robert Scholte

Reputation: 12335

You shouldn't use Maven Profiles per environment. With Maven you build the jar/war/ear. A different environment shouldn't result in another artifact. So instead you should pull the configuration outside this artifact. How to pass value to maven pom.xml at rum time from java file? describes a lot of options how to solve this.

Upvotes: 2

Related Questions