一.那么问题来了
在使用idea过程中经常遇到这个问题:
索引更新不下来,在pom中写依赖的时候就无法获得提示,网上试了很多的方法,用处都不大,这索引的下载完全看心情,时好时坏,idea已经知道这个异常了,但是好像还是没有修复。所以,我决定深挖到底,看看到底是怎么回事。
二.环境:win10,mac
之前只在windows环境下找到了索引文件的位置,mac没找到,我给idea发了邮件询问linux环境下索引文件位置,但是没有回复(万恶的资本主义)。后来发现了。
三.索引文件的位置
windows:~/.IntelliJIdea2017.3/system/Maven
mac:~/Library/Caches/IntelliJIdea2018.2/Maven
注:~是用户根目录,.IntelliJIdea2017.3是个隐藏文件夹,后面的日期是idea的版本,每个人可能不一样。
Indices
├── Index0
│ ├── data0
│ │ ├── archetypes-map.dat
│ │ ├── archetypes-map.dat.keystream
│ │ ├── archetypes-map.dat.keystream.len
│ │ ├── archetypes-map.dat.len
│ │ ├── archetypes-map.dat.values.at
│ │ ├── archetypes-map.dat_i
│ │ ├── archetypes-map.dat_i.len
│ │ ├── artifactIds-map.dat
│ │ ├── artifactIds-map.dat.keystream
│ │ ├── artifactIds-map.dat.keystream.len
│ │ ├── artifactIds-map.dat.len
│ │ ├── artifactIds-map.dat.values.at
│ │ ├── artifactIds-map.dat_i
│ │ ├── artifactIds-map.dat_i.len
│ │ ├── context
│ │ │ ├── _b.cfs
│ │ │ ├── _b_1.del
│ │ │ ├── _c.cfs
│ │ │ ├── segments.gen
│ │ │ ├── segments_9
│ │ │ ├── timestamp
│ │ │ └── write.lock
│ │ ├── versions-map.dat
│ │ ├── versions-map.dat.keystream
│ │ ├── versions-map.dat.keystream.len
│ │ ├── versions-map.dat.len
│ │ ├── versions-map.dat.values.at
│ │ ├── versions-map.dat_i
│ │ └── versions-map.dat_i.len
│ └── index.properties
在indices文件夹中可以看到index0,index1.......这种文件夹,这些根据你设置的仓库来区分,从文件夹里面的一个
index.propertes中可以看出:
#Sat Apr 20 07:48:51 CST 2019
version=4
pathOrUrl=https\://repo.maven.apache.org/maven2
dataDirName=data0
failureMessage=java.lang.RuntimeException\: java.io.FileNotFoundException\: Resource nexus-maven-repository-index.properties does not exist
kind=REMOTE
id=central
值得注意的是:idea会自动去下载中央仓库的索引。当然,如果你创建这么一个中央仓库的index.properties,它就不会去更新了。
index(x)文件下除了index.properties之外,还有data0,data1.。。。。这类文件,这些文件下context文件中包含的内容就是索引的关键了。
四.巴拉巴拉(可不看,直接往下看结果)
那么这些索引文件是如何产生的呢,我从maven的官方文档中了解到了。
附上官方文档链接:https://maven.apache.org/repository/central-index.html
通过一番查看,发现仓库索引是一个maven-indexer的项目处理,这个项目给我们提供了一个indexer-cli的包,我们需要将其打包成jar包。通过它我们就可以对仓库生成索引文件,或者对仓库的索引文件包进行拆解(中央仓库:http://repo.maven.apache.org/maven2/.index/中就可以找到一系列的.gz的索引包了)。附上indexer-cli使用方法:
Maven :: Indexer CLI
Indexer CLI produces a CLI tool for indexing and publishing Maven repositories.
CLI Options Reference
Use either unpack (-u,--unpack) or index (-i,--index and -r,--repository) options.
Options Description
-c,--chunks Create incremental chunks.
-D,--define Define a system property
-d,--destination <arg> Target folder.
-e,--errors Produce execution error messages
-h,--help Display help information
-i,--index <arg> Path to the index folder.
-k,--keep <arg> Number of incremental chunks to keep.
-n,--name <arg> Repository name.
-q,--quiet Quiet output - only show errors
-r,--repository <arg> Path to the Maven repository.
-s,--checksums Create checksums for all files (sha1, md5).
-t,--type <arg> Indexer type (default, min, full or comma separated list of custom types).
-u,--unpack Unpack an index file
-v,--version Display version information
-X,--debug Produce execution debug output
五.解决方案
1.我们可以从中央仓库,或者其他镜像网站下载nexus-maven-repository-index.gz索引文件,通过indexer-cli(可以自己动手丰衣足食)获取索引文件,将其放入上面所述的索引文件夹中,并写好index.properties文件。
2.还有简单的方案:http://ju.outofmemory.cn/entry/359450