maven项目在实践中的构建管理之路的方法

前言

最近一个月参与了公司几个项目的脚手架构建,适当总结下经验。之前见过太多项目依赖,构建,管理混乱不堪,导致后续的维护性差,甚至出现由此引发的事故。当时就有一个规范管理的想法。

依赖管理

依赖管理,其实就是依赖范围的管理。这里我叫他 依赖池。也就是 所有相关项目的依赖只能从这个池子里拿,不能超出其范围。池子里的依赖我们定义为都是久经考验的同志。以maven工程为例,我们可以定义 一个名为ooxx-dependencies 的 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<? xml version = "1.0" encoding = "UTF-8" ?>
< project xmlns = "http://maven.apache.org/POM/4.0.0"
   < modelVersion >4.0.0</ modelVersion >
 
   < groupId >com.ooxx</ groupId >
   < artifactId >ooxx-dependencies</ artifactId >
   < version >1.0.0.RELEASE</ version >
 
   < name >ooxx dependencies</ name >
   < description >the root dependencies</ description >
   < properties >
     < maven.compiler.source >1.8</ maven.compiler.source >
     < maven.compiler.target >1.8</ maven.compiler.target >
     < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
     < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding >
     < java.version >1.8</ java.version >
     < springboot.version >2.1.5.RELEASE</ springboot.version >
     < spring-boot-admin.version >2.1.4</ spring-boot-admin.version >
     < springSecurityJwt.version >1.0.10.RELEASE</ springSecurityJwt.version >
     < mysql.version >5.1.47</ mysql.version >
     < hikari.version >3.2.0</ hikari.version >
     < hutool.version >4.5.5</ hutool.version >
     < mybatisplus.version >3.1.1</ mybatisplus.version >
     < wexin-pay.version >3.2.0</ wexin-pay.version >
     < wexin-miniapp.version >3.2.0</ wexin-miniapp.version >
     < swagger.version >2.9.2</ swagger.version >
   </ properties >
 
   < distributionManagement >
     < repository >
       < id >nexus</ id >
       < name >Releases</ name >
       < url >http://url/repository/maven-releases</ url >
     </ repository >
     < snapshotRepository >
       < id >nexus</ id >
       < name >Snapshot</ name >
       < url >http://url/repository/maven-snapshots</ url >
     </ snapshotRepository >
   </ distributionManagement >
 
   < dependencyManagement >
     < dependencies >
       < dependency >
         < groupId >org.springframework.boot</ groupId >
         < artifactId >spring-boot-starter-parent</ artifactId >
         < version >${springboot.version}</ version >
         < type >pom</ type >
         < scope >import</ scope >
       </ dependency >
       < dependency >
         < groupId >de.codecentric</ groupId >
         < artifactId >spring-boot-admin-dependencies</ artifactId >
         < version >${spring-boot-admin.version}</ version >
         < type >pom</ type >
         < scope >import</ scope >
       </ dependency >
       < dependency >
         < groupId >org.springframework.boot</ groupId >
         < artifactId >spring-boot-starter-web</ artifactId >
         < version >${springboot.version}</ version >
         <!-- 排除Tomcat依赖 -->
         < exclusions >
           < exclusion >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-tomcat</ artifactId >
           </ exclusion >
         </ exclusions >
       </ dependency >
       < dependency >
         < groupId >org.springframework.security</ groupId >
         < artifactId >spring-security-jwt</ artifactId >
         < version >${springSecurityJwt.version}</ version >
       </ dependency >
       < dependency >
         < groupId >com.zaxxer</ groupId >
         < artifactId >HikariCP</ artifactId >
         < version >${hikari.version}</ version >
       </ dependency >
       < dependency >
         < groupId >mysql</ groupId >
         < artifactId >mysql-connector-java</ artifactId >
         < version >${mysql.version}</ version >
       </ dependency >
       < dependency >
         < groupId >com.baomidou</ groupId >
         < artifactId >mybatis-plus-boot-starter</ artifactId >
         < version >${mybatisplus.version}</ version >
       </ dependency >
       < dependency >
         < groupId >com.baomidou</ groupId >
         < artifactId >mybatis-plus-generator</ artifactId >
         < version >${mybatisplus.version}</ version >
       </ dependency >
 
       <!--swagger2-->
       < dependency >
         < groupId >io.springfox</ groupId >
         < artifactId >springfox-swagger2</ artifactId >
         < version >${swagger.version}</ version >
       </ dependency >
       < dependency >
         < groupId >io.springfox</ groupId >
         < artifactId >springfox-swagger-ui</ artifactId >
         < version >${swagger.version}</ version >
       </ dependency >
     </ dependencies >
   </ dependencyManagement >
</ project >

然后,我们根据业务会定义一个parent项目,这个项目同样是pom工程,区别于依赖池的是, 依赖池基于技术栈而不关注业务,parent关注于业务,不同业务application 依赖不同的parent,parent 来定义具体业务的module层次划分。当然parent 必须从依赖池构建。可能例子更直观, 我们有一个项目,模块分为:1.后台管理模块 2.app接口模块 3.通用依赖模块 4.数据层模块 5.app 启动模块 可以结合上面例子进行如下构建parent

?
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
<? xml version = "1.0" encoding = "UTF-8" ?>
< project xmlns = "http://maven.apache.org/POM/4.0.0"
  < modelVersion >4.0.0</ modelVersion >
 
  < groupId >com.ooxx</ groupId >
  < artifactId >ooxx-parent</ artifactId >
  < version >1.0.0</ version >
  < packaging >pom</ packaging >
 
  < name >parent</ name >
  < description >the parent</ description >
  < properties >
    < maven.compiler.source >1.8</ maven.compiler.source >
    < maven.compiler.target >1.8</ maven.compiler.target >
    < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
    < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding >
    < java.version >1.8</ java.version >
    < ooxx.version >1.0.0</ ooxx.version >
    < ooxx-dependencies.version >1.0.0.RELEASE</ ooxx-dependencies.version >
  </ properties >
  < dependencyManagement >
    < dependencies >
     <!--依赖池 -->
      < dependency >
        < groupId >com.ooxx</ groupId >
        < artifactId >ooxx-dependencies</ artifactId >
        < version >${ooxx-dependencies.version}</ version >
        < type >pom</ type >
        < scope >import</ scope >
      </ dependency >
    <!--数据层模块 -->
      < dependency >
        < groupId >com.ooxx</ groupId >
        < artifactId >ooxx-db</ artifactId >
        < version >${ooxx.version}</ version >
      </ dependency >
    <!--通用依赖模块 -->
      < dependency >
        < groupId >com.ooxx</ groupId >
        < artifactId >ooxx-common</ artifactId >
        < version >${ooxx.version}</ version >
      </ dependency >
   <!-- 后台管理模块-->
      < dependency >
        < groupId >com.ooxx</ groupId >
        < artifactId >ooxx-manage-api</ artifactId >
        < version >${ooxx.version}</ version >
      </ dependency >
   <!--app接口模块 -->
      < dependency >
        < groupId >com.ooxx</ groupId >
        < artifactId >ooxx-app-api</ artifactId >
        < version >${ooxx.version}</ version >
      </ dependency >
    </ dependencies >
  </ dependencyManagement >
  < dependencies >
    < dependency >
      < groupId >cn.hutool</ groupId >
      < artifactId >hutool-all</ artifactId >
      < scope >provided</ scope >
    </ dependency >
    < dependency >
      < groupId >org.projectlombok</ groupId >
      < artifactId >lombok</ artifactId >
      < scope >compile</ scope >
    </ dependency >
  </ dependencies >
 
</ project >

上述的具体如 app接口模块 可以直接引用依赖池中的依赖进行具体开发。

补充

同时建议 版本号 为{数字}.{说明格式}。比如1.0.0.RC、 1.0.0.GA 等用于不同的场景。pom 名称尽量 模板化 如 ooxx-parent 下的子module 命名为 ooxx-db、ooxx-app-api 之类。这样可以用maven 模板生成统一的模板项目以快速构建项目。同时达到 “见其名而知其意”的效果。因个人能力有限,不足之处或者更好的建议还望多多指教。

猜你喜欢

转载自www.cnblogs.com/superstara/p/10918947.html