一、概述
maven的settings.xml文件里面有proxy,、server、repository,、mirror的配置,,在配置倉(cāng)庫(kù)地址的時(shí)候容易混淆
proxy是服務(wù)器不能直接訪問(wèn)外網(wǎng)時(shí)需要設(shè)置的代理服務(wù),不常用
server是服務(wù)器要打包上傳到私服時(shí),,設(shè)置私服的鑒權(quán)信息
repository是服務(wù)器下載jar包的倉(cāng)庫(kù)地址
mirror是用于替代倉(cāng)庫(kù)地址的鏡像地址
下面查看源碼來(lái)確定repository和mirror的優(yōu)先順序
二,、初始化遠(yuǎn)程倉(cāng)庫(kù)
在maven的遠(yuǎn)程倉(cāng)庫(kù)初始化時(shí)會(huì)執(zhí)行newRepositorySession方法,會(huì)創(chuàng)建mirror,、proxy,、authentication和其它系統(tǒng)環(huán)境信息:
this .mavenRepositorySystem.injectMirror(request.getRemoteRepositories(), request.getMirrors());
this .mavenRepositorySystem.injectProxy(session, request.getRemoteRepositories());
this .mavenRepositorySystem.injectAuthentication(session, request.getRemoteRepositories());
|
三、遍歷注入鏡像
在injectMirror方法中:
public void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors)
{
if ((repositories != null ) && (mirrors != null )) {
for (ArtifactRepository repository : repositories)
{
Mirror mirror = getMirror(repository, mirrors);
injectMirror(repository, mirror);
}
}
}
|
遍歷repositories,,獲得mirror然后注入mirror
注:這里不用擔(dān)心repositories沒(méi)數(shù)據(jù),,因?yàn)樵谥暗某跏蓟痳epositories方法中會(huì)判斷沒(méi)有配置"central"中央倉(cāng)庫(kù)的時(shí)候加入默認(rèn)倉(cāng)庫(kù)
private void injectDefaultRepositories(MavenExecutionRequest request)
throws MavenExecutionRequestPopulationException
{
Set<String> definedRepositories = this .repositorySystem.getRepoIds(request.getRemoteRepositories());
if (!definedRepositories.contains( "central" )) {
try
{
request.addRemoteRepository( this .repositorySystem.createDefaultRemoteRepository(request));
}
catch (Exception e)
{
throw new MavenExecutionRequestPopulationException( "Cannot create default remote repository." , e);
}
}
}
|
public ArtifactRepository createDefaultRemoteRepository(MavenExecutionRequest request)
throws Exception
{
return createRepository( "https://repo.maven./maven2" , "central" , true , "daily" , false , "daily" , "warn" );
}
|
三、匹配鏡像
在getMirror方法中:
public static Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors)
{
String repoId = repository.getId();
if ((repoId != null ) && (mirrors != null ))
{
for (Mirror mirror : mirrors) {
if ((repoId.equals(mirror.getMirrorOf())) && (matchesLayout(repository, mirror))) {
return mirror;
}
}
for (Mirror mirror : mirrors) {
if ((matchPattern(repository, mirror.getMirrorOf())) && (matchesLayout(repository, mirror))) {
return mirror;
}
}
}
return null ;
}
|
忽略不常用的layout配置,,第一個(gè)循環(huán)優(yōu)先尋找mirrorOf與repositoryId完全相同的mirror
第二個(gè)循環(huán)通過(guò)規(guī)則來(lái)匹配mirror,,matchPattern的方法:
static boolean matchPattern(ArtifactRepository originalRepository, String pattern)
{
boolean result = false ;
String originalId = originalRepository.getId();
if (( "*" .equals(pattern)) || (pattern.equals(originalId)))
{
result = true ;
}
else
{
String[] repos = pattern.split( "," );
for (String repo : repos) {
if ((repo.length() > 1 ) && (repo.startsWith( "!" )))
{
if (repo.substring( 1 ).equals(originalId))
{
result = false ;
break ;
}
}
else
{
if (repo.equals(originalId))
{
result = true ;
break ;
}
if (( "external:*" .equals(repo)) && (isExternalRepo(originalRepository))) {
result = true ;
} else if ( "*" .equals(repo)) {
result = true ;
}
}
}
}
return result;
}
|
如果是mirrorOf是*號(hào)就匹配,如果不是就遍歷可能是逗號(hào)分隔的mirrorOf值,,同時(shí)看是否是!號(hào)排除的
在找到mirror后會(huì)執(zhí)行injectMirror方法,,把匹配的mirror注入到repository里面:
private void injectMirror(ArtifactRepository repository, Mirror mirror)
{
if (mirror != null )
{
ArtifactRepository original = createArtifactRepository(repository.getId(), repository.getUrl(), repository.getLayout(), repository
.getSnapshots(), repository.getReleases());
repository.setMirroredRepositories(Collections.singletonList(original));
repository.setId(mirror.getId());
repository.setUrl(mirror.getUrl());
if (StringUtils.isNotEmpty(mirror.getLayout())) {
repository.setLayout(getLayout(mirror.getLayout()));
}
}
}
|
四、總結(jié)
1,、在mirrorOf與repositoryId相同的時(shí)候優(yōu)先是使用mirror的地址
2,、mirrorOf等于*的時(shí)候覆蓋所有repository配置
3、存在多個(gè)mirror配置的時(shí)候mirrorOf等于*放到最后
4,、只配置mirrorOf為central的時(shí)候可以不用配置repository
我的settings.xml一般這樣設(shè)置:
<? xml version="1.0" encoding="UTF-8"?>
< settings xmlns="http://maven./SETTINGS/1.0.0"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven./SETTINGS/1.0.0 http://maven./xsd/settings-1.0.0.xsd">
< localRepository >X:\工具\(yùn)maven資源\repository</ localRepository >
< pluginGroups ></ pluginGroups >
< proxies ></ proxies >
< servers ></ servers >
< mirrors >
< mirror >
< id >nexus-aliyun</ id >
< name >Nexus aliyun</ name >
< mirrorOf >central</ mirrorOf >
< url >http://maven.aliyun.com/nexus/content/groups/public</ url >
</ mirror >
< mirror >
< id >nexus-mine</ id >
< name >Nexus mine</ name >
< mirrorOf >*</ mirrorOf >
< url >http://xx.xx.xx.xx/nexus/content/groups/public</ url >
</ mirror >
</ mirrors >
< profiles ></ profiles >
</ settings >
|
nexus-aliyun使用阿里云的鏡像作為central中央倉(cāng)庫(kù)
nexus-mine作為私服,,mirrorOf配置為*來(lái)提供中央倉(cāng)庫(kù)中不存在的jar包
|