大家好,我是水滴~~
IntelliJ IDEA 集成了 Maven 的全部功能,可以让我们轻松的创建和管理 Maven 项目。本文主要讲述在 IntelliJ IDEA 中,如何高效地使用 Maven,这需要你有一定的 Maven 基础。
文章目录
一、Maven 的下载、安装和配置
为了方便以后装机时需要,这里把 Maven 的下载、安装和配置记录一下,已经装过的童鞋可以直接看下一章。
1. 下载
如果想下载历史版本,可以点击【Previous Releases】中的 archives 链接。
如果下载速度慢,可以使用迅雷
2. 安装
将下载好的压缩包,解压到指定目录即可。Maven 的目录结构如下图:
3. 配置
3.1 配置本地仓库目录
<!-- 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:\apache-maven-3.8.5-repository</localRepository>
3.2 添加国内镜像源
<!-- 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>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
3.3 设置 Maven 的 JDK 版本
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<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>
3.4 配置环境变量
二、IDEA 配置 Maven 环境
刚安装好的 IntelliJ IDEA 需要我们配置一下 Maven 环境,使我们 Maven 项目使用同一环境。下面开始配置:
在右侧选择我们安装的 Maven 环境:
① 选择 Maven 安装目录
② 勾选【Overrid】后,选择 settings.xml 文件
③ 选择本地仓库目录(操作完 ① 后,会自动选择目录)
三、IDEA 创建 Maven 项目
在 IntelliJ IDEA 中可以轻松的创建 Maven 项目,并为其创建模块(Module)。
1. 创建 Maven 项目
注:如果想要根据模板创建 Maven 项目,请勾选【Create from archetype】,然后选择一个模板。关于模板的介绍,可以参考 Maven 官网:Introduction to Archetypes
下面填写项目信息,其中:
① 项目名称
② 项目的本地目录
③ ④ ⑤ 为 Maven 项目的“GAV坐标”
下图为该 Maven 项目的目录结构,其中 pom.xml
文件已创建好。
2. 创建模块(Module)
下面填写模块信息,其中:
① 上级项目名称,默认为刚才选中的项目
② 模块名称
③ 模块的本地目录
④ ⑤ ⑥ 为 Maven 模块的“GAV坐标”
下图为该 Maven 模块的目录结构,同样 pom.xml
文件已创建好。
并且,在上级项目的 pom.xml
文件,自动增加了 modules
模块。
当然,一个项目可以有多个模块,创建方式相同。下图中创建了多个模块:
四、IDEA 执行 Maven 命令
1. 通过“终端”执行命令
1.1 打包命令
1.2 安装命令
2. 通过“Maven 工具窗口”执行命令
如果你不想手动输出命令,还可以通过 Maven 工具窗口来执行命令。
2.1 打开 Maven 工具窗口
2.2 执行命令
3. 通过“Maven 运行/调试配置”执行命令
我们可以将常用的命令组添加到“运行配置”中,便于我们后面的使用。下面介绍如何添加配置和执行命令。
此处可以添加多个配置,用于执行不同的命令组。
五、IDEA 管理 Maven 依赖
1. 添加并加载依赖
2. 重新加载依赖
如果有些依赖没有下载,可以重新加载依赖
3. 下载源码
4. 配置自动下载源码
这样后面再下载依赖时,会将源码一同下载到本地。