`
郑云飞
  • 浏览: 795786 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Nexus in Docker

 
阅读更多

摘要:本文主要是记录如何使用 Nexus 3.3 官方的 Docker 镜像搭建 Maven 私服。基于 Dockder搭建大大简化了整个流程。

众所周知,Docker 可以大大简化服务器的部署,所以 sonatype 公司也发布了 nexus v3版本的官方docker。有了这个 Dockerfile 我们就可以轻松的在本地或者自己的服务器搭建一个 Nexus 服务器了。

要运行,将暴露的端口8081绑定到主机

下载 Nexus 镜像

这里推荐的方案是利用 Docker 来搭建 Nexus 环境。这里默认大家已经都有 Docker 环境了。

首先利用下面的命令下载最新的 nexus 镜像

 

docker pull sonatype/nexus
 下载过程如下所示:

 

 

docker pull sonatype/nexus
Using default tag: latest
latest: Pulling from sonatype/nexus
8d30e94188e7: Pull complete
9b5961d40d03: Pull complete
074855ae6153: Pull complete
cc0a3a494fba: Pull complete
8cfd0607bbfb: Pull complete
Digest: sha256:6bb1966022009da38c4f679b51d2106f4d595aa7b887ac0bf93d14ebe4c0faa2
Status: Downloaded newer image for sonatype/nexus:latest
 

 

创建并启动 Nexus

首先创建一个数据卷容器,如下命令

 

docker run -d --name nexus-data sonatype/nexus echo "data-only container for Nexus"
 然后启动 Nexus

 

 

docker run -d -p 8081:8081 --name nexus --volumes-from nexus-data sonatype/nexus
 上面的命令中,将本机的 10081 端口映射到容器中的 8081 端口,并加载 数据卷容器 nexus-data。

 

创建容器过程需要一段时间时间进行初始化,可以用如下命令查看相关日志:

 

docker logs -f nexus
 在浏览器输入 http://localhost:8081/nexus/ 可以看到如下界面,说明 nexus 已经启动好了。

使用 Nexus

在这个私有库中,提供了一些仓库地址给我们使用,如下图所示:

关于每个仓库的意义何在,请移步到 Nexus 的官网中学习(文末给出了参考链接),此文不再进行介绍。

私有仓库的使用有两种方法,一种是对工程的 pom.xml 文件进行修改(这种方法只对 pom.xml 所属工程生效),如下所示:
将如下的repositories节点添加到 pom.xml 文件即可,Maven 会到此文件中配置的私有库,下载相应的依赖库。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>testnexus</groupId>
    <artifactId>zzx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <repositories>
        <repository>
            <id>nexus</id>
            <name>Team Nexus Repository</name>
            <url>http://localhost:10081/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <dependencies>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.1.41</version>
    </dependency>
    </dependencies>
</project>

 另外一种方法就是直接修改 maven 的配置文件,这样所有工程默认都会使用私有库。如下所示:

在 ~/.m2/settings.xml 文件中,将 mirror 修改为:

<settings>
  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:10081/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>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

 

当利用 maven 进行工程打包时,可以看到,已经从私有库中下载依赖了(当在另外一台机器上使用相应的依赖库时,就直接使用私有库中所缓存的依赖了,不用再到互联网中下载,是不是很节约时间!):

testnesus mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building zzx 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.pom
Downloaded: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.pom (9 KB at 2.9 KB/sec)
Downloading: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.jar
Downloaded: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.jar (350 KB at 16.5 KB/sec)

 关于更多内容,请上 Nexus 的官网学习。

参考资料

 

 

 

 

 

  • 大小: 264.3 KB
  • 大小: 181.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics