使用maven-war-plugin 对Maven项目进行动态打包

在项目发布的时候,我们可能会希望保持项目代码不变的情况下,针对不同的环境获得相应的运行包(如:war)。
现在遇到的一个项目是要在发布的时候更改logback的配置。如windows环境下,log的路径为F:\\apache-tomcat-7.0.50;linux环境下为/home/server/tomcat。


代码如下:
1、logback配置:logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<contextName>quicksample</contextName>
	<property name="LOG_HOME">${log_home_value}</property>
	
	……
<configuration>

2、在maven的pom.xml中配置如下:

…
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugin</artifactId>
	<version>2.4</version>
	<configuration>
		<warName>${project.artifactId}</warName>
		<filters>
			<filter>src/main/resources/logback.xml</filter>
		</filters>
		<webResources>
			<resource>
				<filtering>true</filtering>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/logback.xml</include>
				</includes>
				 <targetPath>WEB-INF/classes</targetPath>
			</resource>
		</webResources>
	</configuration>
</plugin>
…

<profiles>
	<profile>
		<id>windows</id>
		<properties>
			<log_home_value>F:\\apache-tomcat-7.0.50</log_home_value>
		</properties>
	</profile>
	<profile>
		<id>linux</id>
		<properties>
			<log_home_value>/home/server/tomcat</log_home_value>
		</properties>
	</profile>
</profiles>

3、用 mvn clean package -P windows打包

另可参考:http://nileader.blog.51cto.com/1381108/449956




你可能感兴趣的:(动态打包)