好得很程序员自学网

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

Springboot与Maven多环境配置的解决方案

Profile用法

我们在application.yml中为jdbc.username赋予一个值,这个值为一个变量

?

1

2

jdbc:

   username: ${jdbc.username}

Maven中的profiles可以设置多个环境,当我们选择a环境后,<jdbc.username>内的值将替换上述配置文件中的变量

?

1

2

3

4

5

6

7

8

9

10

11

12

</ profiles >

        < profile >

            < id >a</ id >

            < properties >

                < jdbc.username >root</ jdbc.username >

            </ properties >

            <!-- 默认使用此环境 -->

            < activation >

                < activeByDefault >true</ activeByDefault >

            </ activation >

        </ profile >

    </ profiles >

我们查看编译后的application.yml文件,果然变量已经被赋值。我们猜想是否可以利用Profile的这一特性设置开发、测试、生产环境,选择不同环境时使用不同变量,配合Resources和Filter来指定打包内容以及替换变量。

?

1

2

jdbc:

   username: root

resources

用来操作编译文件

filters

过滤器,设置过滤器的资源将会对同名变量进行赋值(被赋值的资源文件需要设置filtering为true)

多环境配置解决方案

网上大多数都是分为application-dev.xml、application-test.xml、application-prod.xml三个文件,可是我们在真实项目开发中,将会用到很多各式各样的文件(例如log4j的配置文件),它们在不同环境中应该也是不同的配置,不能在测试和生产环境使用同一个配置文件。所以我们将分为三个文件夹分别代表开发环境、测试环境、生产环境,他们里面的配置文件种类一致但是内容不一样。选择完当前环境后,打的jar包只包含当前环境文件夹下的配置文件。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

├─main

│  ├─java

│  │  └─......

│  └─resources

│      ├─dev

│      │   └─config

│      │   │   └─mq.yml

│      │   │   └─redis.yml

│      │   └─application-dev.yml

│      ├─prod

│      │  └─config

│      │  │    └─mq.yml

│      │  │    └─redis.yml

│      │  └─application-prod.yml

│      └─test

│      │  └─config

│      │  │    └─mq.yml

│      │  │    └─redis.yml

│      │  └─application-test.yml

│    └─application.yml

│    └─a.xml

└─test

     └─java

         └─......

dev下的config下的mq.yml

?

1

mq: mq-dev

dev下的config下的redis.yml

?

1

redis: redis-dev

dev下的application-dev.yml

?

1

2

3

4

profiles.active:

   dev

port: dev-port

application.yml

?

1

2

3

4

5

spring:

   profiles:

     active: ${profiles.active}

 

port: ${port}

查看编译后的结果

 

其中application.yml中变量已经被替换为

?

1

2

3

4

spring:

   profiles:

     active: dev

port: dev-port

完整的pom.xml

?

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

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

< build >

     < plugins >

         < plugin >

             < groupId >org.springframework.boot</ groupId >

             < artifactId >spring-boot-maven-plugin</ artifactId >

         </ plugin >

         < plugin >

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

             < artifactId >maven-resources-plugin</ artifactId >

             < version >3.1.0</ version >

             <!--使用默认的变量分割符即${}-->

             < configuration >

                 < useDefaultDelimiters >true</ useDefaultDelimiters >

             </ configuration >

         </ plugin >

     </ plugins >

 

     <!-- 测试文件的编译路径设置 -->

     < testResources >

         < testResource >

             <!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包-->

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

             < includes >

                 < include >application.yml</ include >

             </ includes >

             < filtering >true</ filtering >

         </ testResource >

 

         < testResource >

             <!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包-->

             < directory >src/main/resources/${profiles.active}</ directory >

             < includes >

                 < include >**/*.yml</ include >

             </ includes >

             < filtering >false</ filtering >

         </ testResource >

     </ testResources >

 

     < resources >

         < resource >

             <!--打包该目录下的 application.yml -->

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

             < includes >

                 < include >application.yml</ include >

             </ includes >

             <!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->

             < filtering >true</ filtering >

         </ resource >

         < resource >

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

             < includes >

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

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

             </ includes >

             < filtering >false</ filtering >

         </ resource >

         < resource >

             <!-- ${profiles.active}由profile提供 -->

             < directory >src/main/resources/${profiles.active}</ directory >

             < includes >

                 < include >**/*.yml</ include >

             </ includes >

             < filtering >false</ filtering >

         </ resource >

 

     </ resources >

 

     <!-- 定义 filter,即该资源中的值将会用来替换同名属性(设置 filtering 为 true 的资源中的属性)-->

     < filters >

       < filter >

           src/main/resources/${profiles.active}/application-${profiles.active}.yml

       </ filter >

     </ filters >

</ build >

 

< profiles >

     < profile >

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

         < id >dev</ id >

         < properties >

             < profiles.active >dev</ profiles.active >

         </ properties >

         < activation >

             < activeByDefault >true</ activeByDefault >

         </ activation >

 

     </ profile >

 

     < profile >

         <!-- 测试环境 -->

         < id >test</ id >

         < properties >

             < profiles.active >test</ profiles.active >

         </ properties >

     </ profile >

 

     < profile >

         <!-- 生产环境 -->

         < id >prod</ id >

         < properties >

             < profiles.active >prod</ profiles.active >

         </ properties >

     </ profile >

</ profiles >

到此这篇关于Springboot与Maven多环境配置的解决方案的文章就介绍到这了,更多相关Springboot Maven多环境配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://www.cnblogs.com/haixiang/p/12451703.html

查看更多关于Springboot与Maven多环境配置的解决方案的详细内容...

  阅读:18次