`

搭建maven私服

阅读更多

通过nexus搭建私服,不仅可以代理远程的仓库,而且能够帮助我们管理和部署第三方的构件。

 

1. 下载

http://www.sonatype.org/nexus/go/

下载最新的nexus(我的是2.11.2),有两个版本一个是绑定了jetty的,有JRE就可以直接运行

一个是War包,将其发布到Web容器就可以运行。

 

2. 安装

解压

$tar xvzf nexus-xxx-bundle.tar.gz

 

3. 启动

到解压好的nexus目录下

$ ./nexus start

http://localhost:8081/nexus就可以访问,默认管理员账户:admin,密码:admin123

 

4. 修改密码

可以在左侧的菜单栏 Security->Users中,选中admin右键Reset Password重置密码

 

5. 添加用户

同样可以在Security->Users中,Add一个新的user。根据项目需要,添加了新的用户:kobe

并给其设置了权限:

Nexus Deployment Role   //可以在服务器上部署jar包

Repo:All Maven Repositories(Full Control)    //可以管理服务器上的所有Maven仓库

 

6. 仓库配置

nexus默认会建立几个仓库,每个仓库都有类型:

1. Public Repositories 类型是group,它相当于一个仓库集合,可以把其它仓库加入进去组成一个集成仓库

它已经默认包含了Release、Snapshots、3rd party和Central仓库

2. Central 类型是proxy, 它是用来代理maven的中央仓库,也就是当用户下载中央库的包时,会让代理去中央库下载,然后用户再从代理中下载

3. Releases 类型是hosted,是本地仓库,让用户部署Release类型的包,默认是Disable Redeploy表示不允许具有相同的GAV的release包重复部署,可以修改为Allow Redeploy

4. Snapshots 类型是hosted,是本地仓库,让用户部署Snapshots类型的包(也就是版本带.snapshot后缀的),一般表示开发中的包,默认是可以重复部署

5. 3rd Party 类型也是hosted,本地仓库,一般是部署一些从中央仓库下载不到的第三方的jar包

 

7. maven配置

搭建好nexus后,我们需要将maven下载jar包和发布jar包的地址指向我们的私服

 

修改maven的配置文件setting.xml:

 

在根目录下加入或修改以下内容:

 <servers>
	<server>
		<id>nexus-deploy</id>
		<username>kobe</username>
		<password>xxxxxx</password>
	</server>
</servers>

 <mirrors>
	<mirror>
		<id>nexus</id>
		<mirrorOf>*</mirrorOf>
		<name>nexus</name>
		<url>http://nexus服务器IP:8081/nexus/content/groups/public/</url>
	</mirror>
</mirrors>

<profiles>
	<profile>
      <id>nexus</id>
      <repositories> 
        <repository> 
            <id>nexus</id> 
            <name>local private nexus</name> 
            <url>http://nexus服务器IP:8081/nexus/content/groups/public/</url> 
            <releases><enabled>true</enabled><updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy></releases> 
            <snapshots><enabled>false</enabled></snapshots> 
        </repository>        
      </repositories> 
      <pluginRepositories> 
        <pluginRepository> 
            <id>nexus</id> 
            <name>local private nexus</name> 
            <url>http://nexus服务器IP:8081/nexus/content/groups/public/</url> 
            <releases><enabled>true</enabled><updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy></releases> 
            <snapshots><enabled>false</enabled></snapshots> 
        </pluginRepository>        
       </pluginRepositories> 
    </profile>
</profile>

  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

 

<mirrors>和<profiles>的配置将Maven私服覆盖central仓库配置,这样maven对仍和仓库的下载请求都转到我们的私服中,如果要启用profile,需要加入<activeProfiles>。

 

 

7. maven部署jar包到私服

上面的配置能够保证我们从私服中去请求下载jar包,但我们需要在私服中部署我们的jar包时,还需要配置。

我们注意到上面XML配置中的<server>标签:

1. 标签中<id>标示了服务器具有唯一性

2. <username>就是nexus中的用户,使用我们之前创建的用户(具有部署、管理所有maven仓库的权限)

3. <password>就是用户对应的密码

部署项目:

如果想要部署jar包到私服上去,我们还需要在项目的pom.xml文件根标签下加入以下配置:

 

 <distributionManagement>
        <repository>
            <id>nexus-deploy</id>
            <name>Nexus Release Repository</name>
            <url>http://nexus服务器ip:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-deploy</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://nexus服务器ip:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

 

注意这里的<id>要和setting.xml文件中<server>的<id>对应。

 

最后在项目下执行 mvn deploy命令就可以将项目上传到私服上去,Release包传到Releases仓库,Snapshot上传到Snapshots仓库。(mvn install 是安装到本地仓库,mvn deploy是部署到服务器仓库)
 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics