事情是这样子的(可略过)

  1. 某天,我发现pom中一个依赖无法导入,于是按照百度指引 来到setting-maven-repositories中更新远程仓库索引。
  2. 由于等待时间有点漫长,就关了intellij,中断了索引升级,重启后发现远程仓库索引变成了error,pom中最基本的库也无法导入了。
  3. 经过卸maven插件,更改远程仓库都无果的情况下,重装了inellij。然而,我卸载时没有删配置文件,再次来到setting-maven-repositories中时,发现,只有本地仓库了,也就意味着已经无法从远程仓库获取依赖了。
  4. 这说明了,决定索引的地方在intellij的配置文件中,这些配置文件又在用户家目录中的\.IdeaIC2018.1文件夹中。

解决

百度搜到的结果
略微改动下存了档,如下:


在intellij idea 中配置好maven后 是这样的

intellij idea maven添加远程仓库_apache

image

intellij idea maven添加远程仓库_maven_02

image

如果加载失败,则需要自定义远程仓库,这里以阿里maven仓库为例,

intellij idea maven添加远程仓库_远程仓库_03

image

在项目的pom文件中添加以下配置

<repositories>
<repository>
<id>alimaven</id>
<name>Maven Aliyun Mirror</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

repositories中可配置多个仓库repository(这里只配置了一个),id:唯一标识,name:自定义仓库名称,url:远程仓库地址,
releases 的enable设置为true,告诉maven可以下载releases(稳定版本)的构件;snapshots 的enable 为false,即为禁止下载snapshot(开发中、不稳定)的构件
其他仓库以供备用:

<mirror>  
<id>maven-net-cn</id>
<name>Maven China Mirror</name>
<url>http://mvnrepository.com/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>CN</id>
<name>OSChina Central</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

此时

intellij idea maven添加远程仓库_远程仓库_04

image

intellij idea maven添加远程仓库_apache_05

image

intellij idea maven添加远程仓库_maven_06

image

多试了几个镜像后,挑一个空闲时间,更新了远程仓库索引,终于可以正常使用了。

 

作者:cccccttttyyy
链接:https://www.jianshu.com/p/3f4e19a99ebc
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


今天将IntellIJ IDEA 关于Maven的配置总结一下,方便以后可参考。

 

IDEA版本:

IntelliJ IDEA 2017.2
Build #IU-172.3317.76, built on July 15, 2017
Licensed to Administrator

JRE: 1.8.0_152-release-915-b5 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1

 

IntelliJ IDEA下载地址:​​https://www.jetbrains.com/idea/download/#section=windows​

IntelliJ IDEA 是​​Java​​开发利器,用社区版不爽,干催就用旗舰版,这个是收费的,需要licence。 

如果你资金充足,请自行购买收费版的Idea:https://www.jetbrains.com/idea/buy。

 

一.配置Maven环境

1.下载apache-maven文件,选择自己需要的版本,地址: ​​http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.zip ​

2.解压1所下载文件,本人解压到:D:\server\maven\apache-maven-3.5.0

3.配置Maven环境变量

a. MAVEN_HOME : D:\server\maven\apache-maven-3.5.0

b.PATH : %MAVEN_HOME%\bin;

c. MAVEN_OPTS : -Xms128m -Xmx512m -Duser.language=zh -Dfile.encoding=UTF-8

4.在CMD中输入mvn -v,如出现下列信息,表示配置成功。

其中会显示Java 配置环境以及Maven配置环境

intellij idea maven添加远程仓库_maven_07

 

二.修改Maven仓库路径

1.在D盘(个人根据自身情况而定)新建文件,如下:

intellij idea maven添加远程仓库_apache_08

 

2.配置D:\server\maven\中setting.xml文件,内容如下:

intellij idea maven添加远程仓库_maven_09

<?xml version="1.0" encoding="UTF-8"?>
<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">

<pluginGroups />
<proxies />
<servers />

<localRepository>D:/server/maven/repository</localRepository>

<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>google-maven-central</id>
<name>Google Maven Central</name>
<url>https://maven-central.storage.googleapis.com
</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
<id>maven.net.cn</id>
<name>oneof the central mirrors in china</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

</settings>

intellij idea maven添加远程仓库_maven_09

 

 

三.IntellIJ IDEA 中配置Maven

Idea 自带了apache maven,默认使用的是内置maven,所以我们可以配置全局setting,来调整一下配置,比如远程仓库地址,本地编译环境变量等。

1.打开Settings,在输入框输入maven,如图

intellij idea maven添加远程仓库_maven_11

 

intellij idea maven添加远程仓库_远程仓库_12

 

2.如果本地设置了MAVEN_OPTS 系统环境变量,这个步骤可以忽略。

intellij idea maven添加远程仓库_apache_13

 

3.如果配置了本地apache-maven setting.xml 中的软件源,这步骤可以忽略。

intellij idea maven添加远程仓库_apache_14

 

 

好了,大功告成,,可以开始开心的撸代码了!