|
| 1 | +# Maven 中如何管理多模块项目的依赖关系 |
| 2 | + |
| 3 | +> 平时在查看项目的过程中,发现包的依赖关系及其随便,在各个子模块中都各自引入相应的依赖包,有些时候重复导入了也不会发觉。 |
| 4 | +> 在参考alibaba dubbo的源码之后,做出如下总结(别人家的代码 ==!) |
| 5 | +
|
| 6 | +``` |
| 7 | +groupId:组织标识,一般为:公司网址的反写+项目名 |
| 8 | +artifactId:项目名称,一般为:项目名-模块名 |
| 9 | +``` |
| 10 | + |
| 11 | +## 1.首先建一个空的maven项目,项目只需要一个`pom.xml`文件 |
| 12 | + |
| 13 | +> 交代一下我的demo |
| 14 | +
|
| 15 | +项目(root)的`groupId`是`com.nezha.learn.demo` |
| 16 | + |
| 17 | +项目名称 `artifactId` 是 `demo` |
| 18 | + |
| 19 | +该项目有三个子模块: |
| 20 | + |
| 21 | +```xml |
| 22 | +<!--使用Jedis依赖--> |
| 23 | +<module>demo-redis</module> |
| 24 | +<!--主要用于管理依赖关系--> |
| 25 | +<module>demo-parent</module> |
| 26 | +<!--使用spring boot的依赖--> |
| 27 | +<module>demo-juc</module> |
| 28 | +``` |
| 29 | + |
| 30 | +## 2.通过`IDEA`中`New` > `Module`来构建多个模块 |
| 31 | + |
| 32 | +其中的`pom.xml`文件如下: |
| 33 | + |
| 34 | +```xml |
| 35 | +<?xml version="1.0" encoding="UTF-8"?> |
| 36 | +<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"> |
| 37 | + <modelVersion>4.0.0</modelVersion> |
| 38 | + |
| 39 | + <groupId>com.nezha.learn.demo</groupId> |
| 40 | + <artifactId>demo</artifactId> |
| 41 | + <packaging>pom</packaging> |
| 42 | + <version>1.1-SNAPSHOT</version> |
| 43 | + <!--这里是我们的子模块列表--> |
| 44 | + <modules> |
| 45 | + <!--使用Jedis依赖--> |
| 46 | + <module>demo-redis</module> |
| 47 | + <!--主要用于管理依赖关系--> |
| 48 | + <module>demo-parent</module> |
| 49 | + <!--使用spring boot的依赖--> |
| 50 | + <module>demo-juc</module> |
| 51 | + </modules> |
| 52 | + |
| 53 | + <build> |
| 54 | + <plugins> |
| 55 | + <plugin> |
| 56 | + <groupId>org.apache.maven.plugins</groupId> |
| 57 | + <artifactId>maven-release-plugin</artifactId> |
| 58 | + <version>2.4.1</version> |
| 59 | + <configuration> |
| 60 | + <autoVersionSubmodules>true</autoVersionSubmodules> |
| 61 | + </configuration> |
| 62 | + </plugin> |
| 63 | + </plugins> |
| 64 | + </build> |
| 65 | +</project> |
| 66 | +``` |
| 67 | + |
| 68 | +## 3.创建一个专门管理依赖包的模块 |
| 69 | + |
| 70 | +> 同样,该模块只有一个`pom.xml`文件 |
| 71 | +
|
| 72 | +```xml |
| 73 | +<?xml version="1.0" encoding="UTF-8"?> |
| 74 | +<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"> |
| 75 | + <parent> |
| 76 | + <artifactId>demo</artifactId> |
| 77 | + <groupId>com.nezha.learn.demo</groupId> |
| 78 | + <version>1.1-SNAPSHOT</version> |
| 79 | + <relativePath>../pom.xml</relativePath> |
| 80 | + </parent> |
| 81 | + <modelVersion>4.0.0</modelVersion> |
| 82 | + |
| 83 | + <packaging>pom</packaging> |
| 84 | + <artifactId>demo-parent</artifactId> |
| 85 | + |
| 86 | + <!--集中配置版本关系--> |
| 87 | + <properties> |
| 88 | + <jedis.version>2.9.0</jedis.version> |
| 89 | + <spring-boot.version>1.5.2.RELEASE</spring-boot.version> |
| 90 | + </properties> |
| 91 | + |
| 92 | + <!--此处 dependencyManagement 并不会直接引入依赖,是一种懒加载的方式--> |
| 93 | + <dependencyManagement> |
| 94 | + <dependencies> |
| 95 | + <!--引入Redis的客户端--> |
| 96 | + <dependency> |
| 97 | + <groupId>redis.clients</groupId> |
| 98 | + <artifactId>jedis</artifactId> |
| 99 | + <version>${jedis.version}</version> |
| 100 | + </dependency> |
| 101 | + <!--引入spring boot的依赖--> |
| 102 | + <dependency> |
| 103 | + <groupId>org.springframework.boot</groupId> |
| 104 | + <artifactId>spring-boot-dependencies</artifactId> |
| 105 | + <version>${spring-boot.version}</version> |
| 106 | + <type>pom</type> |
| 107 | + <scope>import</scope> |
| 108 | + </dependency> |
| 109 | + </dependencies> |
| 110 | + </dependencyManagement> |
| 111 | +</project> |
| 112 | +``` |
| 113 | + |
| 114 | +注意此处的`artifactId`是项目名称,因为所有子模块的依赖都在项目`root`的`pom.xml`中定义的。 |
| 115 | + |
| 116 | + |
| 117 | +## 4.在子模块中引入`demo-paren`的依赖 |
| 118 | + |
| 119 | +```xml |
| 120 | +<?xml version="1.0" encoding="UTF-8"?> |
| 121 | +<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"> |
| 122 | + <parent> |
| 123 | + <artifactId>demo-parent</artifactId> |
| 124 | + <groupId>com.nezha.learn.demo</groupId> |
| 125 | + <version>1.1-SNAPSHOT</version> |
| 126 | + <relativePath>../demo-parent/pom.xml</relativePath> |
| 127 | + </parent> |
| 128 | + <modelVersion>4.0.0</modelVersion> |
| 129 | + |
| 130 | + <artifactId>demo-juc</artifactId> |
| 131 | + |
| 132 | + <dependencies> |
| 133 | + <dependency> |
| 134 | + <groupId>org.springframework.boot</groupId> |
| 135 | + <artifactId>spring-boot-starter</artifactId> |
| 136 | + </dependency> |
| 137 | + |
| 138 | + <dependency> |
| 139 | + <groupId>${project.groupId}</groupId> |
| 140 | + <artifactId>demo-redis</artifactId> |
| 141 | + <version>${project.version}</version> |
| 142 | + </dependency> |
| 143 | + </dependencies> |
| 144 | +</project> |
| 145 | +``` |
| 146 | + |
| 147 | +> 注意此处的`artifactId`是`demo-parent` |
| 148 | +> 此时引入的spring依赖是不需要加上版本号的 |
| 149 | +> 然后`${project.groupId}`和`${project.version`用于指代项目和版本。 |
| 150 | +
|
| 151 | +## 如何更新所有子模块的版本号 |
| 152 | + |
| 153 | +> 这个也好解决,Maven有插件鸭! |
| 154 | +
|
| 155 | +在父级的`pom.xml`中加个插件就OK了,`plugins` > `release` > `update-versions` |
| 156 | + |
| 157 | +```xml |
| 158 | + <build> |
| 159 | + <plugins> |
| 160 | + <plugin> |
| 161 | + <groupId>org.apache.maven.plugins</groupId> |
| 162 | + <artifactId>maven-release-plugin</artifactId> |
| 163 | + <version>2.4.1</version> |
| 164 | + <configuration> |
| 165 | + <autoVersionSubmodules>true</autoVersionSubmodules> |
| 166 | + </configuration> |
| 167 | + </plugin> |
| 168 | + </plugins> |
| 169 | + </build> |
| 170 | +``` |
| 171 | + |
| 172 | +## maven的pom文件报错: must be "pom" but is "jar" |
| 173 | + |
| 174 | +> parent工程的pom.xml文件的project节点下加入如下节点: |
| 175 | +
|
| 176 | +```xml |
| 177 | +<packaging>pom</packaging> |
| 178 | +``` |
| 179 | + |
| 180 | +## 参考文献 |
| 181 | + |
| 182 | +1. @Maven POM 详解:<https://www.jianshu.com/p/8417a94c4d94> |
| 183 | +2. 使用maven-release-plugin自动化版本发布<https://blog.csdn.net/shenchaohao12321/article/details/79302791> |
| 184 | +3. spring dubbo的源码:<https://github.com/apache/incubator-dubbo-spring-boot-project> |
0 commit comments