[关闭]
@haokuixi 2014-11-26T00:41:31.000000Z 字数 9935 阅读 28965

Maven 如何为不同的环境打包 —— 开发、测试和产品环境

The best practice for building war for different environment - Dev(Development), QA(Test) and Prod(Product)

maven profile


本文介绍 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连接数据库:

  1. <bean name="SQLServerDriverDS" destroy-method="close">
  2. <property name="driverClassName" value="${driver_class}" />
  3. <property name="username" value="${username}" />
  4. <property name="password" value="${password}" />
  5. <property name="url" value="${url}" />
  6. <property name="maxIdle" value="${maxPoolSize}" />
  7. <property name="maxActive" value="${minPoolSize}" />
  8. </bean>

测试和产品环境都是通过 JNDI 方式连接数据库:

  1. <bean id="SQLServerDriverDS">
  2. <property name="jndiName" value="jdbc/AlteraDS" />
  3. </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:

  1. <profile>
  2. <id>dev</id>
  3. <activation>
  4. <activeByDefault>true</activeByDefault>
  5. </activation>
  6. <properties>
  7. <runtime.env>src/main/env/dev</runtime.env>
  8. <final.name>webapp</final.name>
  9. </properties>
  10. <dependencies>
  11. <dependency>
  12. <groupId>com.eightqiu</groupId>
  13. <artifactId>CodeCmns</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. </dependency>
  16. </dependencies>
  17. </profile>
  18. <profile>
  19. <id>qa</id>
  20. <properties>
  21. <runtime.env>src/main/env/qa</runtime.env>
  22. <final.name>webapp_${buildNumber}</final.name>
  23. </properties>
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.codehaus.mojo</groupId>
  28. <artifactId>buildnumber-maven-plugin</artifactId>
  29. <version>1.1</version>
  30. <executions>
  31. <execution>
  32. <phase>validate</phase>
  33. <goals>
  34. <goal>create</goal>
  35. </goals>
  36. </execution>
  37. </executions>
  38. <configuration>
  39. <format>{0,date,yyyyMMdd}</format>
  40. <items>
  41. <item>timestamp</item>
  42. </items>
  43. </configuration>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. <reporting>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.apache.maven.plugins</groupId>
  51. <artifactId>maven-javadoc-plugin</artifactId>
  52. <version>2.8.1</version>
  53. </plugin>
  54. </plugins>
  55. </reporting>
  56. <dependencies>
  57. <dependency>
  58. <groupId>com.eightqiu</groupId>
  59. <artifactId>CodeCmns</artifactId>
  60. <version>0.0.1-SNAPSHOT</version>
  61. <scope>provided</scope>
  62. </dependency>
  63. </dependencies>
  64. </profile>
  65. <profile>
  66. <id>prod</id>
  67. <properties>
  68. <runtime.env>src/main/env/prod</runtime.env>
  69. <final.name>webapp</final.name>
  70. </properties>
  71. <dependencies>
  72. <dependency>
  73. <groupId>com.eightqiu</groupId>
  74. <artifactId>CodeCmns</artifactId>
  75. <version>0.0.1-SNAPSHOT</version>
  76. <scope>provided</scope>
  77. </dependency>
  78. </dependencies>
  79. </profile>

Maven-war-plugin里配置additional resource folder

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <version>2.1.1</version>
  5. <configuration>
  6. <overlays>
  7. <overlay>
  8. <groupId>TransactionResource</groupId>
  9. <artifactId>TransactionResource</artifactId>
  10. <excludes>
  11. <exclude>WEB-INF/web.xml</exclude>
  12. </excludes>
  13. </overlay>
  14. </overlays>
  15. <webResources>
  16. <resource>
  17. <directory>${runtime.env}</directory>
  18. <targetPath>WEB-INF/classes</targetPath>
  19. </resource>
  20. </webResources>
  21. </configuration>
  22. </plugin>
  1. <finalName>${final.name}</finalName>

${runtime.env} & ${final.name}就是profile里定义的两个properties。
打包的时候使用-P <profile>指定需要打哪个环境的包。
e.g.

mvn clean package -Pdev
mvn clean package -Pqa

附上完整的pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <packaging>war</packaging>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>demo Maven Webapp</name>
  7. <groupId>webapp</groupId>
  8. <artifactId>webapp</artifactId>
  9. <scm>
  10. <connection>scm:svn:http://127.0.0.1/dummy</connection>
  11. <developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
  12. <tag>HEAD</tag>
  13. <url>http://127.0.0.1/dummy</url>
  14. </scm>
  15. <properties>
  16. <spring-version>3.1.0.RELEASE</spring-version>
  17. </properties>
  18. <build>
  19. <finalName>${final.name}</finalName>
  20. <plugins>
  21. <plugin>
  22. <groupId>org.mortbay.jetty</groupId>
  23. <artifactId>maven-jetty-plugin</artifactId>
  24. <version>6.1.10</version>
  25. <configuration>
  26. <connectors>
  27. <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
  28. <port>9090</port>
  29. <maxIdleTime>60000</maxIdleTime>
  30. </connector>
  31. </connectors>
  32. </configuration>
  33. </plugin>
  34. <plugin>
  35. <groupId>org.apache.maven.plugins</groupId>
  36. <artifactId>maven-war-plugin</artifactId>
  37. <version>2.1.1</version>
  38. <configuration>
  39. <overlays>
  40. <overlay>
  41. <groupId>TransactionResource</groupId>
  42. <artifactId>TransactionResource</artifactId>
  43. <excludes>
  44. <exclude>WEB-INF/web.xml</exclude>
  45. </excludes>
  46. </overlay>
  47. </overlays>
  48. <webResources>
  49. <resource>
  50. <directory>${runtime.env}</directory>
  51. <targetPath>WEB-INF/classes</targetPath>
  52. </resource>
  53. </webResources>
  54. </configuration>
  55. </plugin>
  56. <plugin>
  57. <groupId>org.apache.maven.plugins</groupId>
  58. <artifactId>maven-compiler-plugin</artifactId>
  59. <version>2.3.2</version>
  60. <configuration>
  61. <source>1.6</source>
  62. <target>1.6</target>
  63. </configuration>
  64. </plugin>
  65. </plugins>
  66. </build>
  67. <dependencies>
  68. <dependency>
  69. <groupId>org.springframework</groupId>
  70. <artifactId>spring-webmvc</artifactId>
  71. <version>${spring-version}</version>
  72. <exclusions>
  73. <exclusion>
  74. <artifactId>spring-core</artifactId>
  75. <groupId>org.springframework</groupId>
  76. </exclusion>
  77. </exclusions>
  78. </dependency>
  79. <dependency>
  80. <groupId>opensymphony</groupId>
  81. <artifactId>sitemesh</artifactId>
  82. <version>2.4.2</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>jstl</groupId>
  86. <artifactId>jstl</artifactId>
  87. <version>1.2</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>javax.servlet</groupId>
  91. <artifactId>servlet-api</artifactId>
  92. <version>2.5</version>
  93. <scope>provided</scope>
  94. </dependency>
  95. <dependency>
  96. <groupId>javax.servlet.jsp</groupId>
  97. <artifactId>jsp-api</artifactId>
  98. <version>2.2</version>
  99. <scope>provided</scope>
  100. </dependency>
  101. <dependency>
  102. <groupId>TransactionResource</groupId>
  103. <artifactId>TransactionResource</artifactId>
  104. <version>0.0.1-SNAPSHOT</version>
  105. <type>war</type>
  106. </dependency>
  107. <dependency>
  108. <groupId>org.hibernate</groupId>
  109. <artifactId>hibernate-validator</artifactId>
  110. <version>4.2.0.Final</version>
  111. </dependency>
  112. <dependency>
  113. <groupId>org.codehaus.jackson</groupId>
  114. <artifactId>jackson-mapper-asl</artifactId>
  115. <version>1.9.4</version>
  116. </dependency>
  117. <dependency>
  118. <groupId>log4j</groupId>
  119. <artifactId>log4j</artifactId>
  120. <version>1.2.16</version>
  121. </dependency>
  122. <dependency>
  123. <groupId>commons-logging</groupId>
  124. <artifactId>commons-logging</artifactId>
  125. <version>1.1.1</version>
  126. </dependency>
  127. <dependency>
  128. <groupId>net.sf.dozer</groupId>
  129. <artifactId>dozer</artifactId>
  130. <version>5.3.2</version>
  131. </dependency>
  132. <dependency>
  133. <groupId>org.springframework</groupId>
  134. <artifactId>spring-jdbc</artifactId>
  135. <version>${spring-version}</version>
  136. </dependency>
  137. <dependency>
  138. <groupId>commons-dbcp</groupId>
  139. <artifactId>commons-dbcp</artifactId>
  140. <version>1.4</version>
  141. </dependency>
  142. <dependency>
  143. <groupId>com.microsoft.sqlserver</groupId>
  144. <artifactId>sqljdbc</artifactId>
  145. <version>4</version>
  146. </dependency>
  147. <dependency>
  148. <groupId>org.springframework</groupId>
  149. <artifactId>spring-test</artifactId>
  150. <version>${spring-version}</version>
  151. </dependency>
  152. <dependency>
  153. <groupId>junit</groupId>
  154. <artifactId>junit</artifactId>
  155. <version>4.8.2</version>
  156. </dependency>
  157. <dependency>
  158. <groupId>org.slf4j</groupId>
  159. <artifactId>slf4j-api</artifactId>
  160. <version>1.6.4</version>
  161. </dependency>
  162. <dependency>
  163. <groupId>org.slf4j</groupId>
  164. <artifactId>slf4j-log4j12</artifactId>
  165. <version>1.6.4</version>
  166. </dependency>
  167. <dependency>
  168. <groupId>javax.mail</groupId>
  169. <artifactId>mail</artifactId>
  170. <version>1.4</version>
  171. </dependency>
  172. <dependency>
  173. <groupId>commons-fileupload</groupId>
  174. <artifactId>commons-fileupload</artifactId>
  175. <version>1.2.1</version>
  176. </dependency>
  177. <dependency>
  178. <groupId>commons-io</groupId>
  179. <artifactId>commons-io</artifactId>
  180. <version>1.4</version>
  181. </dependency>
  182. <dependency>
  183. <groupId>commons-beanutils</groupId>
  184. <artifactId>commons-beanutils</artifactId>
  185. <version>1.8.3</version>
  186. </dependency>
  187. <dependency>
  188. <groupId>commons-collections</groupId>
  189. <artifactId>commons-collections</artifactId>
  190. <version>3.2.1</version>
  191. </dependency>
  192. <dependency>
  193. <groupId>commons-codec</groupId>
  194. <artifactId>commons-codec</artifactId>
  195. <version>1.5</version>
  196. </dependency>
  197. </dependencies>
  198. <profiles>
  199. <profile>
  200. <id>dev</id>
  201. <activation>
  202. <activeByDefault>true</activeByDefault>
  203. </activation>
  204. <properties>
  205. <runtime.env>src/main/env/dev</runtime.env>
  206. <final.name>webapp</final.name>
  207. </properties>
  208. <dependencies>
  209. <dependency>
  210. <groupId>com.eightqiu</groupId>
  211. <artifactId>CodeCmns</artifactId>
  212. <version>0.0.1-SNAPSHOT</version>
  213. </dependency>
  214. </dependencies>
  215. </profile>
  216. <profile>
  217. <id>qa</id>
  218. <properties>
  219. <runtime.env>src/main/env/qa</runtime.env>
  220. <final.name>webapp_${buildNumber}</final.name>
  221. </properties>
  222. <build>
  223. <plugins>
  224. <plugin>
  225. <groupId>org.codehaus.mojo</groupId>
  226. <artifactId>buildnumber-maven-plugin</artifactId>
  227. <version>1.1</version>
  228. <executions>
  229. <execution>
  230. <phase>validate</phase>
  231. <goals>
  232. <goal>create</goal>
  233. </goals>
  234. </execution>
  235. </executions>
  236. <configuration>
  237. <format>{0,date,yyyyMMdd}</format>
  238. <items>
  239. <item>timestamp</item>
  240. </items>
  241. </configuration>
  242. </plugin>
  243. </plugins>
  244. </build>
  245. <reporting>
  246. <plugins>
  247. <plugin>
  248. <groupId>org.apache.maven.plugins</groupId>
  249. <artifactId>maven-javadoc-plugin</artifactId>
  250. <version>2.8.1</version>
  251. </plugin>
  252. </plugins>
  253. </reporting>
  254. <dependencies>
  255. <dependency>
  256. <groupId>com.eightqiu</groupId>
  257. <artifactId>CodeCmns</artifactId>
  258. <version>0.0.1-SNAPSHOT</version>
  259. <scope>provided</scope>
  260. </dependency>
  261. </dependencies>
  262. </profile>
  263. <profile>
  264. <id>prod</id>
  265. <properties>
  266. <runtime.env>src/main/env/prod</runtime.env>
  267. <final.name>webapp</final.name>
  268. </properties>
  269. <dependencies>
  270. <dependency>
  271. <groupId>com.eightqiu</groupId>
  272. <artifactId>CodeCmns</artifactId>
  273. <version>0.0.1-SNAPSHOT</version>
  274. <scope>provided</scope>
  275. </dependency>
  276. </dependencies>
  277. </profile>
  278. </profiles>
  279. </project>

转载自:http://www.8qiu.cn/archives/1096

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注