下载Maven
- 搜索进入[maven官网]:(https://maven.apache.org/);
- 选择并下载合适的版本(最好选择最新版本的前一个版本)及合适格式(ubuntu下选择tar.gz格式,windows下选择zip格式)的maven;
- 这里我选择的是3.6.1版本的maven,默认下载到了
~/下载
路径下。
安装Maven
- 进入maven下载路径下
cd ~/下载
- 解压文件
tar -zxvf apache-maven-3.6.1
- 将文件移动到合适的路径下(通常为
/opt
或者/usr/local
路径下)sudo mv apache-maven-3.6.1 /usr/local/apache/
配置Maven
-
配置环境变量
*
- 编辑配置文件(全局用户配置或者个人用户配置)
sudo vim /etc/profile 或者 vim ~/.profile
- 添加下面命令(M2_HOME为变量名,然后是maven文件路径)
export M2_HOME=/usr/local/apache/apache-maven-3.6.1 export PATH=${M2_HOME}/bin:$PATH
- 编辑配置文件(全局用户配置或者个人用户配置)
-
配置国内阿里云镜像来加速
- 编辑maven配置文件
vim /usr/local/apache/apache-maven-3.6.1/conf/settings.xml
- 更换为阿里云镜像源
<mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror>
- 配置参考自官方文档
- 编辑maven配置文件
使用Maven
- 帮助命令
mvn -help
踩坑1:eclipse创建maven项目时失败
-
报错如下:
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any of the configured repositories. Could not resolve artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 Failure to transfer org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from/to central (https://repo.maven.apache.org/maven2): connect timed out Failure to transfer org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from/to central (https://repo.maven.apache.org/maven2): connect timed out -
分析及解决方案:
- 简要分析: 翻译:maven-archetype-webapp:pom:1.0已经缓存在本地了,直到中心更新间隔已过或者强制更新之前不会再有解决方案。
- 解决方案:删除maven-archetype本地缓存
sudo rm -rf ~/.m2/repository/org/apache/maven/archetypes/maven*
踩坑2:maven pom文件报错
- 报错如下:
Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.22.2 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. - 解决方案:
- 删除本地仓库此插件:maven-surefire-plugin
rm -rf ~/.m2/repository/org/apache/maven/plugins/maven-surefire-plugin
- 更新项目
右键项目->maven->Update Project 勾上Force Update of Snapshots/Releases即可。
- 删除本地仓库此插件:maven-surefire-plugin