简介
Maven是基于POM(Project Object Model)的项目管理工具,可以管理项目的构建、发布。
主要特点
- 简化编译(build)过程
- 统一开发规范与工具
- 统一管理jar包
好处
- 管理多个.jar文件。在开发Java应用时常常需要使用大量的第三方开发包,这些开发包大多数是.jar 的形式提供,比如使用spring 框架,通常的用法是下载这些.jar 文件,然后添加到Java下面中。部署时也要一些打包这些.jar 文件。使用Maven,在POM 中描述所需引用(依赖的)的库,在编译,发布,部署时Maven会自动下载或本地寻找所需库文件。因此使用Maven通常需要有网络连接。
- 管理Java项目的依赖和版本,Java依赖的库有不同的版本,提供在POM文件指定所引用库的版本号,Maven自动帮助管理这些依赖关系及其版本。
- 使用标准的项目结构,开发和测试人员可以使用标准的项目目录结构。
- Maven定义了项目开发的几个标准步骤:编译,发布,单元测试及部署以帮助项目开发。
安装
- 安装JDK,配置JAVA_HOME
- 下载Maven
- 解压,并把解压的bin目录加入PATH
- 测试
mvn -v
配置Maven
环境变量PATH
MAVEN_HOME
为maven安装目录,如D:\apache-maven-3.6.3
。
在path里增加mvn命令所在目录,如%MAVEN_HOME%\bin
。
配置国内镜像
在maven配置文件(%MAVEN_HOME%\conf\settings.xml
)里增加:
<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>
配置本地库
在maven配置文件里增加:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\maven_repos</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
配置maven本地库在D:\maven_repos
目录下。
测试:
mvn help:system
如果本地库目录下有文件了就是成功配置了。
在IntelliJ IDEA里配置maven
在IntelliJ的File Settings里,配置如下:
如果想默认都这样设置,需要在IntelliJ - File - Other Settings - Settings for New Projects… 里同样这样设置。
运行
命令格式
mvn [options] [<goal(s)>] [<phase(s)>]
初始化
最好使用阿里的代理,速度会快很多。
mvn archetype:generate
打包
mvn package
运行
java -cp .\HelloWorld-1.0-SNAPSHOT.jar com.pstreets.mavendemo.App
查看帮助信息
mvn -h
配置代理
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
配置文件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.iqilu</groupId>
<artifactId>iqilu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.2.8</version>
</dependency>
</dependencies>
</project>
使用Eclipse
###导入到workspace
File->Import->Maven->Existing Maven Projects
参考:
https://www.jianshu.com/p/1782feee6eff
https://developer.aliyun.com/mvn/guide