Use profiles to manage the configuration information of multiple environments

        A software project may have different related configuration information (such as database connection parameters) in different life cycles such as development, testing, and launch. If it is maintained by traditional manual methods, it will be complicated and error-prone. To change this situation, you can use Maven to build the project, and then use Maven's Profile and Filterring features to solve.

 

Filtering is a feature of the Maven Resources Plugin that replaces the value of the ${...} symbol in the resource file with the value of the project property or system property.

 

For example, in a properties file there are the following key-value pairs:

        web.root.path=${web.root.path}

The web.root.path property item is configured in the pom.xml file:

        <web.root.path>http://localhost:8080/cjmWeb/</web.root.path>

Then when Maven compiles and packages, ${web.root.path} in the property file will be automatically replaced with "http://localhost:8080/cjmWeb/".

 

The filtering-related configuration in pom.xml is as follows:

<build>
  	<plugins>
  		<plugin>
			<groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-war-plugin</artifactId>
			<version>2.2</version>
            <configuration>  
                <warName>${war.name}</warName>
                <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>  
  	</plugins>
  	
  	<resources>
  		<resource>
  			<filtering>true</filtering>
			<directory>src/main/resources</directory>
  			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
  		</resource>
  	</resources>
</build>

     webResources > resource > filtering: A value of true means to replace ${...} in web resource files with project properties or system properties

     webResources > resource > directory: Specify the web resource file directory, the default is src/main/webapp

     webResources > resource > includes: Define which web resource files will be replaced. Only the web.xml file is replaced here.

 

     resources > resource > filtering: A value of true means to replace ${...} in the project resource file with project properties or system properties

     resources > resource >  directory: Specify the project resource file directory, the default is src/main/resources

     resources > resource >  includes: Define which resource files will be replaced. Here, only two resource files with extensions named properties and xml are replaced.

 

application.properties file content:

 

web.root.path=${web.root.path}

 

Contents of beans.xml file:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<web_root_path value="${web.root.path}" />
</beans>

 

web.xml file content:

 

<web-app>
  	<context-param>  
        <param-name>spring.profiles.active</param-name>  
        <param-value>${profiles.activation}</param-value>  
	</context-param>
</web-app>

 

The profile-related configuration in the pom.xml file is as follows:

 

<profiles>
  	<profile>
  		<id>development</id>
  		<activation>
  			<activeByDefault>true</activeByDefault>
  		</activation>
  		<properties>
  			<profiles.activation>development</profiles.activation>
  			<web.root.path>http://localhost:8080/cjmWeb/</web.root.path>
  		</properties>
  	</profile>
  	
  	<profile>
  		<id>product</id>
  		<activation>
  			<activeByDefault>false</activeByDefault>
  		</activation>
  		<properties>
  			<profiles.activation>product</profiles.activation>
  			<web.root.path>http://127.0.0.1/cjmWeb/</web.root.path>
  		</properties>
  	</profile>
</profiles>

     Two Profile sections are defined here. The segment with the id of development is used in the development environment, which is activated by default, and the segment with the id of product is used in the production environment. If you want to compile the release package for the production environment, you can use the following command:

    mvn clean package -Pproduct , -P is followed by the id value of the profile. If -P is not specified, the profile segment activeByDefault=true will be used.

 

     profile  >  properties: used to define the actual values ​​of the properties available in the resource file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326206431&siteId=291194637