maven 仓库
maven 仓库:存储依赖的地方,maven仓库不仅存放依赖,而且每个依赖都有唯一标识(坐标),供 java 项目使用
maven 仓库分类:

依赖查找顺序:本地仓库 > 私服(如果配置) > 公共仓库(如果配置) > 中央仓库
- 本地仓库:setting.xml中配置的目录
- 中央仓库:由Maven社区提供,不用任何配置,Maven中内置了中央仓库的地址
- https://mvnrepository.com/ :仓库搜索服务
- http://repo.maven.apache.org/maven2 :中央仓库地址
- 公共仓库,用于加速,比如阿里云仓库
- 私服
maven 多项目管理
1、建立一个 maven 工程,packaging 为 pom 类型
2、统一管理依赖和版本号,子工程使用依赖时无需指定版本号,类似 Spring Boot 的父项目
3、父项目的 pom.xml
<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.spiritmark.cyf</groupId><artifactId>environment</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><!-- 集中定义依赖版本号 --><properties><junit.version>4.10</junit.version><spring.version>4.2.2.RELEASE</spring.version><mybatis.version>3.2.8</mybatis.version><mybatis.spring.version>1.2.2</mybatis.spring.version><mybatis.paginator.version>1.2.15</mybatis.paginator.version><mysql.version>5.1.47</mysql.version><slf4j.version>1.6.4</slf4j.version><jackson.version>2.4.2</jackson.version><druid.version>1.0.9</druid.version><httpclient.version>4.3.5</httpclient.version><jstl.version>1.2</jstl.version><servlet-api.version>2.5</servlet-api.version><jsp-api.version>2.0</jsp-api.version><joda-time.version>2.5</joda-time.version><commons-lang3.version>3.3.2</commons-lang3.version><commons-io.version>1.3.2</commons-io.version></properties><!-- 管理jar包,不会引入 ,如果子工程需要哪些jar包,则具体地在子工程中引入,不过不需要写版本号--><dependencyManagement><dependencies><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis.spring.version}</version></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- sl4j日志管理 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><!-- Jackson Json处理工具包 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><!-- 连接池 --><dependency><groupId>com.jolbox</groupId><artifactId>bonecp-spring</artifactId><version>0.8.0.RELEASE</version></dependency><!-- httpclient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>${httpclient.version}</version></dependency><!-- JSP相关 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>${servlet-api.version}</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>${jsp-api.version}</version><scope>provided</scope></dependency><!-- 时间操作组件 --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>${joda-time.version}</version></dependency><!-- Apache工具组件 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons-lang3.version}</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency></dependencies></dependencyManagement><build><finalName>${project.artifactId}</finalName><plugins><!-- 资源文件拷贝插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.7</version><configuration><encoding>UTF-8</encoding></configuration></plugin><!-- java编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins><pluginManagement><plugins><!-- 配置Tomcat插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version></plugin></plugins></pluginManagement></build></project>
4、在子项目中直接引用相关依赖,无需指定版本号
<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>com.spiritmark.cyf</groupId><artifactId>environment</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.spiritmark.cyf</groupId><artifactId>usermanage</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.10.1</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><!-- 时间操作组件 --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId></dependency><!-- spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency><!-- 切面 --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><!-- spring的jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.7.RELEASE</version></dependency><!-- mysql的驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></dependency><!-- mybatis于spring整合的jar --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId></dependency><!-- generator --><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency><!-- jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- pagehelper --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.2</version></dependency><!-- c3p0数据源 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!-- --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!-- --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!-- 文件上传 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.4.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.4.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><version>1.4.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></dependency></dependencies></project>
5、子项目插件配置:
<build><plugins><!-- 配置tomcat插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><!-- tomcat配置 --><configuration><!-- 端口号 --><port>8001</port><!-- 项目的路径 --><path>/</path></configuration></plugin></plugins></build>
子项目继承
pom.xml的继承关系:
- 父项目的packaging元素必须是pom
- 子项目的使用 parent 元素声明父模块:
- group、artifactId、version:指定父项目的坐标
- relativePath:表示父模块POM的相对路径,在项目构建时,maven会首先根据relativePath检查父POM
<parent><groupId>com.example</groupId><artifactId>parent</artifactId><version>1.0-SNAPSHOT</version><relativePath>../parent/pom.xml</relativePath></parent>
可继承的POM元素:
- groupId:项目组ID
- version:项目版本,所以子项目不需要写版本号
- distributionManagement:项目的部署配置
- properties:自定义的Maven属性
- dependencies:项目的依赖配置
- dependencyManagement:项目的依赖管理配置
- build:包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等
依赖管理:
- dependencyManagement元素说明:让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。在dependencyManagement元素下的依赖声明不会引入实际的依赖,不过它能够约束dependencies下的依赖使用。
插件管理:
- Maven提供了dependencyManagement元素帮助管理依赖,类似地,Maven也提供了pluginManagement元素帮助管理插件。同dependencyManagement一样,在pluginManagement元素中配置的依赖不会造成实际的插件调用行为,当POM中配置了真正的plugin元素,并且其groupId和artifactId与pluginManagement中配置的插件匹配时,pluginManagement的配置才会影响实际的插件行为。
依赖的生命周期
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.1.RELEASE</version><type>pom</type><!-- 生命周期 --><scope>import</scope></dependency>
maven的生命周期 scope 有如下几种:
- compile:默认,指该依赖用于maven项目的所有阶段(测试、编译、运行、打包),且会随着项目一起被发布(被打包)
- provided:该依赖只需要在编译过程中提供,不会被打包
- runtime:只在运行时使用,适用运行和测试阶段,会被一起发布
- test:该依赖只用于测试,用于编译和运行测试,不会被打包
- system:类似provided,但maven不会在Repository中查找,而是在本地磁盘目录中查找,参与编译、测试、打包、运行
- import:用于导入子模块中使用dependencyManagement管理的依赖,注意,scope=import只能用在dependencyManagement里面,并且 type=pom 的dependency
import的出现缘由
在maven多模块项目中,为了保持模块间依赖的统一,常规做法是在parent model中,使用dependencyManagement预定义所有模块需要用到的dependency(依赖)。然后子model中根据实际需要引入parent中预定义的依赖。
优点:
- 依赖统一管理(parent中定义,需要变动dependency版本,只要修改一处即可);
- 代码简洁(子model只需要指定groupId、artifactId即可)
- dependencyManagement只会影响现有依赖的配置,但不会引入依赖。即子model不会继承parent中dependencyManagement所有预定义的depandency,只引入需要的依赖即可,简单说就是“按需引入依赖”或者“按需继承”。因此,在parent中严禁直接使用depandencys预定义依赖,坏处是子model会自动继承depandencys中所有预定义依赖。
问题:
- 单继承:maven的继承跟java一样,单继承,也就是说子model中只能出现一个parent标签;parent模块中,dependencyManagement中预定义太多的依赖,造成pom文件过长,无法清晰管理。
解决:import scope依赖
使用:
- maven2.9以上版本
- 将dependency分类,每一类建立单独的pom文件
- 在需要使用到这些依赖的子model中,使用dependencyManagement管理依赖,并import scope依赖
- 注意:scope=import只能用于dependencyManagement中,且仅用于type=pom的dependency
优点:
- 便于依赖管理,分类
- 解决单继承问题,通过import pom文件达到依赖的目的(典型的非继承模式),从而不用从父类中引用依赖
- 父模块的pom会很干净,便于维护
