安装 Maven
环境准备
Maven 需要 JDK 支持,请先下载安装 JDK 。
通常要求 JDK 1.8 或以上版本,推荐 Hotspot JDK 1.8 或 OpenJDK 1.8 。
参考:《安装 JDK》
下载 Maven
从官网下载 Maven 安装包,解压缩到自行选定的目录下,该目录作为 Maven 的主目录,通常将其配置为环境变量 M2_HOME
。
Maven 主目录(M2_HOME
) 下需要关注的目录和文件如下:
M2_HOME
|—— bin # Maven 可执行脚本目录
|
|—— conf
| |__ settings.xml # Maven 配置文件;
|
|__ libs # Maven 代码库;
设置环境变量
Linux/MacOS
假设将 Maven 安装包解压缩到目录 /usr/local/ci/maven/maven-3.8.4
vi /etc/profile
# 在 /etc/profile 的末尾加入以下两行
export M2_HOME=/usr/local/ci/maven/maven-3.8.4
export PATH=$PATH:$M2_HOME/bin
# :wq 保存退出后,执行配置使更改立即生效
source /etc/profile
vi /etc/zprofile
# 在 /etc/zprofile 的末尾加入以下两行
export M2_HOME=/usr/local/ci/maven/maven-3.8.4
export PATH=$PATH:$M2_HOME/bin
# :wq 保存退出后,执行配置使更改立即生效
source /etc/zprofile
Windows
假设将 Maven 安装包解压缩到目录 D:\maven\maven-3.8.4
右键 "计算机",选择 "属性",之后点击 "高级系统设置",点击"环境变量",来设置环境变量。
新建环境变量
M2_HOME
变量值:
D:\maven\maven-3.8.4
编辑系统变量
Path
变量值末尾加入:
;%M2_HOME%\bin
验证安装
输入命令 mvn -v
查看 Maven 版本,验证安装是否正确。
% mvn -v
显示结果类似于以下情况:
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: /usr/local/ci/maven/maven-3.8.4
Java version: 1.8.0_212, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"
配置 Maven
在正式进行开发之前,建议参考以下方法,打开 M2_HOME/conf/settings.xml
配置文件,对 Maven 进行配置,可以大幅优化 Maven 的执行效率。
本地仓库
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 本地仓库(默认值:${user.home}/.m2/repository) -->
<localRepository>/usr/local/ci/maven/repo</localRepository>
</settings>
这是可选操作。
由于本地仓库目录是用来保存从 Maven 下载过的所有代码包,随着时间推移可能会持续增加,因此建议将路径更改到空间足够大、性能更好的磁盘目录下。
镜像仓库
给 Maven 配置国内的镜像仓库下载代码包,避免从 Maven 中央仓库直接下载,能够显著加速代码包的下载速度。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 镜像仓库 -->
<mirrors>
<!-- 阿里云提供的镜像仓库 -->
<mirror>
<id>aliyun</id>
<name>Aliyun Central</name>
<url>https://maven.aliyun.com/repository/central</url>
<!-- 只镜像 Maven 的中央仓库 -->
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
其它镜像仓库
腾讯云镜像仓库:
(公网)http://mirrors.cloud.tencent.com/nexus/repository/maven-public
(内网)http://mirrors.tencentyun.com/nexus/repository/maven-publicMaven 官方欧洲镜像库:http://uk.maven.org/maven2/
星河低代码仓库
星河低代码开发框架代码包最新的版本最先发布在 maven.linkgie.com
的公开仓库 https://maven.linkgie.com/repository/maven-public/
上,因此,建议在搭建开发环境时加入 maven.linkgie.com
的仓库配置。有两种可选的配置方法:全局配置 和 项目级配置。
全局配置
Maven 的全局配置文件位于 $M2_HOME/conf/settings.xml
.
注意
这一步骤是可选的。
由于 GDK 生成应用项目时,默认会在应用项目根模块的
pom.xml
中加入仓库配置,所以,不必须设置 Maven 全局配置文件。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 星河低代码仓库 -->
<profiles>
<profile>
<id>linkgie</id>
<repositories>
<repository>
<id>linkgie-repo</id>
<url>https://maven.linkgie.com/repository/maven-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>linkgie-repo</id>
<url>https://maven.linkgie.com/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>linkgie</activeProfile>
</activeProfiles>
</settings>
项目级配置
项目级配置是在应用的 Maven 项目文件 pom.xml
中通过 <repositories> ... </repositories>
或 <pluginRepositories> ... </pluginRepositories>
标签指定 maven.linkgie.com 仓库。
注意
这一步骤不需要手工配置。
默认情况下, GDK 生成应用项目时会在应用项目根模块的
pom.xml
中加入如下的 maven.linkgie.com 仓库配置
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- ... -->
<repositories>
<repository>
<id>linkgie-repo</id>
<url>https://maven.linkgie.com/repository/maven-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>linkgie-repo</id>
<url>https://maven.linkgie.com/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
</project>
完整的全局配置
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 本地仓库(默认值:${user.home}/.m2/repository) -->
<localRepository>/usr/local/ci/maven/repo</localRepository>
<!-- 镜像仓库 -->
<mirrors>
<!-- 阿里云提供的镜像仓库 -->
<mirror>
<id>aliyun</id>
<name>Aliyun Central</name>
<url>https://maven.aliyun.com/repository/central</url>
<!-- 只镜像 Maven 的中央仓库 -->
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!-- 星河低代码仓库(可选配置) -->
<profiles>
<profile>
<id>linkgie</id>
<repositories>
<repository>
<id>linkgie-repo</id>
<url>https://maven.linkgie.com/repository/maven-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>linkgie-repo</id>
<url>https://maven.linkgie.com/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活星河低代码仓库(可选配置) -->
<activeProfiles>
<activeProfile>linkgie</activeProfile>
</activeProfiles>
</settings>