好得很程序员自学网

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

使用maven profile指定配置文件打包适用多环境的方法

开发过程, 我们习惯把数据源配置, 项目常量, 日志配置等基础数据配置写到一个个单独的的文件中. 如jdbc.properties等各种.格式的文件.

如何不频繁修改配置文件, 随时打包不同基础数据配置信息的项目.

1.新建maven项目,   在pom.xml中添加 profile节点信息如下:

?

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

<profiles>

         <profile>

             <!-- 开发环境 -->

             <id>dev</id>

             <properties>

                 <environment>development</environment><!-- 节点名字environment是自己随意取的 -->

             </properties>

             <activation>

                 <activebydefault> true </activebydefault><!-- 默认激活该profile节点-->

             </activation>

         </profile>

         <profile>

             <!-- 测试环境 -->

             <id>test</id>

             <properties>

                 <environment>test</environment>

             </properties>

         </profile>

         <profile>

             <!-- 预演环境 -->

             <id>prev</id>

             <properties>

                 <environment>preview</environment>

             </properties>

         </profile>

         <profile>

             <!-- 生产环境 -->

             <id>prod</id>

             <properties>

                 <environment>production</environment>

             </properties>

         </profile>

</profiles>

 2. 在项目中添加各环境需要的数据源配置文件,分不同目录存放, 分别是开发,测试, 预演,生产  环境. 如下图左边部分所示.  

3. pom.xml中配置resource节点信息.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<build>          

         <resources>

             <resource>

                 <directory>src/main/resources</directory>

                 <excludes>

                     <exclude>environment/development/*</exclude>

                     <exclude>environment/test/*</exclude>

                     <exclude>environment/preview/*</exclude>

                     <exclude>environment/production/**</exclude>

                 </excludes>

             </resource>

             <resource>

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

                 <targetpath>environment/${environment}</targetpath>

             </resource>

         </resources>

     </build>

resource节点信息解释: 

?

1

2

3

4

5

6

7

<directory>src/main/resources</directory> <!--打包时包含src/main/resources目录下所有 "子" 文件 和 "孙" 文件.如config 和environment -->

<exclude>environment/development/**</exclude> <!--打包时排除src/main/resources/environment/development下所有 "子" 文件 和 "孙" 文件.-->

<exclude>environment/test/**</exclude> <!--打包时排除src/main/resources/environment/test下所有 "子" 文件 和 "孙" 文件.-->

<exclude>environment/preview/**</exclude> <!--打包时排除src/main/resources/environment/preview下所有 "子" 文件 和 "孙" 文件.-->

<exclude>environment/production/**</exclude><!--打包时排除src/main/resources/environment/production下所有 "子" 文件 和 "孙" 文件.-->

<!-- 注意点: 如果写一个心号*, 如<exclude>environment/development/*</exclude> 则表示:打包时排除src/main/resources/environment/development下所有 "子" 文件, 不排除 "孙" 文件, 如上一个截图所示.-->

<!-- 以上配置优先度从上到下 递增, 这就达到目的: config目录下的配置各环境都需要, 而其它环境相关的配置只会有一个目录被打包--->

?

1

2

<directory>src/main/resources/environment/${environment}</directory> <!-- 打包时包含src/main/resources/environment/${environment}下所有 "子" 文件,environment变量值来自profile中赋值 -->

<targetpath>environment/${environment}</targetpath><!--指定src/main/resources/environment/${environment}所有 "子文件" 打包 到包的哪个目录 -->

4.  项目打包.   打包命令mvn package -pdev来指定激活id为 dev 的profile节点, 这样, 开发环境配置文件就会被打包.

开发: mvn package -pdev (因为配置了默认激活dev部分, 所以也可以使用mvn package, 这与 mvn package -pdev 效果相同)

测试: mvn package -ptest

预演:mvn package -pprev

生产:mvn package -pprod

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

原文链接:https://blog.csdn.net/hjiacheng/article/details/57413933

查看更多关于使用maven profile指定配置文件打包适用多环境的方法的详细内容...

  阅读:49次