dependencyManagement和dependencies区别和联系
参考:https://blog.csdn.net/Sunshineoe/article/details/121083505
<?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>io.qar</groupId><artifactId>qar-commons-dependencies</artifactId><packaging>pom</packaging><version>${revision}</version><properties><revision>3.10.0</revision><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><knife4j.version>2.0.9</knife4j.version><spring.boot.admin.version>2.7.5</spring.boot.admin.version><captcha.version>1.6.2</captcha.version><druid.version>1.2.12</druid.version><mybatisplus.version>3.5.2</mybatisplus.version><sqlserver.version>4.0</sqlserver.version><oracle.version>11.2.0.3</oracle.version><dameng.version>8.1.2.141</dameng.version><commons.lang.version>3.7</commons.lang.version><commons.fileupload.version>1.3.3</commons.fileupload.version><commons.io.version>2.6</commons.io.version><hutool.version>5.7.22</hutool.version><easyexcel.version>3.0.5</easyexcel.version><joda.time.version>2.9.9</joda.time.version><gson.version>2.8.6</gson.version><jsoup.version>1.11.3</jsoup.version><lombok.version>1.18.22</lombok.version><xxl-job.version>2.3.1</xxl-job.version><quartz.version>2.3.2</quartz.version><aliyun.core.version>3.2.2</aliyun.core.version><aliyun.dysmsapi.version>1.1.0</aliyun.dysmsapi.version><qcloud.qcloudsms.version>1.0.5</qcloud.qcloudsms.version><qiniu.version>7.10.4</qiniu.version><aliyun.oss.version>2.8.3</aliyun.oss.version><qcloud.cos.version>5.4.4</qcloud.cos.version><fastdfs.version>1.27.2</fastdfs.version><minio.version>8.4.1</minio.version><freemarker.version>2.3.31</freemarker.version><mail.version>1.6.2</mail.version><ureport2.version>2.2.9</ureport2.version><IJPay.version>2.8.2</IJPay.version><weixin-java-mp.version>4.2.0</weixin-java-mp.version><xstream.version>1.4.20</xstream.version><spring-security-oauth2.version>2.3.8.RELEASE</spring-security-oauth2.version></properties><dependencyManagement><dependencies><dependency><groupId>io.qar</groupId><artifactId>qar-commons-dynamic-datasource</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-lock</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-log</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-mybatis</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-security</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-swagger</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-tools</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-xxl-job</artifactId><version>${revision}</version></dependency><dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId><version>${captcha.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>${oracle.version}</version></dependency><dependency><groupId>com.microsoft.sqlserver</groupId><artifactId>sqljdbc4</artifactId><version>${sqlserver.version}</version></dependency><dependency><groupId>com.dameng</groupId><artifactId>DmJdbcDriver18</artifactId><version>${dameng.version}</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatisplus.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>${druid.version}</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>${knife4j.version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons.lang.version}</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>${commons.fileupload.version}</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>${commons.io.version}</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>${easyexcel.version}</version></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>${joda.time.version}</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>${gson.version}</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>${jsoup.version}</version></dependency><dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>${xxl-job.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.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>${quartz.version}</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>${aliyun.core.version}</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>${aliyun.dysmsapi.version}</version></dependency><dependency><groupId>com.github.qcloudsms</groupId><artifactId>qcloudsms</artifactId><version>${qcloud.qcloudsms.version}</version></dependency><dependency><groupId>com.qiniu</groupId><artifactId>qiniu-java-sdk</artifactId><version>${qiniu.version}</version></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>${aliyun.oss.version}</version></dependency><dependency><groupId>com.qcloud</groupId><artifactId>cos_api</artifactId><version>${qcloud.cos.version}</version></dependency><dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>${fastdfs.version}</version></dependency><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>${minio.version}</version></dependency><dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>${mail.version}</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>${freemarker.version}</version></dependency><dependency><groupId>com.bstek.ureport</groupId><artifactId>ureport2-console</artifactId><version>${ureport2.version}</version></dependency><dependency><groupId>com.github.javen205</groupId><artifactId>IJPay-AliPay</artifactId><version>${IJPay.version}</version></dependency><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>${weixin-java-mp.version}</version></dependency><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>${xstream.version}</version></dependency><dependency><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId><version>${spring-security-oauth2.version}</version></dependency></dependencies></dependencyManagement></project>
project-root的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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.7</version></parent><groupId>io.qar</groupId><artifactId>qar-cloud</artifactId><version>${revision}</version><packaging>pom</packaging><description>qar微服务平台,是基于Spring Cloud打造的微服务平台,为企业提供一站式微服务解决方案!</description><organization><name>qar</name><url>https://www.qar.io/community</url></organization><modules><module>qar-commons</module><module>qar-gateway</module><module>qar-monitor</module><module>qar-auth</module><module>qar-admin</module><module>qar-module</module><module>qar-new</module></modules><properties><revision>3.10.0</revision><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring.cloud.version>2021.0.5</spring.cloud.version><spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>2.1.1</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud.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.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-dependencies</artifactId><version>${revision}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><repositories><repository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public/</url><releases><enabled>true</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>
项目pom中要想包含子模块,需要配置modules其中module是子模块的artifactId值
<modules><module>qar-commons</module><module>qar-gateway</module><module>qar-monitor</module><module>qar-auth</module><module>qar-admin</module><module>qar-module</module><module>qar-new</module>
</modules>
子模块想要继承父级模块的dependency依赖包,需要配置parent指定继承节点
<parent><groupId>io.qar</groupId><artifactId>qar-cloud</artifactId><version>${revision}</version>
</parent>
每一个模块必须要有groupId、artifactId、packaging、version:
<groupId>io.qar</groupId>
<artifactId>qar-commons</artifactId>
<packaging>pom</packaging>
每个模块的变量先在当前配置中查找,如果没有就去父模块查找:
<version>${revision}</version>
属性变量的配置:
<properties><revision>3.10.0</revision><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring.cloud.version>2021.0.5</spring.cloud.version><spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version>
</properties>
repositories 远程仓库配置:
唯一的ID,一个描述性的name,远程仓库的url, true告诉Maven可以从这个仓库下载releases版本的构件
<repositories><repository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public/</url><releases><enabled>true</enabled></releases></repository>
</repositories>
pluginRepositories远程插件仓库配置:
<pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository>
</pluginRepositories>
运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获取的。如果本地仓库没有,它会首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件
当我们运行install的时候,Maven实际上是将项目生成的构件安装到了本地仓库,即只有install了之后,其它项目才能使用此项目生成的构件
packaging的配置:
pom:父类型都为pom类型, 在maven install时不会生成jar/war压缩包
<packaging>pom</packaging>
jar:内部调用或者是作服务使用,是默认的打包方式,
这种打包方式意味着在maven build时会将这个项目中的所有java文件都进行编译形成.class文件,且按照原来的java文件层级结构放置,最终压缩为一个jar文件
<packaging>jar</packaging>
war:打包项目,用于在容器(Tomcat、Jetty等)上部署
war包与jar包非常相似,同样是编译后的.class文件按层级结构形成文件树后打包形成的压缩包。不同的是,它会将项目中依赖的所有jar包都放在WEB-INF/lib这个文件夹下
war包非常适合部署时使用,不再需要下载其他的依赖包,能够使用户拿到war包直接使用,因此它经常使用于微服务项目群中的入口项目的pom配置中
<packaging>war</packaging>
build配置
指定install打包的包名
<build><finalName>${project.artifactId}</finalName>
</build>
#默认的build
<build> <defaultGoal>install</defaultGoal> <directory>${basedir}/target</directory> <finalName>${artifactId}-${version}</finalName> <filters><filter>filters/filter1.properties</filter></filters> ...
</build>
全局配置(project build)
针对整个项目的所有情况都有效
配置(profile build)
针对不同的profile配置
参考: https://blog.csdn.net/zmemorys/article/details/105406053
openfeign调用配置pom
在A微服务中采用feign调用,一个服务提供当前服务使用的接口和提供给外部使用的接口,此时, 创建两个模块,一个模块给自身提供服务接口,一个模块提供外部使用的接口(这个模块有DTO的定义,有DTO的定义就需要swagger的配置)所以该模块中依赖swagger构件,为了减少重复代码自身服务的模块需要依赖外部模块的构件,使用它的DTO和swagger依赖构件。
当外部B服务调用前者A服务的openfeign时,为了不需要重复写feign调用,需要依赖前者的A服务构件,这样的结构设计可以最大程度的简化代码避免冗余代码出现,也可以很好的管理服务间的调用,服务的提供方和服务的调用方,内部服务和外部服务很好的隔离组织在一起啦!
qar-flow-server内部服务的pom配置:
依赖提供外部服务的qar-flow-client构件和调用三方服务的qar-admin-client构件,client构件中依赖swagger和tools工具构件;
<dependency><groupId>io.qar</groupId><artifactId>qar-flow-client</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-admin-client</artifactId><version>${revision}</version></dependency><dependency><groupId>io.qar</groupId><artifactId>qar-commons-log</artifactId>
</dependency>
<dependency><groupId>io.qar</groupId><artifactId>qar-commons-mybatis</artifactId>
</dependency>
provided配置:
<scope>provided</scope>
使用provided标签的好处是可以减小最终打包的项目的大小,避免将不必要的库打包进去。同时,也方便服务器在运行时提供具体的库。
在install打包的qar-commons-dynamic-datasource
的jar中,没有qar-commons-mybatis,但编译运行不受影响,如果有外部项目使用qar-commons-dynamic-datasource构件,就需要单独引入qar-commons-mybatis依赖构件;
好处: 既方便依赖包的使用,也防止重复打包,减少代码冗余,减轻服务器的磁盘占用
<artifactId>qar-commons-dynamic-datasource</artifactId>
<packaging>jar</packaging><dependencies><dependency><groupId>io.qar</groupId><artifactId>qar-commons-mybatis</artifactId><scope>provided</scope></dependency>
</dependencies><build><finalName>${project.artifactId}</finalName>
</build>
qar-admin-server服务的pom配置依赖,qar-commons-mybatis和qar-commons-dynamic-datasource构件
<dependency><groupId>io.qar</groupId><artifactId>qar-commons-mybatis</artifactId>
</dependency>
<dependency><groupId>io.qar</groupId><artifactId>qar-commons-dynamic-datasource</artifactId>
</dependency>