用profile管理多个环境的配置信息

        一个软件项目在开发、测试和上线等不同生命周期里,相关的配置信息可能都不一样(比如数据库连接参数),如果用传统的手工方式来维护的话,会很繁杂而且容易出错。为了改变这种情况,可以使用Maven来构建项目,然后使用Maven的Profile和Filterring功能来解决。

 

Filtering是Maven Resources Plugin的一个功能,该功能会使用项目属性或者系统属性的值来替换资源文件中${...}符号的值。

 

比如在一个属性文件中有以下键值对:

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

在pom.xml文件中配置了web.root.path这个属性项:

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

那么在Maven编译打包时,属性文件中的${web.root.path}会自动被替换为“http://localhost:8080/cjmWeb/”。

 

pom.xml中filtering相关配置如下:

<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:值为true表示用项目属性或者系统属性来替换web资源文件中的${...}

     webResources > resource > directory:指定web资源文件目录,默认是src/main/webapp

     webResources > resource > includes:定义哪些web资源文件会被做替换处理。此处只对web.xml文件进行替换处理。

 

     resources > resource > filtering:值为true表示用项目属性或者系统属性来替换项目资源文件中的${...}

     resources > resource > directory:指定项目资源文件目录,默认为src/main/resources

     resources > resource > includes:定义哪些资源文件会被做替换处理。此处只对扩展名为properties和xml这两种资源文件进行替换处理。

 

application.properties文件内容:

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

 

beans.xml文件内容:

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

 

web.xml文件内容:

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

 

pom.xml文件中profile相关配置如下:

<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>

     此处定义了两个Profile段。其中id为development的段用于开发环境,其默认是激活的,id为product的段用于生产环境。如果要编译出生产环境的发布包,可以用下面这个命令:

    mvn clean package -Pproduct    ,-P后面跟profile的id值,如果不指定-P,则会使用activeByDefault=true这个profile段。

     profile > properties:用于定义资源文件中可用的属性实际值

猜你喜欢

转载自chenjumin.iteye.com/blog/2296119