手机版
你好,游客 登录 注册
背景:
阅读新闻

使用Nexus搭建私服

[日期:2014-03-23] 来源:Linux社区  作者:Linux [字体:大 中 小]

这样的配置只对当前的Maven项目有效。但是,实际的项目开发中,我们都想着一次配置就能让本机的所有Maven项目都使用Maven私服,那么我们就要用到下面的方法。

2、在settings.xml中配置远程仓库。

Maven提供的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。有了profile,你就可以为不同的环境定制构建。那么在settings.xml中加入以下代码:

<settings>
  ...
 <profiles>
  <profile>
   <id>local_nexus</id>
   <repositories>
    <repository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </repository>
   </repositories>
   <pluginRepositories>
    <pluginRepository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </pluginRepository>
   </pluginRepositories>
  </profile>
 </profiles>
 <activeProfiles>
  <activeProfile>local_nexus</activeProfile>
 </activeProfiles>
  ...
</settings>

上面的配置中,使用了一个id为local_nexus的profile,这个profile包含了相关的仓库配置,同时配置中又使用了activeProfiles元素将nexus这个profile激活,这样当执行Maven构建的时候,激活的profile会将仓库配置应用到项目中去。

通过上面的配置,我们会发现Maven除了从Nexus下载构件外还会从中央仓库下载构件构件。既然是私服,那么我们就只希望Maven下载请求都仅仅通过Nexus。我们可以通过镜像实现这一需求。可以创建一个匹配任何仓库的镜像,镜像的地址是私服,这样Maven对任何仓库的构件下载请求都会转到私服中。把上面的配置修改为如下配置:

<settings>
  ...
 <mirrors>
  <mirror>
   <id>local_mirror</id>
   <mirrorOf>*</mirrorOf>
   <name>local_mirror</name>
   <url>http://localhost:8081/nexus/content/groups/public/</url>
  </mirror>
 </mirrors>
 
 <profiles>
  <profile>
   <id>local_nexus</id>
   <repositories>
    <repository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </repository>
    <repository>
     <id>central</id>
     <!--由于配置过镜像,这个url不起作用-->
     <url>http://repo.maven.apache.org/maven2</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </repository>
   </repositories>
   <pluginRepositories>
    <pluginRepository>
     <id>local_nexus</id>
     <name>local_nexus</name>
     <url>http://localhost:8081/nexus/content/groups/public/</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </pluginRepository>
    <pluginRepository>
     <id>central</id>
     <url>http://repo.maven.apache.org/maven2</url>
     <releases>
      <enabled>true</enabled>
     </releases>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </pluginRepository>
   </pluginRepositories>
  </profile>
 </profiles>
 <activeProfiles>
  <activeProfile>local_nexus</activeProfile>
 </activeProfiles>
  ...
</settings>

部署构件到私服

我们在实际开发过程是多个人的,那么总有一些公共模块或者说第三方构件是无法从Maven中央库下载的。我们需要将这些构件部署到私服上,供其他开发人员下载。用户可以配置Maven自动部署构件至Nexus的宿主仓库,也可以通过界面手动上传构件。

使用Maven部署构件到Nexus私服上

日常开发的快照版本部署到Nexus中策略为Snapshot的宿主仓库中,正式项目部署到策略为Release的宿主仓库中,POM的配置方式如下:

...
<distributionManagement>
 <repository>
  <id>local_nexus_releases</id>
  <name>core Release Repository</name>
  <url>http://localhost:8081/nexus/content/repositories/releases/</url>
 </repository>
 <snapshotRepository>
  <id>local_nexus_snapshots</id>
  <name>core Snapshots Repository</name>
  <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
 </snapshotRepository>
</distributionManagement>
...

其中,验证信息中service的id应该与POM中repository的id一致。

 

在Nexus界面上手动部署第三方构件至私服

 

我们除了自己的构件要部署到Nexus私服上外,我们有可能还要将第三方构件(如:SQLService的JDBC)部署到Nexus上。这个时候,在Nexus界面上选择一个宿主仓库(如3rd party),再在页面下方选择Artifact Upload选项卡。填写对应的Maven坐标。然后点击“Select Artifact(s) for Upload”按钮从本机选择要上传的构件,然后点击“Add Artifact”按钮将其加入到上传列表中。最后,单击页面底部的“Upload Artifact(s)”按钮将构件上传到仓库中。

linux
相关资讯       Nexus 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款