Maven 私服的搭建(Nexus)

来源:互联网 发布:dotamax数据不更新 编辑:程序博客网 时间:2024/05/15 20:28

Maven私服

1.1.1. 关于中央仓库注意事项

地址: 目前来说:http://repo1.maven.org/maven2/是真正的Maven中央仓库的地址,该地址内置在Maven的源码中其他的都是镜像

索引:中央仓库带有索引文件以方便用户对其进行搜索,完整的索引文件大小约为60M,索引每周更新一次

黑名单: 如果某个IP地址恶意的下载中央仓库内容,例如全公司100台机器使用同一个IP反复下载,这个IP(甚至是IP段)会进入黑名单,因此稍有规模的使用Maven时,应该用Nexus架设私服

1.1.2. 为什么需要私服

当前主流的maven私服:

1.ApacheArchiva

2.JFrogArtifactory

3.SonatypeNexus

为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到这台私服上,这样的话就可以通过这台搭建了nexus私服的电脑访问maven的远程仓库

 

1.1.3. Nexus环境搭建

第一步:下载nexus-2.14.1-01-bundle.zip包; 符:nuxus

百度网盘地址:

http://pan.baidu.com/s/1mgznhd2

第二步:解压缩nexus:目录结构如下:

 

 

个人建议使用 windows 服务的方式,安装nexus

 

 

 

 

 

启动 默认登录账户密码为:admin  admin123  

第三步:访问http://localhost:8081/nexus/

1.1.4. 认识repository

Nexus仓库的默认的地址, nexus-2.1.2\WEB-INF\plexus.properties 中定义的

Views/Repositories-->Repositories里进行配置, nexus里可以配置3种类型的仓库,分别是proxyhostedgroup

proxy是远程仓库的代理。比如说在nexus中配置了一个central repositoryproxy,当用户向这个proxy请求一个artifact,这个proxy就会先在本地查找,如果找不到的话,就会从远程仓库下载,然后返回给用户,相当于起到一个中转的作用

Hosted是宿主仓库,用户可以把自己的一些构件,deployhosted中,也可以手工上传构件到hosted里。比如说oracle的驱动程序,ojdbc6.jar,在central repository是获取不到的,就需要手工上传到hosted

Group是仓库组,在maven里没有这个概念,是nexus特有的。目的是将上述多个仓库聚合,对用户暴露统一的地址,这样用户就不需要在pom中配置多个地址,只要统一配置group的地址就可以了右边那个Repository Path可以点击进去,看到仓库中artifact列表。不过要注意浏览器缓存。我今天就发现,明明构件已经更新了,在浏览器里却看不到,还以为是BUG,其实是被浏览器缓存了

virtual也是中央仓库镜像,支持M1老版本

1.1.5. 每个仓库的作用

3rd party: 无法从公共仓库获得的第三方发布版本的构件仓库
Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库
Central: 用来代理maven中央仓库中发布版本构件的仓库
Central M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库
Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库
Releases: 用来部署管理内部的发布版本构件的宿主类型仓库
Snapshots:用来部署管理内部的快照版本构件的宿主类型仓库

 

 

 

 

1.1.6. Central Repository

最关键的一个配置, 就是Central Repositoryproxy配置,因为大部分的构件,都是要通过这个proxy得到的,

1.1.7. 配置Hosted Repository

一般会配置3hosted repository,分别是3rd partySnapshotsReleases,分别用来保存第三方jar(典型的比如ojdbc7.jar http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html),项目组内部的快照、项目组内部的发布版,这里并没有什么特别的配置,只是Deployment Policy这个选项,一般Snapshots会配置成允许,而Releases3rd party会设置为禁止

 

1.1.8. 配置group repository

前面说过group其实是一个虚拟的仓库,通过对实体仓库(proxyhosted)进行聚合,对外暴露一个统一的地址这里要注意的是,放到左边的仓库,才是会被聚合的仓库

 

1.1.9. 从私服中下载资源

可以在支持Maven的项目中进行如下配置

<repositories>

  <repository>

  <id>shop</id>

  <name>仓库介绍</name>

  <url>http://localhost:8081/nexus/content/groups/public/</url>

  </repository>

 </repositories>

 <pluginRepositories>

  <pluginRepository>

  <id>plugin</id>

  <name>插件下载地址</name>

  <url>http://localhost:8081/nexus/content/groups/public/</url>

  </pluginRepository>

 </pluginRepositories>

也可以把地址配置在setting.xml<profiles>标签中这样对所有项目都生效

<profiles>

      <repositories>

<repository>

<id>shop</id>

<name>仓库介绍</name>

<url>http://localhost:8081/nexus/content/groups/public/</url>

</repository>

</repositories>

<pluginRepositories>

   <pluginRepository>

   <id>plugin</id>

   <name>插件下载地址</name><url>http://localhost:8081/nexus/content/groups/public/</url>

   </pluginRepository>

</pluginRepositories>

 </profiles>

l 把本地项目提交到私服中

<distributionManagement>

<repository>

<!-- 要在个人的setting.xml中配置上传的密码 -->

<id>snapshots</id>

<name>snapshots 测试版</name>

<!-- nexus中查找相应的地址-->

        <url>http://localhost:8081/nexus/content/repositories

            /snapshots/</url>

</repository>

</distributionManagement>

 

<servers>

    <!-- 在.m2\setting.xml中 配置了要上传仓库的密码 -->

<server>

<id>snapshots</id>

<username>admin</username>

<password>admin123</password>

</server>

<server>

<id>releases</id>

<username>admin</username>

<password>admin123</password>

</server>

  </servers>

1.1. 其它小技巧

1.1.1. 跳过单元测试

你可能想要配置 Maven使其完全跳过单元测试。 可能你有一个很大的系统,单元测试需要花好多分钟来完成,而你不想在生成最终输出前等单元测试完成。 你可能正工作在一个遗留系统上面,这个系统有一系列的失败的单元测试,你可能仅仅想要生成一个JAR 而不是去修复所有的单元测试。Maven 提供了跳过单元测试的能力

采用命令的方式跳过单元测试

$ mvn install -D maven.test.skip=true

采用配置的方式跳过单元测试

surefire 插件用来在maven构建生命周期的test 的插件, 如果每次都不需要运行test则通过简单的配置即可

<build>

<plugins>

<plugin>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.7.1</version>

<configuration>

<skip>true</skip>

</configuration>

</plugin>

</plugins>

</build>

1.1.2. 代码的方式生成maven骨架

Archetype可以帮助我们迅速地构建起项目的骨架,在前面的例子中,我们完全可以在Archetype生成的骨架的基础上开发Hello World项目以节省大量时间,如果没有制定packageName则默认与goupId相同

mvn archetype:create -D groupId=com.test.maven -D artifactId=test1 -D packageName=a.b.c

1.8.3 常见错误:

/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced

 

解决方法:重新启动eclipse,因为是eclipse有缓冲。更新了settting.xml的内容,但是一直没有生效。

 

 

 

 

清楚cache的方法

To clean the local cache try using the dependency plug-in.

1. mvn dependency:purge-local-repository: This is an attempt to delete the local repository files but it always goes and fills up the local repository after things have been removed.

2. mvn dependency:purge-local-repository -DreResolve=false: This avoids the re-resolving of the dependencies but seems to still go to the network at times.

3. mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false: This was added by Paweł Prażak and seems to work well. I'd use the third if you want the local repo emptied, and the first if you just want to throw out the local repo and get the dependencies again.

 

 

一直提示无法链接到nexus代理服务器(私服):

 

 

 

 

 

 

 

maven不从cache中获取内容

mvn clean package -U

You can use -e to confirm that, and -X to get a lot more debugging information during resolution. As a commenter also mentioned, you should use -U to stop Maven from caching the failed resolution until the issue is resolved.

 

 

编码GBK的不可映射字符

 

 

 

 

直接将项目改为UTF-8编码,无效!

 

要通过修改pom.xml文件,告诉maven这个项目使用UTF-8来编译。

 

方案一:

 

pom.xml/project/build/plugins/下的编译插件声明中加入下面的配置:<encoding>UTF-8</encoding>

 

即:

[html] view plaincopy

 

    <plugin>  

        <groupId>org.apache.maven.plugins</groupId>   

        <artifactId>maven-compiler-plugin</artifactId>  

        <version>3.1</version>  

        <configuration>  

            <source>1.7</source>  

            <target>1.7</target>  

            <encoding>UTF-8</encoding>  

        </configuration>  

    </plugin>  

 

 

方案二:

 

pom.xml/project/properties/下的属性配置中加入下面的配置:

 

<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

 

即:(有第一句即可)

 

[html] view plaincopy

 

    <properties>  

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  

        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>  

</properties>

 

 

 

 

 

 

 

 

 

程序包org.junit不存在

 

 

 

解决方法:

去掉:

<scope>test</scope>

怀疑是 eclipse 插件的bug。不能够识别开发和测试的阶段区别;

 

 

 

1.8.4 上传第三方的snapshot jar包以及sources jar包到私服中:

 

mvn deploy:deploy-file -DgroupId=open.campus.client4j.internal -DartifactId=open-campus-client4j-internal -Dversion=1.0.7.r1-SNAPSHOT -Dpackaging=jar -DrepositoryId=snapshots -Dfile=C:\完美校园开放平台\open-campus-client4j-internal-1.0.7.r1-SNAPSHOT-distribution\open-campus-client4j-internal-1.0.7.r1-SNAPSHOT\open-campus-client4j-internal-1.0.7.r1-SNAPSHOT.jar -Durl=http://192.168.33.35:8081/nexus/content/repositories/snapshots/ -DgeneratePom=false

 

 

mvn deploy:deploy-file -DgroupId=open.campus.client4j.internal -DartifactId=open-campus-client4j-internal -Dversion=1.0.7.r1-SNAPSHOT -Dclassifier=sources -Dpackaging=jar -DrepositoryId=snapshots -Dfile=C:\\open-campus-client4j-internal-1.0.7.r1-SNAPSHOT-distribution\open-campus-client4j-internal-1.0.7.r1-SNAPSHOT\open-campus-client4j-internal-1.0.7.r1-SNAPSHOT-sources.jar -Durl=http://192.168.33.35:8081/nexus/content/repositories/snapshots/ -DgeneratePom=false

1 0
原创粉丝点击