微服务 SpringCloud 工程构建

前文

微服务 SpringCloud 简介

微服务 SpringCloud 版本选型

下载工程

为了方便,这里直接从 Spring 的官网上下载项目,链接地址 https://start.spring.io/
在这里插入图片描述
下载之后直接解压即可
在这里插入图片描述
使用 IDEA 导入项目工程
在这里插入图片描述
修改 pom.xml 文件,添加 pom,设置为总工程
在这里插入图片描述

Maven 中的 DependencyManagement 和 Dependencies

Maven 使用 dependencyManagement 元素来提供了一种管理依赖版本号的方式

通常会在一个组织或者项目的最顶层的父 POM 中看到 dependencyManagement 元素

使用 pom.xml 中的 dependencyManagement 元素能让所有子项目中引用一个依赖而不用显式的列出版本号。Maven 会沿着父子层次向上走,知道找到一个拥有 dependencyManagement 元素的项目,然后它就会使用这个 dependencyManagement 元素指定的版本号

例如在父项目里:

<!-- 子模块继承之后,提供作用:锁定版本 + 子模块不用写 groupId 和 version -->
<dependencyManagement>
	<dependencies>
		<!-- MySQL 连接驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>
	</dependencies>
</dependencyManagement>

然后在子项目就可以添加 mysql-connector-java 时可以不用指定版本号,例如:

<dependencies>
	<!-- MySQL 连接驱动 -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
</dependencies>

这样做的好处是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另外的一个版本,只需要声明 version 即可

扫描二维码关注公众号,回复: 11128037 查看本文章
  • dependencyManagement 里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖

  • 如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项目,并且 version 和 scope 都读取自父 pom

  • 如果子项目中指定了版本号,那么会使用子项目中指定的 jar 版本

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>demo1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- SpringBoot 版本 2.2.2.RELEASE -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.2.2.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>

		<!-- SpringCloud 版本 Hoxton.SR1 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Hoxton.SR1</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>

		<!-- SpringCloud alibaba 版本 2.1.0.RELEASE -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-dependencies</artifactId>
			<version>2.1.0.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>

		<!-- MySQL 连接驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>

		<!-- Druid 连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
		</dependency>

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
发布了227 篇原创文章 · 获赞 1032 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/Woo_home/article/details/105055960
今日推荐