美文网首页
使用Nexus搭建Maven私有仓库

使用Nexus搭建Maven私有仓库

作者: 深海同志草田苇 | 来源:发表于2017-05-16 10:42 被阅读0次

为什么要搭建私有仓库?


  • 避免所有人都从Maven中央仓库重复下载所需构件,节省外网带宽。
  • 当项目开发在内网环境下进行的时候,仍然可以使用Maven。

Nexus仓库的搭建


准备工作

  • 安装CentOS 7.0
  • 安装JDK
  • 安装Maven
  • 下载Nexus

安装和启动Nexus

  1. 解压Nexus,将解压后的目录名去掉版本号:
> tar -xzf nexus-2.14.4-bundle.tar.gz 
> mv nexus-2.14.4 nexus
  1. 配置环境变量并使配置生效:
> echo "export NEXUS_HOME=\$HOME/nexus" >> /etc/profile
> echo "export PATH=\$PATH:\$NEXUS_HOME/bin" >> /etc/profile
> source /etc/profile
  1. 启动Nexus:
> nexus start
> firefox 127.0.0.1:8081/nexus
  1. 启动Nexus之后,可以在浏览器界面中进行仓库和用户的管理。Nexus默认的最高权限用户是admin,默认密码admin123。

仓库和仓库组管理

仓库的类型(type)主要可分为hosted、proxy、virtual三种类型。hosted仓库是由仓库管理者在私服本地创建的仓库,proxy仓库是远程仓库的代理。virtual仓库可以理解为不同种类(format)仓库之间的适配器。

hosted仓库

Nexus自带仓库:

Repository Name Repository Policy Deployment Policy Remark
3rd Party Release Disable Redeploy 通常用于添加Maven仓库中没有的第三方依赖
Releases Release Allow Redeploy 用于项目组内部的稳定版本发布
Snapshots Snapshot Allow Redeploy 用于项目组内部的快照版本发布

另外,也可以自建hosted仓库。

proxy仓库

Nexus自带仓库:

Repository Name Repository Policy Remark
Apache Snapshots Release Apache软件基金会发布的快照版本组件
Codehaus Snapshots Release Codehaus发布的快照版本组件
Central Snapshot Maven中央仓库中发布的组件

另外,也可以自建proxy仓库,例如阿里云仓库的代理等等。

virtual仓库

Nexus自带一个名为Central M1,将原本的format为Maven2的Central仓库配置为format为Maven1的影子仓库。它在配置时有一个provider属性,可以选择:

  • Maven2转化Maven1
  • Maven1转化Maven2
  • 转化为NuGet(.NET平台的包管理工具)

仓库组

仓库组可以将多个仓库合并为一组,使用时作为单独的组件来引用。可以在UI界面中配置同组各个仓库的访问顺序。

用户管理

权限

Nexus默认创建了很多权限,例如UI:Basic UI Privileges包含了访问Nexus界面必须的最基本的权限,Repo:All Repositories (Read)给予用户读取所有仓库内容的权限。没有仓库的读权限用户将无法再仓库页面看到实际的仓库内容,也无法使用Maven从仓库下载构件等等,我们也可以自己创建对某个具体仓库增删改查的权限。

角色

Nexus默认创建了很多角色,每个角色拥有一个或多个权限,不同的角色权限不同。例如Nexus Administrator拥有Nexus的所有权限,Deployment只能够访问Nexus,浏览仓库内容、搜索、上传部署构件,但是不能对Nexus进行任何配置等等,我们也可以通过组合不同权限,创建自定义的角色。

用户

Nexus默认创建了一些用户,每个用户拥有一个或多个角色。我们也可以通过组合不同角色,创建新的用户。

在项目中使用私有仓库


添加私服依赖

在pom.xml文件中加入私服的地址:

<repositories>
        <repository>
            <id>MavenPrivateServer</id>
            <name>MavenPrivateServer</name>
            <url>${repositoriesIp}</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <layout>default</layout>
        </repository>
    </repositories>

配置私服发布

在pom.xml中配置快照版本和稳定版本的发布地址:

<distributionManagement>
        <snapshotRepository>
            <!--<id>testSSH_snopshot</id>-->
            <id>test_hosted_snapshot</id>
            <!--<name>User Project SNAPSHOTS</name>-->
            <name>test_hosted_snapshot</name>
            <url>http://${commonServerIp}:${commonServerPort}/nexus/content/repositories/test_hosted_snapshot</url>
        </snapshotRepository>

        <repository>
            <!--<id>testSSH_snopshot</id>-->
            <id>test_hosted_releases</id>
            <!--<name>User Project SNAPSHOTS</name>-->
            <name>test_hosted_releases</name>
            <url>http://${commonServerIp}:${commonServerPort}/nexus/content/repositories/test_hosted_releases/</url>
        </repository>
    </distributionManagement>

在settings.xml中配置要发布到的仓库的id和Nexus用户名和密码:

 <server>
      <id>test_hosted_snapshot</id>
      <username>user</username>
      <password>passwd</password>
    </server>

插件的管理

在pom.xml中配置插件:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>${cargo.version}</version>

                <configuration>
                    <!-- Container configuration -->
                    <container>
                        <containerId>tomcat8x</containerId>
                        <type>remote</type>
                    </container>

                    <configuration>
                        <type>runtime</type>
                        <properties>
                            <cargo.tomcat.manager.url>http://${commonServerIp}:${remoteTomcatPort}/manager/</cargo.tomcat.manager.url>
                            <cargo.remote.username>tomcat</cargo.remote.username>
                            <cargo.remote.password>tomcat</cargo.remote.password>
                            <cargo.servlet.port>${remoteTomcatPort}</cargo.servlet.port>
                            <cargo.hostname>${commonServerIp}</cargo.hostname>
                            <cargo.tomcat.ajp.port>8009</cargo.tomcat.ajp.port>
                        </properties>
                    </configuration>
                </configuration>

            </plugin>
        </plugins>

其它


Jetty

和Tomcat类似,Jetty也是一个开源的Servlet容器,和Tomcat相比,它更加轻量级,适合中小型的应用。
在Maven项目中使用Jetty,首先要增加jetty-maven-plugin到你的pom.xml:

<plugin>  
  <groupId>org.eclipse.jetty</groupId>  
  <artifactId>jetty-maven-plugin</artifactId>  
  <configuration>
    <!-- 配置端口 -->
    <httpConnector>
           <port>8787</port>
           <idleTimeout>300000</idleTimeout>
    </httpConnector>
       <webApp>
          <contextPath>/${project.artifactId}
 </contextPath>
       </webApp>
   <configuration>
</plugin> 

然后在项目根目录下执行命令:

mvn jetty:run

Cargo

使用Cargo可以将项目部署到已经在运行状态的容器上。
首先,需要在settings.xml中增加server:

 <server>
      <id>tomcat7</id>
      <username>user</username>
      <password>passwd</password>
</server>

然后,在pom.xml中加入:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>${cargo.version}</version>

                <configuration>
                    <!-- Container configuration -->
                    <container>
                        <containerId>tomcat8x</containerId>
                        <type>remote</type>
                    </container>

                    <configuration>
                        <type>runtime</type>
                        <properties>
                            <cargo.tomcat.manager.url>http://${commonServerIp}:${remoteTomcatPort}/manager/</cargo.tomcat.manager.url>
                            <cargo.remote.username>tomcat</cargo.remote.username>
                            <cargo.remote.password>tomcat</cargo.remote.password>
                            <cargo.servlet.port>${remoteTomcatPort}</cargo.servlet.port>
                            <cargo.hostname>${commonServerIp}</cargo.hostname>
                            <cargo.tomcat.ajp.port>8009</cargo.tomcat.ajp.port>
                        </properties>
                    </configuration>
                </configuration>
            </plugin>
        </plugins>
</build>

最后,通过命令启动Cargo并部署项目:

mvn cargo:run
mvn cargo:deploy

相关文章

网友评论

      本文标题:使用Nexus搭建Maven私有仓库

      本文链接:https://www.haomeiwen.com/subject/youhxxtx.html