有时候maven管理war项目,需要根据不同的环境,使用不同的配置文件。
pom.xml文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chinaso</groupId>
<artifactId>test</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<url>http://maven.apache.org</url>
<properties>
<!--
不加下面这一行会报错
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
增加之后
Using 'UTF-8' encoding to copy filtered resources.
-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.7</version>
</dependency>
</dependencies>
<build>
<finalName>test</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<webResources>
<resource>
<!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
<directory>${app_config_path}</directory>
<!-- 是否过滤文件,也就是是否启动auto-config的功能 -->
<filtering>false</filtering>
<!-- 目标路径 -->
<targetPath>WEB-INF/conf</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<app_config_path>src/main/conf/dev</app_config_path>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<app_config_path>src/main/conf/test</app_config_path>
</properties>
</profile>
<profile>
<id>online</id>
<properties>
<app_config_path>src/main/conf/online</app_config_path>
</properties>
</profile>
</profiles>
</project>
打包命令
mvn clean package -Dmaven.test.skip=true -Pdev
输出日志
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ test ---
[INFO] Packaging webapp
[INFO] Assembling webapp [test] in [E:\x\test\target\y]
[INFO] Processing war project
[INFO] Copying webapp webResources [E:\E:\x\test\target\ysrc/main/conf/dev] to [E:\x\test\target\y]
[INFO] Copying webapp resources [E:\x\test\target\y\src\main\webapp]
[INFO] Webapp assembled in [4776 msecs]
[INFO] Building war: E:\x\test\target\vertical-search.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
参考文章
http://lishuaibt.iteye.com/blog/614783