Maven私有仓库-使用docker部署Nexus

查看官方镜像说明

建议使用nexus2,可能网上的资料这个版本居多。
我选择的是nexus3,~~~

启动容器

官方说明中提到的是使用docker直接启动。我选择用docker-compose启动。
docker-compose.yml文件如下:

  • mkdir -p /opt/nexus
  • cd /opt/nexus
  • vim docker-compose.yml
    version: '2'
    services:
      nexus3:
        image: sonatype/nexus3
        container_name: nexus3
        volumes:
          - './nexus-data:/nexus-data:rw'
        ports:
          - '8081:8081'
  • docker-compose up -d
  • docker-compose ps #查看容器启动状态

使用supervisor管理容器进程

  • vim /etc/supervisor/conf.d/nexus.conf
    [program:nexus]
    directory = /opt/nexus ; 程序的启动目录
    command=docker-compose up ; 启动命令,可以看出与手动在命令行启动的命令是一样的
    autostart = true     ; 在 supervisord 启动的时候也自动启动
    startsecs = 30        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
    autorestart = true   ; 程序异常退出后自动重启
    startretries = 3     ; 启动失败自动重试次数,默认是 3
    user = root          ; 用哪个用户启动
    redirect_stderr = true  ; 把 stderr 重定向到 stdout,默认 false
    stdout_logfile_maxbytes = 20MB  ; stdout 日志文件大小,默认 50MB
    stdout_logfile_backups = 20     ; stdout 日志文件备份数
    ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
    stdout_logfile = /var/log/nexus.log
  • supervisorctl update #更新配置,就会启动新增的程序nexus
  • supervisorctl status nexus
  • cd /opt/nexus
  • docker-compose ps

访问nexus3的portal

http://0.0.0.0:18081
帐号:admin
密码:admin123
默认已经有四个maven相关的仓库,分别是maven-central,maven-releases,maven-snapshots,maven-public。其中的类型的定义如下:

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

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

  • group是仓库组,在maven里没有这个概念,是nexus特有的。目的是将上述多个仓库聚合,对用户暴露统一的地址,这样用户就不需要在pom中配置多个地址,只要统一配置group的地址就可以了。

如何发布自己的构件到私有仓库中?

  • 在~/.m2/settings中增加server节点,根据需要可以添加多个server
  <servers>
	<server>
      <id>nexus-ielong</id>
      <username>admin</username>
      <password>admin123</password>
   </server>
  </servers>
  • 在项目的pom.xml/build/中配置中配置maven-deploy-plugin
    <!--发布到私有仓库依赖的插件-->
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-deploy-plugin</artifactId>
       <version>2.8.1</version>
       <executions>
          <execution>
             <id>default-deploy</id>
             <phase>deploy</phase>
             <goals>
                <goal>deploy</goal>
             </goals>
          </execution>
       </executions>
       <configuration>
            <skip>false</skip>
        </configuration>
    </plugin>

温馨提示:如果在多模块项目中,可以将不需要发布的项目配置为skip=true。比如在parent的pom.xml中增加以下内容后,所有模块都会跳过发布。

    <build>
        <plugins>
            <!--发布到私有仓库依赖的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 在项目的pom.xml中配置distributionManagement
    <!--发布到私有仓库的配置-->
    <distributionManagement>
		<repository>
			<id>nexus-ielong</id>
			<name>nexus-ielong-releases</name>
			<url>http://0.0.0.0:18081/repository/maven-releases/</url>
		</repository>
		<snapshotRepository>
			<id>nexus-ielong</id>
			<name>nexus-ielong-snapshots</name>
			<url>http://0.0.0.0:18081/repository/maven-snapshots/</url>
			<uniqueVersion>false</uniqueVersion>
			<layout>legacy</layout>
		</snapshotRepository>
	</distributionManagement>

其中repository.id指~/.m2/settings.xml中配置的server.id, 这两个是同一个概念,值要一致。

  • 运行pom.xml所在目录 运行发布命令
    mvn clean deploy -Dmaven.test.skip=true

如何指定使用私有仓库下载maven构件?

有两种方式,建议使用第二种方式:

  • 第一种方式:在需要引用构件的项目中添加repository配置
    <!--在项目中指定使用私有仓库或者在~/.m2/settings.xml配置全局的mirror-->
	<repositories>
		<repository>
			<id>nexus-ielong</id>
			<name>ielong Nexus Repository</name>
			<url>http://0.0.0.0:18081/repository/maven-public/</url>
		</repository>
	</repositories>
  • 第二种方式: 在~/.m2/settings中指定mirror
    <mirrors>
        <!--不再使用aliyun的镜像-->
    	<!--<mirror>
    		<id>nexus-aliyun</id>
    		<name>Nexus aliyun</name>
    		<url>http://maven.aliyun.com/nexus/content/groups/public</url>
    		<mirrorOf>central</mirrorOf>
    	</mirror>-->
    	
    	<!--使用自己的私有镜像-->
        <mirror>
            <id>nexus-ielong</id>
            <name>nexus public group</name>
            <url>http://0.0.0.0:18081/repository/maven-public/</url>
            <mirrorof>*</mirrorof>
        </mirror>

    </mirrors>

参考文档

posted on 2017-08-23 17:55  James.H.Fu  阅读(848)  评论(0编辑  收藏  举报

导航