当前位置:   article > 正文

Maven根据不同个环境打包, 获取不同的配置文件_package.environment

package.environment

本地的测试配置文件与内测的配置,甚至线上的配置不一样, 以前比较土的做法就是每次复制粘贴。然后不停的来回改文件。

现在是将本地的开发环境,内测的,生产环境的,各个不同的配置文件放在配置文件夹里。例如:

在项目的资源文件夹 resources下面再新建四个文件夹:

local: 表示本地开发环境的配置文件

internal : 表示 内测环境配置文件

product : 表示生产环境配置环境

除了三这不同的配置文件之外,还有一些是共同的,与环境无关的,就可以放在同一个文件夹里,统一管理,这个文件夹就是:

public : 表示共同的配置文件

结果如一下:

相应在的pom里进行设置:

  1. <resources>
  2. <resource>
  3. <directory>src/main/resources</directory>
  4. <filtering>true</filtering>
  5. <excludes>
  6. <exclude>local/*</exclude>
  7. <exclude>internal/*</exclude>
  8. <exclude>product/*</exclude>
  9. <exclude>public/*</exclude>
  10. </excludes>
  11. </resource>
  12. </resources>

完整理的 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. <groupId>net.saindy</groupId>
  5. <artifactId>faq</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>faq Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <tomcat.version>2.1</tomcat.version>
  13. <slf4j.version>1.7.5</slf4j.version>
  14. <log4j.version>1.2.17</log4j.version>
  15. <java.version>1.7</java.version>
  16. <commons-io.version>2.4</commons-io.version>
  17. <java.encoding>utf8</java.encoding>
  18. <java.home>D:\Development\Java\jdk1.7.0_76\jre</java.home>
  19. <package.environment>local</package.environment>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>3.8.1</version>
  26. <scope>test</scope>
  27. </dependency>
  28. <!-- web setting start -->
  29. <dependency>
  30. <groupId>javax.servlet</groupId>
  31. <artifactId>jstl</artifactId>
  32. <version>1.2</version>
  33. <type>jar</type>
  34. </dependency>
  35. <dependency>
  36. <groupId>javax.servlet</groupId>
  37. <artifactId>javax.servlet-api</artifactId>
  38. <version>3.0.1</version>
  39. <scope>provided</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>javax.servlet.jsp</groupId>
  43. <artifactId>jsp-api</artifactId>
  44. <version>2.2</version>
  45. <scope>provided</scope>
  46. </dependency>
  47. <!-- web setting end -->
  48. <dependency>
  49. <groupId>commons-io</groupId>
  50. <artifactId>commons-io</artifactId>
  51. <version>${commons-io.version}</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.apache.commons</groupId>
  55. <artifactId>commons-lang3</artifactId>
  56. <version>3.4</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.apache.commons.lang</groupId>
  60. <artifactId>commons-lang</artifactId>
  61. <version>2.1</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.apache.commons</groupId>
  65. <artifactId>commons-fileupload</artifactId>
  66. <version>1.2.1</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.apache.commons</groupId>
  70. <artifactId>commons-email</artifactId>
  71. <version>1.2</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.apache.commons</groupId>
  75. <artifactId>commons-pool2</artifactId>
  76. <version>2.4.2</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>com.alibaba</groupId>
  80. <artifactId>fastjson</artifactId>
  81. <version>1.2.5</version>
  82. </dependency>
  83. <dependency>
  84. <groupId>org.slf4j</groupId>
  85. <artifactId>slf4j-api</artifactId>
  86. <version>${slf4j.version}</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>log4j</groupId>
  90. <artifactId>log4j</artifactId>
  91. <version>${log4j.version}</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>mysql</groupId>
  95. <artifactId>mysql-connector-java</artifactId>
  96. <version>5.1.38</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>net.sf.ehcache</groupId>
  100. <artifactId>ehcache-core</artifactId>
  101. <version>2.5.2</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>com.alibaba</groupId>
  105. <artifactId>druid</artifactId>
  106. <version>1.0.9</version>
  107. </dependency>
  108. <dependency>
  109. <groupId>javax.mail</groupId>
  110. <artifactId>mail</artifactId>
  111. <version>1.4.7</version>
  112. <scope>provided</scope>
  113. </dependency>
  114. </dependencies>
  115. <!-- 配置架包从中央仓库下载 -->
  116. <repositories>
  117. <!-- 仓库地址 -->
  118. <repository>
  119. <id>my-nexus-repositories</id>
  120. <name>my-nexus-repository</name>
  121. <url>http://192.168.0.9:8081/nexus/content/groups/public/</url>
  122. <releases>
  123. <enabled>true</enabled>
  124. </releases>
  125. <snapshots>
  126. <enabled>false</enabled>
  127. </snapshots>
  128. </repository>
  129. </repositories>
  130. <pluginRepositories>
  131. <!-- 插件地址 -->
  132. <pluginRepository>
  133. <id>my-nexus-pluginRepositories</id>
  134. <name>my-nexus-repository</name>
  135. <url>http://192.168.0.9:8081/nexus/content/groups/public/</url>
  136. <releases>
  137. <enabled>true</enabled>
  138. </releases>
  139. <snapshots>
  140. <enabled>false</enabled>
  141. </snapshots>
  142. </pluginRepository>
  143. </pluginRepositories>
  144. <!-- 配置发布到中央仓库 -->
  145. <distributionManagement>
  146. <repository>
  147. <id>my-nexus-releases</id>
  148. <name>my-nexus-releases</name>
  149. <url>http://192.168.0.9:8081/nexus/content/repositories/my-nexus-releases/</url>
  150. </repository>
  151. <snapshotRepository>
  152. <id>my-nexus-snapshot</id>
  153. <name>my-nexus-snapshots</name>
  154. <url>http://192.168.0.9:8081/nexus/content/repositories/my-nexus-snapshots/</url>
  155. </snapshotRepository>
  156. </distributionManagement>
  157. <profiles>
  158. <profile>
  159. <id>local</id><!-- 本地开发环境 -->
  160. <properties>
  161. <package.environment>local</package.environment>
  162. </properties>
  163. <activation>
  164. <activeByDefault>true</activeByDefault>
  165. </activation>
  166. </profile>
  167. <profile>
  168. <id>internal</id><!-- 内测环境 -->
  169. <properties>
  170. <package.environment>internal</package.environment>
  171. </properties>
  172. </profile>
  173. <profile>
  174. <id>product</id><!-- 生产环境 -->
  175. <properties>
  176. <package.environment>product</package.environment>
  177. </properties>
  178. </profile>
  179. </profiles>
  180. <build>
  181. <finalName>${project.artifactId}</finalName>
  182. <resources>
  183. <resource>
  184. <directory>src/main/resources</directory>
  185. <filtering>true</filtering>
  186. <excludes>
  187. <exclude>local/*</exclude>
  188. <exclude>internal/*</exclude>
  189. <exclude>product/*</exclude>
  190. <exclude>public/*</exclude>
  191. </excludes>
  192. </resource>
  193. </resources>
  194. <plugins>
  195. <!-- tomcat7插件 -->
  196. <plugin>
  197. <groupId>org.apache.tomcat.maven</groupId>
  198. <artifactId>tomcat7-maven-plugin</artifactId>
  199. <version>${tomcat.version}</version>
  200. <configuration>
  201. <!-- 自动发布到tomcat -->
  202. <url>http://localhost:80/manager/text</url>
  203. <update>true</update>
  204. <server>tomcat</server>
  205. <username>admin</username>
  206. <password>admin</password>
  207. <uriEncoding>utf-8</uriEncoding>
  208. <path>/${project.artifactId}</path>
  209. </configuration>
  210. </plugin>
  211. <plugin>
  212. <groupId>org.apache.maven.plugins</groupId>
  213. <artifactId>maven-war-plugin</artifactId>
  214. <version>2.1.1</version>
  215. <configuration>
  216. <archive>
  217. <addMavenDescriptor>false</addMavenDescriptor>
  218. </archive>
  219. <warName>${project.artifactId}</warName>
  220. <webResources>
  221. <!-- 不同的环境,使用不同的配置文件 -->
  222. <resource>
  223. <directory>src/main/resources/${package.environment}</directory>
  224. <targetPath>WEB-INF/classes</targetPath>
  225. <filtering>true</filtering>
  226. </resource>
  227. <!-- 公共的配置文件 -->
  228. <resource>
  229. <directory>src/main/resources/public</directory>
  230. <targetPath>WEB-INF/classes</targetPath>
  231. <filtering>true</filtering>
  232. </resource>
  233. </webResources>
  234. </configuration>
  235. </plugin>
  236. </plugins>
  237. <pluginManagement>
  238. <plugins>
  239. <plugin>
  240. <groupId>org.apache.maven.plugins</groupId>
  241. <artifactId>maven-compiler-plugin</artifactId>
  242. <configuration>
  243. <source>${java.version}</source>
  244. <target>${java.version}</target>
  245. <encoding>${java.encoding}</encoding>
  246. <compilerArguments>
  247. <verbose />
  248. <!-- <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> -->
  249. </compilerArguments>
  250. </configuration>
  251. </plugin>
  252. </plugins>
  253. </pluginManagement>
  254. </build>
  255. </project>

对这个pom 再多作一些说明:

  1. <profiles>
  2. <profile>
  3. <id>local</id><!-- 本地开发环境 -->
  4. <properties>
  5. <package.environment>local</package.environment>
  6. </properties>
  7. <activation>
  8. <activeByDefault>true</activeByDefault>
  9. </activation>
  10. </profile>
  11. <profile>
  12. <id>internal</id><!-- 内测环境 -->
  13. <properties>
  14. <package.environment>internal</package.environment>
  15. </properties>
  16. </profile>
  17. <profile>
  18. <id>product</id><!-- 生产环境 -->
  19. <properties>
  20. <package.environment>product</package.environment>
  21. </properties>
  22. </profile>
  23. </profiles>

这里就是不同的resources文件夹, 在这里可以区分本地、内测、生产;  

<activeByDefault>true</activeByDefault>
设置为true的就是默认被激活的. 所以后面打包默认就是本地;

  1. <!-- 不同的环境,使用不同的配置文件 -->
  2. <resource>
  3. <directory>src/main/resources/${package.environment}</directory>
  4. <targetPath>WEB-INF/classes</targetPath>
  5. <filtering>true</filtering>
  6. </resource>

<directory>src/main/resources/${package.environment}</directory>

这边指的是动态文件夹

最后别忘了

<filtering>true</filtering>

这个一定要设置成true

最后, 打包, 使用Maven的生命周期,比如,打包生产环境的配置文件,package  打包生成 war 文件。

mvn -Pproduct package;




声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号