章
目
录
1、前言
我们在使用SpringBoot
开发一个项目时,需要遵循约定>配置>编码
的原则,即:
开发人员仅需规定应用中不符合约定的部分,在没有规定配置的地方,采用默认配置,以力求最简配置为核心思想。
2、创建微服务聚合父工程
2.1 创建Maven项目
打开File->New->Project
,创建Maven
项目,archetype我们选maven-archetype-site
,具体如图:
2.2 聚合总工程命名
主要命名GAV,分别为组织名(一般公司域名逆向书写),分支名(工程名称)和版本号(一般默认即可),我的命名如下:
2.3 Maven配置
选择自己配好的版本,以及核心配置文件(在配置maven时一般都设置好了)
2.4 确定工程名称
2.5 统一编码
选中创建好的项目,点击File->settings->Editor->Filer Encodings
,全设置为UTF-8
2.7 统一编译版本
选中创建好的项目,点击File->settings->Build...->Java Compiler
,将版本改为自己的JDK版本
2.8 注解生效激活
选中创建好的项目,点击File->settings->Build...->Annotation Processors
,勾上选项
2.8 File Types过滤器(可选)
我这里过滤了*.iml
和*.idea
,这是个可选操作,为了项目看起来更简洁
3、设置父工程POM
打开父工程的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.panziye.springcloud</groupId> <artifactId>mscloud</artifactId> <version>1.0-SNAPSHOT</version> <!-- 父类型都为pom类型 --> <packaging>pom</packaging> <!-- 统一管理jar包版本 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.18.8</lombok.version> <mysql.version>8.0.22</mysql.version> <druid.version>1.2.5</druid.version> <mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version> </properties> <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version --> <dependencyManagement> <dependencies> <!--spring boot 2.3.2.RELEASE--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR8--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR8</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud alibaba 2.2.5.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <optional>true</optional> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.9.RELEASE</version> </plugin> </plugins> </build> </project>
至此,父项目工程整个框架就建好了
4、Maven知识相关补充
1)项目的打包(packing)类型:pom、jar、war,默认是jar类型
- pom——父类型都为pom类型
- jar——内部调用或者是作服务使用
- war ——需要部署的项目
2)dependencyManagement
和dependencies
区别
在Maven中dependencyManagement
的作用其实相当于一个对所依赖jar包进行版本管理的管理器。
在dependencyManagement
下申明的dependencies,Maven并不会去实际下载所依赖的jar包,而是在dependencyManagement
中用一个Map记录了jar的三维坐标。而被仅仅是被dependencies包裹的元素,Maven是会去仓库实际下载所需要的jar包的,而至于需要下载什么版本的jar包就有两种判断途径:
a) 如果dependencies
里的dependency
自己没有声明version
元素,那么maven就会倒dependencyManagement
里面去找有没有对该artifactId
和groupId
进行过版本声明,如果有,就继承它,如果没有就会报错,告诉你必须为dependency
声明一个version
b) 如果dependencies
中的dependency
声明了version
,那么无论dependencyManagement
中有无对该jar的version
声明,都以dependency
里的version
为准。
3) Maven中跳过单元测试
找到IDEA最右侧有个Maven
菜单,点开执行如下操作即可,如果代码没有问题,跳过单元测试可以提高效率
4) Maven其他重要常用指令
(1)maven clean
对项目进行清理,清理的过程中会删除target目录下编译的内容。
(2)maven compile
编译项目源代码。
(3)maven test
对项目的运行测试。
(4)maven package
可以打包后的文件存放到项目的 target 目录下,打包好的文件通常都是编译后生成的class文件。
(5)maven install
在本地仓库生成仓库的安装包可以供其他项目引用,同时打包后的文件存放到项目的 target 目录下。
1)
mvn clean package
依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)等7个阶段。2)
mvn clean install
依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install等8个阶段。3)
mvn clean deploy
依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install、deploy等9个阶段。