好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Maven根据不同环境打包不同配置文件的方法

开发项目时会遇到这个问题:开发环境,测试环境,生产环境的配置文件不同, 打包 时经常要手动更改配置文件,更改的少还可以接受,但是如果需要更多个配置文件,手动的方法就显得非常笨重了。

下面介绍一种方法,利用 maven 插件来打包不同环境的配置文件。我们用到的是 maven-war-plugin 这个插件。

首先贴出整个pom文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

<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/xsd/maven-4.0.0.xsd" >

   <modelversion> 4.0 . 0 </modelversion>

   <groupid>cms</groupid>

   <artifactid>cms</artifactid>

   <packaging>war</packaging>

   <version> 0.0 . 1 -snapshot</version>

   <name>cms</name>

   <url>http: //maven.apache.org</url>

   <properties>

     <spring.version> 4.1 . 6 .release</spring.version>

     <project.build.sourceencoding>utf- 8 </project.build.sourceencoding>

   </properties>

   <dependencies>  

    <!-- 依赖省略-->

   </dependencies>

  <profiles>

     <profile>

       <!-- 本地开发环境 -->

       <id>dev</id>

       <properties>

         < package .environment>dev</ package .environment>

       </properties>

       <activation>

         <activebydefault> true </activebydefault>

       </activation>

     </profile>

     <profile>

       <!-- 测试环境 -->

       <id>test</id>

       <properties>

         < package .environment>test</ package .environment>

       </properties>

     </profile>

     <profile>

       <!-- 生产环境 -->

       <id>prod</id>

       <properties>

         < package .environment>prod</ package .environment>

       </properties>

     </profile>

   </profiles>

   <build>

     <finalname>cms</finalname>

     <sourcedirectory>src</sourcedirectory>

     <plugins>

       <plugin>

         <artifactid>maven-compiler-plugin</artifactid>

         <version> 3.3 </version>

         <configuration>

           <source> 1.7 </source>

           <target> 1.7 </target>

         </configuration>

       </plugin>

       <plugin>

         <groupid>org.apache.maven.plugins</groupid>

         <artifactid>maven-war-plugin</artifactid>

         <version> 2.1 . 1 </version>

         <configuration>

           <webxml>webroot\web-inf\web.xml</webxml>

           <warsourcedirectory>webroot</warsourcedirectory>

           <archive>

             <addmavendescriptor> false </addmavendescriptor>

           </archive>

           <warname>cms</warname>

           <webresources>

             <resource>

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

               <targetpath>web-inf/classes</targetpath>

               <filtering> true </filtering>

             </resource>

           </webresources>

         </configuration>

       </plugin>

     </plugins>

     <resources>

       <resource>

         <directory>src</directory>

         <filtering> true </filtering>

         <includes>

           <include>** /*.properties</include>

           <include>**/ *.xml</include>

         </includes>

       </resource>

     </resources>

   </build>

</project>

简单说明几个地方:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<profiles>

   <profile>

     <!-- 本地开发环境 -->

     <id>dev</id>

     <properties>

       < package .environment>dev</ package .environment>

     </properties>

     <activation>

       <activebydefault> true </activebydefault>

     </activation>

   </profile>

   <profile>

     <!-- 测试环境 -->

     <id>test</id>

     <properties>

       < package .environment>test</ package .environment>

     </properties>

   </profile>

   <profile>

     <!-- 生产环境 -->

     <id>prod</id>

     <properties>

       < package .environment>prod</ package .environment>

     </properties>

   </profile>

</profiles>

此处借助profiles定义几个不同的环境文件夹,相同的需要在项目里面创建同id的文件夹,用来存放特定环境的配置文件。

我之前的resource目录:

我的目录结构是相对复杂的一种有2层目录,只有一层目录的也一样更简单。外层xml文件,还有一个properties文件夹。这里我针对不同环境需要更改的配置文件有4个,标红的。

 

再看一下改造后resource的目录结构:

随便展开一个dev文件夹是这样:

可见需要更改的配置文件,需要copy到各个环境的文件夹当中去,而不需要更改的文件,则不需要复制一份。

此处需要说明的是,如果我指定的是dev,则maven会将dev下的所有文件拿出来,db.xml覆盖掉外面的db.xml,dev.properties文件夹中的配置文件会拿出来放到外面的properties文件夹中。所以说这里是非常灵活的,你需要哪些文件定制,完全由自己来控制。

再来看pom文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<plugin>

   <groupid>org.apache.maven.plugins</groupid>

   <artifactid>maven-war-plugin</artifactid>

   <version> 2.1 . 1 </version>

   <configuration>

     <webxml>webroot\web-inf\web.xml</webxml>

     <warsourcedirectory>webroot</warsourcedirectory>

     <archive>

       <addmavendescriptor> false </addmavendescriptor>

     </archive>

     <warname>test</warname>

     <webresources>

       <resource>

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

         <targetpath>web-inf/classes</targetpath>

         <filtering> true </filtering>

       </resource>

     </webresources>

   </configuration>

</plugin>

这里使用的是

?

1

maven-war-plugin

这个插件,此插件的功能是很强大的,想深入了解,可以到官网去看。

?

1

${ package .environment}

动态指定目录,接收参数。

?

1

targetpath

目标路径。

另外说2点,这两个标签

?

1

2

<webxml>webroot\web-inf\web.xml</webxml>

<warsourcedirectory>webroot</warsourcedirectory>

1.如果maven打包错误说找不到web.xml,说明你得项目结构不是标准的,用webxml标签指定一下就可以了

2.如果jsp打包没有的话,同样的问题,指定一下目录,我的项目结构就不是maven标准结构,所以需要指定一下。

改造完毕,接下来就是利用maven打包了。

mvn clean ; mvn compile;

mvn -p test package; 传相应环境参数就ok了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/li295214001/article/details/52044800

查看更多关于Maven根据不同环境打包不同配置文件的方法的详细内容...

  阅读:51次