首先,了解几个概念
- 1、<dependencyManagement> 简单说:就是声明,而不引入进项目,在子项目pom文件里引入该jar才会被引入
<dependencyManagement> <dependencies> <!-- spring cloud alibaba 依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- 2、<dependencies> 简单说:就是使用该标签,不管子项目是否引入该jar都会自动引入
<dependencies> <!-- spring cloud alibaba 依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies>
- 3、<modules> 多模块,当项目构建多模块时候配置子模块名称
<!-- 注意这一块的写法,xxx == name,没有网上一些其他博客说的../ --> <modules> <module>xxx</module> </modules>
接下来,就是贴代码
- 父工程
<?xml version="1.0" encoding="UTF-8"?> <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>org.example</groupId> <artifactId>learn_nacos_cloud</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>provider</module> </modules> <properties> <java.version>1.8</java.version> <spring-boot.version>2.4.4</spring-boot.version> <spring-cloud.version>2020.0.2</spring-cloud.version> <spring-cloud-alibaba.version>2020.0.RC1</spring-cloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <!-- spring boot 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring cloud 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring cloud alibaba 依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
- 子工程
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <artifactId>learn_nacos_cloud</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <name>provider</name> <groupId>org.example</groupId> <artifactId>provider</artifactId> <version>${parent.version}</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <properties> <maven.compiler.source>14</maven.compiler.source> <maven.compiler.target>14</maven.compiler.target> </properties> </project>
结尾呢,说一下注意事项
- 就是 <modules> 网上各种各样的写法
- 然后就是子模块只要写对了父以下内容
<parent> <artifactId>learn_nacos_cloud</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent>
- 然后 也是今天最困扰我的一点,就是子模块所引入的jar,一定一定要是你父模块引入的。否则直接会报如下异常
这个异常在1.5秒左右直接就报出来,而且清空了console,让人很不好定位问题。