maven的安装
maven的安装依赖于java环境,首先安装java环境然后安装maven并且在系统中进行配置
打开apache-maven-3.8.1\conf\settings.xml文件进行如下配置
配置阿里云镜像提高下载速度
这段代码要放在<mirrors></mirrors> 标签中
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
设置本地仓库
<localRepository>D:\Maven\maven-repository</localRepository>
设置java版本
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
maven的作用
1,依赖管理
2,项目构建(对项目的编译,测试,打包,部署...等等)
maven坐标含义
<dependency>
<!-- 组id 我们在创建maven项目时组id就是我们打包到本地仓库的位置-->
<groupId>junit</groupId>
<!-- 项目名 -->
<artifactId>junit</artifactId>
<!-- 版本-->
<version>4.13.1</version>
<!-- 作用域默认值为compile -->
<scope>test</scope>
</dependency>
maven常见作用域及作用范围
provided表示这个jar包服务器已经提供了,在我们部署项目时jar包不会被部署到服务器上仅在本地起作用
打包方式
<packaging>xxx</packaging>
pom:当项目存在继承关系时父项目的打包方式为pom
jar: Java项目打包方式
war:web项目打包方式
依赖传递
依赖排除
A依赖希望排除C依赖则只需在引入B依赖的时候进行如下操作
<dependency>
<exclusions>
<exclusion>
<groupId>xxx</groupId>
<artifactId>C</artifactId>
</exclusion>
</exclusions>
<groupId>xxx</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
</dependency>
maven项目继承
maven项目继承的主要目的是实现父项目对子项目的管理列如依赖管理
虽然子工程继承了父工程但是我们要在子工程中声明依赖坐标才能使用依赖
打包方式为pom的工程才可以作为父工程
<!-- 父工程的pom.xml文件 -->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- 创建自定义标签使用${标签名}来进行使用 -->
<com.test>4.12</com.test>
</properties>
<!-- 父工程对子工程的依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${com.test}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
子工程的pom.xml文件
<!-- 通过<parent></parent>标签引入父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 在子工程的依赖中我们如果不配置版本号那么就使用父工程配置好的
如果子工程配置的版本号和父工程不一样那么使用子工程的版本号 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>