原创文章,转载请注明出处,谢谢!http://casheen.iteye.com/admin/blogs/730295
从08年初开始使用Maven2至今,已经两年有半。期间我们一直在使用的Maven2 Repository管理工具是Apache的Archiva。在使用Archiva的过程中,遇到一些问题没有及时解决,主要有:
- 有些artifact不会通过配置的Archiva服务器下载;
- 有些artifact下载的压缩包有问题,而系统会认为下载成功,不会每次从Archiva再次下载正确的artifact。
由于没有时间对Archiva配置进行细致的研究,对于这些问题我们并没有解决,而是采用替代方案:
- 让那些artifact绕过Archiva服务器下载(反正绕过它也没遇到更到问题);
- 写脚本扫描并删除有问题的压缩包文件(要同时删除本地文件,以及Archiva服务器上的缓存文件),再次更新项目的Maven依赖后,会自动再次下载。
最近由于网络问题,一些artifact始终下载不了,所以下定决心去研究一下Archiva的配置。过程中也下载了一份Nexus并做了简单的对比,下面是对Archiva和Nexus配置的一些记载。
我理解的Archiva服务器工作方式:
- 当本地的Maven项目请求一个artifact时,会先去本地的Repository中查找;
- 若没有找到符合的artifact,则会向Archiva服务器请求该artifact,这是由于本地设置(~/.m2/settings.xml或%MAVEN_HOME%/conf/settings.xml中设置)了以Archiva服务器作为所有Repository的mirror;
- Archiva服务器的Managed Repository会接收这个请求,并从Managed Repository中寻找符合条件的artifact;
- 若没有找到符合条件的artifact,会从设置了proxy connector的Remote Repository中寻找符合条件的artifact,如果找到则从Remote Repository中下载一份到Managed Repository中,并回应客户请求(发自我们本地的请求);
- 如果没有找到符合条件的artifact,Archiva服务器则返回404信息,交给本地处理(本地会再去配置的Repository列表中去查找)。
<mirrors> <mirror> <id>archiva.default</id> <name>archiva.default</name> <url>http://localhost:8088/archiva/repository/internal</url> <mirrorOf>*</mirrorOf> </mirror> <mirror> <id>archiva.apache.snapshots</id> <name>archiva.apache.snapshots</name> <url>http://localhost:8088/archiva/repository/snapshots</url> <mirrorOf>apache.snapshots</mirrorOf> </mirror> </mirrors>
- 要保证Archiva找到符合的artifact,必须保证在Archiva中配置的Remote Repository中能够找到该artifact;
- 要想Archiva能够从Remote Repository中去找artifact,必须要对该Remote Repository设置一个proxy connector,否则Archiva从本地找不到artifact时不会去没有配置proxy connector的Remote Repository中找,这就是我们之前遇到的问题1的根源;
- 当本地的Maven项目请求一个artifact时,会先去本地的Repository中查找;
- 若没有找到符合的artifact,则会从本地配置的Repository列表中按顺序去查找;
- 若当前查找的Repository是设置(~/.m2/settings.xml或%MAVEN_HOME%/conf/settings.xml中设置)为被Nexus mirror的,则向Nexus请求该artifact;
- Nexus接收请求后,会到相应的Repository中查找本地是否有该artifact的缓存,若有则返回给客户;
- 若没有,则从Repository中查找是否有该artifact,若有则从远处Repository下载一份在本地缓存;
- 若没有则返回客户,客户端会继续查找下一个Repository。
<mirrors> <mirror> <!--This sends everything else to /public --> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://localhost:8081/nexus/content/groups/public</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <!-- Enable snapshots for the built in central repo to direct all requests to nexus via the mirror --> <repositories> <!-- Maven Central --> <repository> <id>central</id> <id>central</id> <name>Maven Central</name> <url>http://repo1.maven.org/maven2/</url> <snapshots><enabled>false</enabled></snapshots> </repository> <!-- Apache Snapshots --> <repository> <id>apache-snapshots</id> <name>Apache Snapshots</name> <url>http://repository.apache.org/snapshots/</url> <snapshots><enabled>true</enabled></snapshots> </repository> <!-- Codehaus Snapshots --> <repository> <id>codehaus-snapshots</id> <name>Codehaus Snapshots</name> <url>http://nexus.codehaus.org/snapshots/</url> <snapshots><enabled>true</enabled></snapshots> </repository> <!-- java.net - Maven 2 --> <repository> <id>java.net-m2</id> <name>java.net - Maven 2</name> <url>http://download.java.net/maven/2/</url> <snapshots><enabled>false</enabled></snapshots> </repository> <!-- Google Code --> <repository> <id>google</id> <name>Google Code</name> <url>http://google-maven-repository.googlecode.com/svn/repository/</url> </repository> <!-- jahia --> <repository> <id>jahia</id> <name>jahia</name> <url>http://maven.jahia.org/maven2/</url> <snapshots><enabled>false</enabled></snapshots> </repository> <!-- SpringSide Additional Repository --> <repository> <id>springside</id> <name>SpringSide Additional Repository</name> <url>http://springside.googlecode.com/svn/repository/</url> <snapshots><enabled>false</enabled></snapshots> </repository> <!-- Jboss Repository --> <repository> <id>jboss</id> <name>Jboss Repository</name> <url>http://repository.jboss.com/maven2/</url> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <!--make the profile active all the time --> <activeProfile>nexus</activeProfile> </activeProfiles>
- 要保证Nexus去管理所有的artifact,必须设置Nexus mirror你项目中配置的所有Repository;
- 要在Nexus服务器中配置你mirror的所有Repository;
最后,关于问题2,应该是proxy connector中参数设置问题,只要把每个proxy connector的Checksum设置为fail,问题应该会自然解决。(之前配置的两个proxy connector的Checksum设置有问题,故有问题2发生;目前已更改配置,但是否解决问题仍需进一步跟踪)