本文介绍 Maven 打包的实践,以 build war 为例。
每个项目都会有多套运行环境,最基本的就是Dev、QA、Prod,不同的环境对应的配置也不尽相同,比如说数据库连接,文件路径,WebServices address 等等。
不同环境下需要构建不同的包,那么我们可以通过以下两种方式来解决:
一、定义多个 pom,使用 -f <pom_file>
指定
e.g.
pom.xml (default)
pom_qa.xml
pom_prod.xml
Build war for qa:
mvn clean package -f pom_qa.xml
Build war for product:
mvn clean package -f pom_prod.xml
多个 pom.xml 之间重复配置很多,不容易维护,极不推荐这种方式。
二、结合 maven-war-plugin 和 profile 进行配置
需求假设:
1) 不同环境的数据库连接地址以及 DataSource 类型不一样
开发环境通过普通的DataSource连接数据库:
<bean name="SQLServerDriverDS" destroy-method="close">
<property name="driverClassName" value="${driver_class}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="url" value="${url}" />
<property name="maxIdle" value="${maxPoolSize}" />
<property name="maxActive" value="${minPoolSize}" />
</bean>
测试和产品环境都是通过 JNDI 方式连接数据库:
<bean id="SQLServerDriverDS">
<property name="jndiName" value="jdbc/AlteraDS" />
</bean>
2) 不同环境的 log4j.properties 不同,文件上传下载路径也不一样。
3) QA 的 war 包名称要带上日期,e.g. xxx-20120830.war
为了实现上述需求,组织工程目录结构如下:
project/
`-- src
|-- main
| |-- java
| |-- env
| | |-- dev
| | | |-- log4j.properties
| | | |-- spring-dataSource.xml
| | | `-- variable.propertes
| | |-- prod
| | | |-- log4j.properties
| | | |-- spring-dataSource.xml
| | | `-- variable.propertes
| | `-- qa
| | |-- log4j.properties
| | |-- spring-dataSource.xml
| | `-- variable.propertes
| |-- resources
| `-- webapp
`-- test
pom.xml里定义三个环境的profile:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<runtime.env>src/main/env/dev</runtime.env>
<final.name>webapp</final.name>
</properties>
<dependencies>
<dependency>
<groupId>com.eightqiu</groupId>
<artifactId>CodeCmns</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>qa</id>
<properties>
<runtime.env>src/main/env/qa</runtime.env>
<final.name>webapp_${buildNumber}</final.name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<format>{0,date,yyyyMMdd}</format>
<items>
<item>timestamp</item>
</items>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
</plugin>
</plugins>
</reporting>
<dependencies>
<dependency>
<groupId>com.eightqiu</groupId>
<artifactId>CodeCmns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<runtime.env>src/main/env/prod</runtime.env>
<final.name>webapp</final.name>
</properties>
<dependencies>
<dependency>
<groupId>com.eightqiu</groupId>
<artifactId>CodeCmns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
Maven-war-plugin里配置additional resource folder
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<overlays>
<overlay>
<groupId>TransactionResource</groupId>
<artifactId>TransactionResource</artifactId>
<excludes>
<exclude>WEB-INF/web.xml</exclude>
</excludes>
</overlay>
</overlays>
<webResources>
<resource>
<directory>${runtime.env}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<finalName>${final.name}</finalName>
${runtime.env}
& ${final.name}
就是profile里定义的两个properties。
打包的时候使用-P <profile>
指定需要打哪个环境的包。
e.g.
mvn clean package -Pdev
mvn clean package -Pqa
附上完整的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>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<groupId>webapp</groupId>
<artifactId>webapp</artifactId>
<scm>
<connection>scm:svn:http://127.0.0.1/dummy</connection>
<developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
<tag>HEAD</tag>
<url>http://127.0.0.1/dummy</url>
</scm>
<properties>
<spring-version>3.1.0.RELEASE</spring-version>
</properties>
<build>
<finalName>${final.name}</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<overlays>
<overlay>
<groupId>TransactionResource</groupId>
<artifactId>TransactionResource</artifactId>
<excludes>
<exclude>WEB-INF/web.xml</exclude>
</excludes>
</overlay>
</overlays>
<webResources>
<resource>
<directory>${runtime.env}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
<exclusions>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</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>TransactionResource</groupId>
<artifactId>TransactionResource</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc</artifactId>
<version>4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.5</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<runtime.env>src/main/env/dev</runtime.env>
<final.name>webapp</final.name>
</properties>
<dependencies>
<dependency>
<groupId>com.eightqiu</groupId>
<artifactId>CodeCmns</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>qa</id>
<properties>
<runtime.env>src/main/env/qa</runtime.env>
<final.name>webapp_${buildNumber}</final.name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<format>{0,date,yyyyMMdd}</format>
<items>
<item>timestamp</item>
</items>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
</plugin>
</plugins>
</reporting>
<dependencies>
<dependency>
<groupId>com.eightqiu</groupId>
<artifactId>CodeCmns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<runtime.env>src/main/env/prod</runtime.env>
<final.name>webapp</final.name>
</properties>
<dependencies>
<dependency>
<groupId>com.eightqiu</groupId>
<artifactId>CodeCmns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>