Maven项目管理工具
简介
Maven的本质时一个项目管理工具,将项目开发和管理过程抽象成一个项目对象模型POM(Project Object Model)
作用:
- 项目构建:提供标准的、跨平台的自动化项目构建方式
- 依赖管理:方便快捷的管理项目依赖的资源(jar包),避免资源间的版本冲突问题
- 统一开发结构:提供标准的、统一的项目结构
下载
- 官网http://maven.apache.org/
- 下载地址http://maven.apache.org/download.cgi
- 需配置环境变量
基础概念
- 仓库:用于存储资源,包含各种jar包
仓库分类:-
本地仓库:自己电脑上存储资源的仓库
-
远程仓库:非本机上的仓库
- 中央仓库:Maven团队维护,存储所有资源的仓库
- 私服:部门/公司范围内的仓库,从中央仓库获取资源
-
私服作用:
- 保存具有版权的资源,包含购买或自主研发的jar(中央仓库都是开源的jar)
- 对内部开放
-
- 依赖:描述仓库中资源的位置
组成:- groupId:定义当前Maven项目所有组织名称(通常时域名反写,如:org.mybatis)
- artifactId:定义当前Maven项目名称(通常是模块名称)
- version:定义当前项目版本号
作用:使用唯一标识,唯一行定位资源位置
- 在config/setting.xml配置连接仓库地址和配置阿里云镜像仓库
<localRepository>F:\repository</localRepository> <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
依赖配置
<!--dependencies下可有依赖多个jar,使用depenency标签-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 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>
</build>
分模块开发
-
聚合
创建一个空模块作为父模块,打包类型为pom
<groupId>com.it</groupId> <artifactId>project-parent</artifactId> <version>1.0-SNAPSHOT</version> <!--定义当前模块进行构建时关联的其他模块--> <modules> <module>project-common</module> <module>project-interface</module> <module>project-service-provider</module> <module>project-backend</module> </modules> <!--版本管理--> <properties> <spring.version>5.1.9.RELEASE</spring.version> </properties> <!--依赖管理标签,子模块调用时才依赖--> <dependencyManagement> <!--定义依赖的jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependencyManagement>
-
继承
实现在子工程中使用父工程的配置
<!--定义父工程--> <parent> <artifactId>project-parent</artifactId> <groupId>com.it</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>project-common</artifactId> <packaging>jar</packaging> <!--继承父工程的依赖,版本由父工程统一管理--> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> </dependencies>
-
模块类型
pom工程:用在父级工程或聚合工程中。用来做jar包的版本控制。
war工程:将会打包成war,发布在服务器上的工程。如网站或服务。
jar工程:将会打包成jar用作jar包使用。