Ubuntu18.04下安装配置Maven及踩坑记

文章目录

  • 下载Maven
  • 安装Maven
  • 配置Maven
  • 使用Maven
  • 踩坑1:eclipse创建maven项目时失败
  • 踩坑2:maven pom文件报错

下载Maven

  1. 搜索进入[maven官网]:(https://maven.apache.org/);
  2. 选择并下载合适的版本(最好选择最新版本的前一个版本)及合适格式(ubuntu下选择tar.gz格式,windows下选择zip格式)的maven;
  3. 这里我选择的是3.6.1版本的maven,默认下载到了~/下载路径下。

安装Maven

  1. 进入maven下载路径下
    cd ~/下载
    
  2. 解压文件
    tar -zxvf apache-maven-3.6.1
    
  3. 将文件移动到合适的路径下(通常为/opt 或者/usr/local路径下)
    sudo mv apache-maven-3.6.1 /usr/local/apache/
    

配置Maven

  • 配置环境变量*

    1. 编辑配置文件(全局用户配置或者个人用户配置)
      sudo vim /etc/profile 或者 vim ~/.profile
      
    2. 添加下面命令(M2_HOME为变量名,然后是maven文件路径)
      export M2_HOME=/usr/local/apache/apache-maven-3.6.1
      export PATH=${M2_HOME}/bin:$PATH
      
  • 配置国内阿里云镜像来加速

    1. 编辑maven配置文件
      vim /usr/local/apache/apache-maven-3.6.1/conf/settings.xml
      
    2. 更换为阿里云镜像源
      <mirror>
      <id>aliyunmavenid>
      <mirrorOf>centralmirrorOf>
      <name>阿里云公共仓库name>
      <url>https://maven.aliyun.com/repository/publicurl>
      mirror>
      
    3. 配置参考自官方文档

使用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

  • 分析及解决方案:

    1. 简要分析: 翻译:maven-archetype-webapp:pom:1.0已经缓存在本地了,直到中心更新间隔已过或者强制更新之前不会再有解决方案。
    2. 解决方案:删除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.
  • 解决方案:
    1. 删除本地仓库此插件:maven-surefire-plugin
      rm -rf ~/.m2/repository/org/apache/maven/plugins/maven-surefire-plugin
      
    2. 更新项目
      右键项目->maven->Update Project 勾上Force Update of Snapshots/Releases即可。

你可能感兴趣的:(Linux)