1

I have 3 maven projects A, B, C. A is parent project of B and B is the parent of C. All the profiles are defined in the pom.xml of project A.

In project C, I am trying to select properties file in spring-test-context (under src/test/resources) based on selected profile.For the regression tests, we have 2 properties file :

  • application-test-local.properties
  • application-test.properties

On our windows development system, selected profile will be "local" and on the servers accordingly. When "local" profile is selected, application-test-local.properties should be used and application-test.properties otherwise in the test Spring context. In project C, in spring-test-context.xml, I tried :

<beans profile="docker">
    <util:properties id="metaDbProps"  location="application-test-local.properties"/>
 </beans>
 <beans profile="default">
    <util:properties id="metaDbProps"  location="application-test.properties"/>
 </beans>

However, it seems that application is not able to pass the selected maven profile to spring profile as I am trying "mvn clean test -Pdocker" and it's always picking up the properties file from "default" profile.

Any idea what to fix to pass the maven profile to spring profile so that it can pick up the right properties file?

For understanding, here is how the profiles are defined in project A:

<profiles>
     <!-- Windows development  -->
    <profile>
        <id>docker</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>localhost</INSTALL_MACHINE_LIST>
            <COPY_MODE>local</COPY_MODE>
        </properties>
    </profile>
    <!-- Development -->
    <profile>
        <id>dev</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>dev01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
    <!-- QA -->
    <profile>
        <id>qa</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>dqa01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
    <!-- Production -->
    <profile>
        <id>prod</id>
        <activation/>
        <properties>
            <INSTALL_MACHINE_LIST>prod01</INSTALL_MACHINE_LIST>
        </properties>
    </profile>
</profiles>
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
VictorGram
  • 2,521
  • 7
  • 48
  • 82
  • What do you mean by regression tests, unit- or integration tests? How is that invoked, through the surefire plugin? Finally, why do you have two sets of test cases, don't you just want to test everything during the test phase? – rustyx Nov 08 '18 at 21:00

1 Answers1

3

By default Maven tests are run with Maven Surefire Plugin. You can declare the Spring profiles as a property in your Maven profile:

<profile>
  <id>docker</id>
  <properties>
    <spring.profiles.active>docker</spring.profiles.active>
  </properties>
</profile>

and then pass it to Surefire using <argLine> configuration:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>-Dspring.profiles.active=@{spring.profiles.active} @{argLine}</argLine>
      </configuration>
    </plugin>
  </plugins>
</build>

Do note that @{...} syntax:

Since the Version 2.17 using an alternate syntax for argLine, @{...} allows late replacement of properties when the plugin is executed, so properties that have been modified by other plugins will be picked up correctly

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111