<modelVersion>4.0.0modelVersion>
<groupId>com.companyname.project-groupgroupId>
<artifactId>projectartifactId>
<version>1.0.0version>
<packaging>jar/pom/warpackaging>
<dependencies>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
<scope>runtimescope>
dependency>
dependencies>
<properties>
<junit.version>4.11junit.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
dependencies>
短路优先原则(第一原则)
A—>B—>C—>D—>E—>X(version 0.0.1)
A—>F—>X(version 0.0.2)
则A依赖于X(version 0.0.2)
依赖路径长度相同情况下,则“先声明优先”(第二原则)
A—>E—>X(version 0.0.1)
A—>F—>X(version 0.0.2)
在
前提:
A 1.1 -> B 1.1 -> C 1.1
F 2.2 -> B 2.2
pom声明:
F 2.2
A 1.1
B 2.2
在使用 Maven 构建项目时,可能会发生依赖项下载错误的情况,主要原因有以下几种:
解决方案:
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.8version>
dependency>
命令 | 描述 |
---|---|
mvn clean | 清理编译或打包后的项目结构,删除target文件夹 |
mvn compile | 编译项目,生成target文件 |
mvn test | 执行测试源码 (测试) |
mvn site | 生成一个项目依赖信息的展示页面 |
mvn package | 打包项目,生成war / jar 文件 |
mvn install | 打包后上传到maven本地仓库(本地部署) |
mvn deploy | 只打包,上传到maven私服仓库(私服部署) |
构建生命周期可以理解成是一组固定构建命令的有序集合,触发周期后的命令,会自动触发周期前的命令!也是一种简化构建的思路!
清理周期:主要是对项目编译生成文件进行清理
默认周期:定义了真正构件时所需要执行的所有步骤,它是生命周期中最核心的部分
报告周期
打包: mvn clean package 本地仓库: mvn clean install
打包: mvn clean package
重新编译: mvn clean compile
本地部署: mvn clean install
周期,命令和插件:
周期→包含若干命令→包含若干插件!
使用周期命令构建,简化构建过程!
最终进行构建的是插件!
插件配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
plugins>
build>
作用:在父工程中统一管理项目中的依赖信息,进行统一版本管理!
它的背景是:
它背后的需求是:
通过在父工程中为整个项目维护依赖信息的组合既保证了整个项目使用规范、准确的 jar 包;又能够将以往的经验沉淀下来,节约时间和精力。
<groupId>com.alex.mavengroupId>
<artifactId>pro03-maven-parentartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<parent>
<groupId>com.alex.mavengroupId>
<artifactId>pro03-maven-parentartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>pro04-maven-moduleartifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>4.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.0.0.RELEASEversion>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
dependency>
dependencies>
<project>
<groupId>com.examplegroupId>
<artifactId>parent-projectartifactId>
<packaging>pompackaging>
<version>1.0.0version>
<modules>
<module>child-project1module>
<module>child-project2module>
modules>
project>
需求案例:搭建一个电商平台项目,该平台包括用户服务、订单服务、通用工具模块等。
项目架构:
服务依赖:
<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.0modelVersion>
<groupId>com.alexgroupId>
<artifactId>micro-shopartifactId>
<version>1.0.1version>
<packaging>pompackaging>
<properties>
<spring.version>6.0.6spring.version>
<jackson.version>2.15.0jackson.version>
<shiro.version>1.10.1shiro.version>
<commons.version>2.11.0commons.version>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-coreartifactId>
<version>${shiro.version}version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>${commons.version}version>
dependency>
dependencies>
dependencyManagement>
<dependencies>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
plugins>
build>
project>
<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.0modelVersion>
<parent>
<groupId>com.alexgroupId>
<artifactId>micro-shopartifactId>
<version>1.0.1version>
parent>
<artifactId>common-serviceartifactId>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
dependency>
dependencies>
project>
<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.0modelVersion>
<parent>
<groupId>com.alexgroupId>
<artifactId>micro-shopartifactId>
<version>1.0.1version>
parent>
<artifactId>user-serviceartifactId>
<packaging>warpackaging>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
dependency>
dependencies>
project>
<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.0modelVersion>
<parent>
<groupId>com.alexgroupId>
<artifactId>micro-shopartifactId>
<version>1.0.1version>
parent>
<artifactId>order-serviceartifactId>
<packaging>warpackaging>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
dependency>
<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-coreartifactId>
dependency>
dependencies>
project>
核心点 | 掌握 |
---|---|
安装 | maven安装、环境变量、maven配置文件修改 |
工程创建 | gavp属性理解、JavaSE/EE工程创建、项目结构 |
依赖管理 | 依赖添加、依赖传递、版本提取、导入依赖错误解决 |
构建管理 | 构建过程、构建场景、构建周期等 |
继承和聚合 | 理解继承和聚合作用、继承语法和实践、聚合语法和实践 |