转自:https://my.oschina.net/vernon/blog/271970
摘要: 这段时间接触Maven, 发现还是个不错的东西, 我在OpenShift申请了一个账号, 准备自己做一个自己的网站, 它默认是Maven管理的, 也可以自己用其他的. 但是我要打war, 然后发布. 我本地的测试配置文件 与 线上的配置不一样, SB的做法就是每次复制粘贴, 这太.... 所以, 我必须要在本地的IDEA里面一样, 需要谁作为我的资源文件就要作为的我资源文件, 那就方便了, 找了很多的BLGO, 说的很好, 就是琳琳碎碎的. 还不如给我一个pom.xml了. 以至于我一直没有跑起来, 或者他们的碎片就是不完整的, 自己折腾了半天, 并且自己测试, 上线, 都OK. ...
<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>book</groupId>
<artifactId>book</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>book</name>
<repositories>
<repository>
<id>eap</id>
<url>http://maven.repository.redhat.com/techpreview/all</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>eap</id>
<url>http://maven.repository.redhat.com/techpreview/all</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<package.environment>local</package.environment>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1003-jdbc4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>local</id>
<properties>
<package.environment>local</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>product</id>
<properties>
<package.environment>product</package.environment>
</properties>
</profile>
</profiles>
<build>
<finalName>book</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>local/*</exclude>
<exclude>product/*</exclude>
<exclude>public/*</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<warName>book</warName>
<webResources>
<resource>
<directory>src/main/resources/${package.environment}</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/public</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
<!--<resource>-->
<!--<directory>${project.build.directory}/classes</directory>-->
<!--<includes>-->
<!--<include>**/*.properties</include>-->
<!--<include>**/*.xml</include>-->
<!--</includes>-->
<!--</resource>-->
</webResources>
</configuration>
</plugin>
</plugins>
</build></project>
其中, 我们注意的地方: 我解释一下:
<profiles>
<profile>
<id>local</id>
<properties>
<package.environment>local</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>product</id>
<properties>
<package.environment>product</package.environment>
</properties>
</profile>
</profiles>
这里就是不同的resources文件夹, 我这里只区分本地 产品; 设置为true的就是默认被激活的. 所以后面打包默认就是本地;
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>local/*</exclude>
<exclude>product/*</exclude>
<exclude>public/*</exclude>
</excludes>
</resource>
</resources>
这里就是我的资源文件, public里面存放我的公用的, 比如Spring的配置文件, 就是本地与产品都一样的;local 与 product一看就知道了;
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<warName>book</warName>
<webResources>
<resource>
<directory>src/main/resources/${package.environment}</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/public</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
<!--<resource>-->
<!--<directory>${project.build.directory}/classes</directory>-->
<!--<includes>-->
<!--<include>**/*.properties</include>-->
<!--<include>**/*.xml</include>-->
<!--</includes>-->
<!--</resource>-->
</webResources>
</configuration>
</plugin>
这里就是war的插件了,
${package.environment}
就是动态指定文件夹了.
<filtering>true</filtering>
这里一定需要设置为true才行.
然后就差不多了. 最后执行 mvn clean ; mvn compile; mvn package; 这里是Maven的生命周期, 其他介绍的文章太多了, 我就不再具体讲. 如果说我要打product的war包;
mvn clean ; mvn compile; mvn -Pproduct package;
那就激活了product 的资源文件;
就这样子, 很简单吧~