Maven integrates spring profile to achieve automatic switching of multiple environments

Profile is mainly used when the project is running in multiple environments, such as development environment, test environment, and online production environment.
A data management background of the project I am responsible for involves 12 different operating environments including the test environment, so every release is very painful and tangled. The configuration is changed and changed, and in the end, some environments have forgotten how to configure of.
The following is an example of this project.
Preparation conditions: spring3.x, Maven 2

Here is the integration of spring's profile and Maven's profile function.

Spring's profile configuration

is first of all spring's configuration data source and property file

<beans profile="xmj_old">
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
            <property name="password" value="123456" />
            <property name="username" value="abc" />
        </bean>
        <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basenames">
                <list>
                    <value>classpath:messages_zh_CN</value>
                    <value>classpath:messages/messages_en_US-xmj_old</value>
                </list>
            </property>
        </bean>
    </beans>
    <beans profile="xmj_new">
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
            <property name="password" value="123456" />
            <property name="username" value="abc" />
        </bean>
        <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basenames">
                <list>
                    <value>classpath:messages_zh_CN</value>
                    <value>classpath:messages/messages_en_US-xmj_new</value>
                </list>
            </property>
        </bean>
    </beans>
...
...
...


The message_en_US-* family of properties files here is where the name of the site and the blacklist are configured:

resource.blacklist.dir=/var/resource/blacklist.txt
system.title.id=system.xmj_old.title


Activating spring's profile

profile is configured, but also activate spring's profile feature.
Do the following configuration in web.xml:

<context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>xmj_old</param-value>
</context-param>


In this way, the profile in the spring configuration file is activated as the configuration of xmj_old, but the web.xml configuration still needs to be changed for manual configuration.
Maven also has a profile function, and our projects are generally managed by Maven. The Maven configuration profile is described below.

Maven configuration profile
Maven configuration profile is also very simple, for example:
<profiles>
        <profile>
            <id>xmj_old</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profiles.activation>xmj_old</profiles.activation>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>tomcat-maven-plugin</artifactId>
                        <version>1.1</version>
                        <configuration>
                            <!-- Configure project auto-publisher-->
                            <url>http://xx.xx.xx.xx:8080/manager/text</url>
                            <path>/xmj-manager</path>
                            <server>Tomcat</server>
                            <warFile>target/${profiles.activation}.war</warFile>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>xmj_new</id>
            <properties>
                <profiles.activation>xmj_new</profiles.activation>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>tomcat-maven-plugin</artifactId>
                        <version>1.1</version>
                        <configuration>
                            <!-- Configure project auto-publisher-->
                            <url>http://xx2.xx.xx.xx:8080/manager/text</url>
                            <path>/xmj-manager</path>
                            <server>Tomcat</server>
                            <warFile>target/${profiles.activation}.war</warFile>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
...
...
...
</profiles>


The activeByDefault tag configuration true indicates the default activated environment, and the direct install is the environment that uses this configuration.
Here is a custom attribute configuration: profiles.activation It is an important configuration for

integrating

Maven profile and spring profile. Integrating Maven profile and spring profile is automated through Maven management web. Less changes will lead to less errors. Change the above web.xml configuration to the following of:
<context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>${profiles.activation}</param-value>
</context-param>


This is not the end, but also configure a plugin for Maven:
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>${profiles.activation}</warName>
                    <!-- activate spring profile -->
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
</plugin>


When packaging, Maven will automatically modify the web.xml file, and automatically replace it with the corresponding attribute value according to the placeholder. The
warName tag can not be configured. I added it here, so I need to add the previous target/${profiles.activation}. war, otherwise an exception that the war package cannot be found will be reported.

Execute the profile
below is the moment to witness the miracle Execute the command
in eclipse:
clean install

The default environment configured earlier is xmj_old. After executing the command, a war package named xmj_old.war appears under the target folder.



Open war package to view web.xml



and switch to other environments under the test.
Execute the command
clean install -P xmj_new

Add the id of the profile to the -P parameter to complete the automatic switch. After execution, see if it has automatically switched to the profile environment with the id of xmj_new.
There is a war package of xmj_new.war in the target file (xmj_old.war is cleaned away). )



Open the war package to view the web.xml file



As expected, Maven successfully completed the automatic switching of multiple environments in combination with spring
Note: The display-name and webAppRootKey in the above web.xml both use the placeholder ${profiles. activation} and then Maven automatically replaces it. If you use log4j/logback and spring in your project, and then deploy multiple instances of the same project to a tomcat, it is recommended to configure the parameter webAppRootKey.
Please refer to log4j/logback + spring's webRootKey bug

locally Running in eclipse's built-in tomcat

because the built-in tomcat compiles the source code directly and then puts it in the specified location to load
, the above method does not work for the built-in tomcat, and it is very painful to test locally. It is said on the
Internet that Maven is set in the project properties. I checked the eclipse official website and said
it I have to find another way. Here is a method explored:

First, create a new profile in the main directory. folder, copy the web.xml under WEB-INF
and then modify the placeholder in web.xml under WEB-INF to the default configuration (that is, the profile for local testing without placeholders) value)
The web.xml configuration that retains placeholders



under is the normal configuration without placeholders under WEB-INF



. I have replaced these placeholders with ysxj.
Next, modify the Maven configuration
to configure:
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>${profiles.activation}</warName>
                    <!-- activate spring profile -->
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
            </plugin>

change into:
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>${profiles.activation}</warName>
                    <!-- activate spring profile -->
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                                <!-- Here is the directory just created -->
                            <directory>src/main/profile</directory>
                                <!-- The target directory is WEB-INF -->
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
            </plugin>

Witness the miracle at the end
, execute the order
install -P naruto

Under the target, the war package naruto.war, which is played according to the profile, appears.
Next, it is proved that the command can be
executed
eclipse:eclipse  -Dwtpversion=1.5

Compile into a normal eclipse project, and then add tomcat server, you will find that it can run successfully.
Here, tomcat uses the web.xml under WEB-INF in the source code, that is, the web.xml that just changed the placeholder to ysxj.
The principle is to change the placeholder in the web.xml under the profile folder during the compilation phase. Replace it with the properties configured in Maven, and then overwrite the web.xml under WEB-INF. The
troublesome point of this method is that if the web.xml file is modified, remember to synchronize the two. In general, this file rarely changes

ps. Sao Nian, don't do it manually, let's play the automatic one

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326634051&siteId=291194637