1. 引言
备注:本文的代码都提交到了码云,有兴趣的童鞋可以clone参阅,地址:https://gitee.com/19931024/microservice-framework
《轮子项目》前面前53
篇文章都是描写理论的,本文回归本质,从代码开始讲起。
基本所有Java
后台的项目都是使用Maven
来构建的,本文主要讲解《轮子项目》的根节点管理。
2. 项目根节点
先贴上项目根节点的pom
内容:
<?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.ylw</groupId> <artifactId>central-parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <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> <java.version>8</java.version> <spring-cloud-alibaba-dependencies.version>2.2.5.RELEASE</spring-cloud-alibaba-dependencies.version> <spring-boot-dependencies.version>2.3.8.RELEASE</spring-boot-dependencies.version> <spring-cloud-dependencies.version>Hoxton.SR9</spring-cloud-dependencies.version> <commons-collections4.version>4.4</commons-collections4.version> <swagger.version>2.9.2</swagger.version> <swagger.ui.version>1.8.7</swagger.ui.version> <swagger.mg.ui.version>1.0.6</swagger.mg.ui.version> <swagger.butler.version>2.0.1</swagger.butler.version> <jjwt.version>0.9.1</jjwt.version> <druid-starter>1.1.22</druid-starter> <jasypt.version>1.14</jasypt.version> <sharding-sphere.version>3.1.0</sharding-sphere.version> <security-oauth2.version>2.3.8.RELEASE</security-oauth2.version> <security-jwt.version>1.1.0.RELEASE</security-jwt.version> <redisson-starter.version>3.14.1</redisson-starter.version> <easyCaptcha.version>1.6.2</easyCaptcha.version> <hutool.version>5.1.4</hutool.version> <mybatis-plus-boot-starter.version>3.4.0</mybatis-plus-boot-starter.version> <aliyun-sdk-oss>3.8.1</aliyun-sdk-oss> <qiniu-java-sdk>7.2.28</qiniu-java-sdk> <easypoi.version>4.1.3</easypoi.version> <poi.version>4.1.1</poi.version> <spring-boot-admin.version>2.3.1</spring-boot-admin.version> <velocity.version>1.7</velocity.version> <commons-configuration2.version>2.7</commons-configuration2.version> <txlcn.version>5.0.2.RELEASE</txlcn.version> <fastdfs-client.version>1.27.2</fastdfs-client.version> <userAgent.version>1.21</userAgent.version> <transmittable.version>2.12.0</transmittable.version> <banner.version>1.0.2</banner.version> <commons-beanutils.version>1.9.4</commons-beanutils.version> <spring-social-security.version>1.1.6.RELEASE</spring-social-security.version> <commons-io.version>2.6</commons-io.version> <servlet-api.version>4.0.1</servlet-api.version> <spring-data-elasticsearch.version>4.1.3</spring-data-elasticsearch.version> <elasticsearch.version>7.10.2</elasticsearch.version> <knife4j.version>2.0.5</knife4j.version> <hibernate-validator.version>6.2.0.Final</hibernate-validator.version> <dubbo.version>2.7.8</dubbo.version> <docker-maven-plugin.version>1.2.2</docker-maven-plugin.version> <sonar.exclusions>ylw-job/**/*, ylw-register/**/*, ylw-web/**/*</sonar.exclusions> <docker.baseImage>openjdk:8-jre-alpine</docker.baseImage> <docker.volumes>/tmp</docker.volumes> <docker.image.prefix>hub.ylw.com:8080/microservices-platform</docker.image.prefix> <docker.java.security.egd>-Djava.security.egd=file:/dev/./urandom</docker.java.security.egd> <docker.java.opts>-Xms128m -Xmx128m</docker.java.opts> </properties> <dependencyManagement> <dependencies> <!-- druid 官方 starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid-starter}</version> </dependency> <!-- mybatis-plus start --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>${mybatis-plus-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis-plus-boot-starter.version}</version> </dependency> <!-- 加入spring security spring security oauth2的处理 --> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>${security-oauth2.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-jwt</artifactId> <version>${security-jwt.version}</version> </dependency> <!-- 在Redis基础上的一个Java实用工具包 --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>${redisson-starter.version}</version> </dependency> <!-- swagger --> <dependency> <groupId>com.github.zlt2000</groupId> <artifactId>swagger-butler-core</artifactId> <version>${swagger.butler.version}</version> </dependency> <!-- 验证码生成工具 --> <dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>${easyCaptcha.version}</version> </dependency> <!-- hutool java工具类库 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>${aliyun-sdk-oss}</version> </dependency> <!-- 七牛依赖 --> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>${qiniu-java-sdk}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <!-- easypoi --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>${easypoi.version}</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>${spring-boot-admin.version}</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>${velocity.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-configuration2</artifactId> <version>${commons-configuration2.version}</version> </dependency> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tm</artifactId> <version>${txlcn.version}</version> </dependency> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tc</artifactId> <version>${txlcn.version}</version> </dependency> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-txmsg-netty</artifactId> <version>${txlcn.version}</version> </dependency> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${sharding-sphere.version}</version> </dependency> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>${fastdfs-client.version}</version> </dependency> <dependency> <groupId>eu.bitwalker</groupId> <artifactId>UserAgentUtils</artifactId> <version>${userAgent.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>transmittable-thread-local</artifactId> <version>${transmittable.version}</version> </dependency> <dependency> <groupId>com.nepxion</groupId> <artifactId>banner</artifactId> <version>${banner.version}</version> </dependency> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-security</artifactId> <version>${spring-social-security.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-api.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>${spring-data-elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-micro-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate-validator.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>${commons-collections4.version}</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>${commons-beanutils.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot-dependencies.version}</version> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>${docker-maven-plugin.version}</version> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository> <id>aliyun-repos</id> <url>https://maven.aliyun.com/repository/public</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <distributionManagement> <repository> <id>releases</id> <name>releases Repository</name> <url>http://192.168.28.138:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <url>http://192.168.28.138:8081/repository/maven-snapshots/</url> <uniqueVersion>true</uniqueVersion> </snapshotRepository> </distributionManagement> <developers> <developer> <name>LinWei YangLinwei</name> <organizationUrl>https://gitee.com/19931024/microservice-framework</organizationUrl> <url>https://yanglinwei.blog.csdn.net/</url> </developer> </developers> </project>
很多内容对吧!我上面的一串可以不看,我把代码折叠后,如下图:
主要分为如下几块:
- 根节点的版本、描述、id、组织id定义
- properties版本号统一管理
- dependencyManagement依赖管理
- dependencies 公用依赖管理
- build
- repositories
- distributionManagement
- developers
下面来详解。
2.1 基础配置
解析:
- modelVersion:指定了当前
POM
模型的版本,对于Maven2
及Maven 3
来说,它只能是4.0.0
; - groupId:项目组织唯一的标识符,实际对应
JAVA
的包的结构,是main
目录里java
的目录结构; - artifactId:就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称;
- description:描述
- version:版本号
- packaging:分为
pom
、jar
、war
下面继续讲解
2.1.1 packaging方式
packaging
默认是jar
类型,有几种打包方式:
- pom:父类型都为
pom
类型 - jar:内部调用或者是作服务使用
- war:需要部署的项目
2.2 properties配置
通过properties
元素用户可以自定义一个或多个Maven
属性,然后在POM
的其它地方使用**${属性名}**
的方式引用该属性。
这种做法的最大意义在于消除重复和统一管理。
2.3 dependency配置
dependencyManagement | dependencies |
描述:
- dependencyManagement:
dependencyManagement
里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version
和scope
都读取自父pom
;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。 - dependencies:即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
2.4 build配置
和前面说的dependencies
和 dependencyManagement
的区别是非常类似的
描述:
- plugins:
plugins
下的plugin
是真实使用的 - pluginManagement :
pluginManagement
下的plugins
下的plugin
则仅仅是一种声明,子项目中可以对pluginManagement
下的plugin
进行信息的选择、继承、覆盖等。
2.5 repositories配置
描述:
repositories
标签的作用是用来配置maven
项目的远程仓库;- 可以在
repositories
下面添加多个repository
; - 每个
repository
都有它唯一的ID
,一个描述性的name
,以及最重要的,远程仓库的url
; <releases><enabled>true</enabled></releases>
告诉Maven
可以从这个仓库下载releases
版本的构件,而<snapshots><enabled>false</enabled></snapshots>
告诉Maven不要从这个仓库下载snapshot
版本的构件(禁止从公共仓库下载snapshot
构件是推荐的做法,因为这些构件不稳定,且不受你控制,你应该避免使用。当然,如果你想使用局域网内组织内部的仓库,你可以激活snapshot的支持)。
2.6 distributionManagement配置
描述:
- mvn install 会将项目生成的构件安装到本地Maven仓库;
- mvn deploy 用来将项目生成的构件分发到远程Maven仓库;
- distributionManagement的作用是来指定Maven分发构件的位置。
- snapshot为开发过程中的版本,实时,但不稳定,release版本则比较稳定
- Maven会根据你项目的版本来判断将构件分发到哪个仓库
2.7 developers配置
描述:用来声明一些开发者的信息