How to use Maven and various application.properties to generate artifacts of different test regions in a customized Spring application

I want to use Maven to handle artifact generation for different local and test regions. I believe that I can use different profiles, but I'm not sure.

In Maven, can I choose different directories to select the files used in packaging (for example, application.properties)? How to fix this?

The idea that I want is to have the following folders for resources in my project

  • local
  • build server
  • dev
  • sys
  • prod

application.resources, Spring, . . , .

:

  • Maven IDE (IntelliJ)
  • .
  • .
  • / , .

(Duvall, Matyas, Glover).

+5
3

, .

  • Spring PropertyPlaceholderConfigurer
  • Maven ( Dev),
  • (TeamCity )
  • Maven

, , , , , , , .

, , . /. , .

Maven, profile pom, development, , env. , , POM Maven Compiler Maven Tomcat.

, , Tomcat IntelliJ env. env classpath*:dev-common.properties. - .

env PropertyPlaceholderConfigurer


1 - Maven profile pom.xml:

a profile pom:

<profile>
        <id>development</id>
        <activation>
            <property>
                <name>env</name>
                <value>development</value>
            </property>
        </activation>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.3.2</version>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.0-SNAPSHOT</version>
            ...
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>

2 - :

Spring, a PropertyPlaceholderConfigurer. build classpath .

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>
                ${env}
            </value>
        </list>
    </property>


3. :

Spring , , PropertyPlaceholderConfigurer. @ContextConfiguration(locations = {"classpath:test-dataexchange-application-context.xml"}) @RunWith(SpringJUnit4ClassRunner.class)).

PropertyPlaceholderConfigurer , :

<bean id="testpropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:dev-local-common.properties</value>
        </list>
    </property>


:

Team City , , , . , .

, , development. , .


:

  • Maven Project.
  • Maven 3 profile pom settings.xml( Maven) , , settings.xml
+4

Spring, .

maven Spring, .

    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- Default spring profile to use -->
            <spring.profiles.active>dev</spring.profiles.active>
            <!-- Default environment -->
            <environment>develop</environment>
        </properties>
    </profile>

application.properties : spring.profiles.active = ${spring.profiles.active}

application.property , application-profile.properties. : application-dev.properties application-prod.properties

:

  ...
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
 ...

- maven activeprofile.properties. Spring boot , . :

   <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <configuration>
                        <target>
                            <echo message="spring.profiles.active=${spring.profiles.active}" file="target/classes/config/activeprofile.properties" />
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
            </configuration>
        </plugin>
+3

(CI/Build server), ​​ / , , CI.

Maven Maven , TeamCity.

Spring application.resources , . application-resources-${region}.

+1

All Articles