前提条件:

  1. 有一台电脑 安装了maven
  2. 安装了jdk1.8 以上版本
  3. 安装了 git
  4. 安装了intellij idea
  5. 电脑能上网
  6. 有一台nexus 服务器,如果没有可以参考我的另一篇文章:Sonatype Nexus 自动化部署实践 https://blog.csdn.net/happyfreeangel/article/details/89481720
    我的nexus 服务器器是支持https的,可以 端口是:上传镜像端口是1443, 下载镜像端口是2443,
    域名是nexus.linkaixin.com

spotify docker-maven-plugin 官方源代码地址:
https://github.com/spotify/docker-maven-plugin

下面是一个大型网上商店的源代码,用于测试镜像打包,上传nexus.
https://github.com/HappyFreeAngel/mall.git

git clone https://github.com/HappyFreeAngel/mall.git

cd mall
mvn clean compile package docker:build -DpushImage

实现了编译,打包,封装docker 镜像,推送docker 镜像到nexus 仓库。

结果如下:
本地镜像结果如下:
docker images | grep ‘nexus.linkaixin.com:1443/mall’


远程仓库查看: 结果如下
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019081616103873.png

看起来很简单的插件,使用起来其实坑很多。
我后来查看了源代码,通过几种测试方法,才发现问题所在。

需要配置的文件: 1. .m2/settings.xml

    <!-- Another sample, using keys to authenticate.<server><id>siteServer</id><privateKey>/path/to/private/key</privateKey><passphrase>optional; leave empty if not used.</passphrase></server>--><server><id>nexus-docker-registry</id><username>admin</username><password>admin123</password><configuration><email>11394019@qq.com</email></configuration></server>
  1. 配置pom.xml
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.macro.mall</groupId><artifactId>mall-admin</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>mall-admin</name><description>mall-admin project for mall</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><skipTests>true</skipTests></properties><parent><groupId>com.macro.mall</groupId><artifactId>mall</artifactId><version>1.0-SNAPSHOT</version></parent><dependencies><dependency><groupId>com.macro.mall</groupId><artifactId>mall-mbg</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!--JWT(Json Web Token)登录支持--><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.9.0</version></dependency><!-- 阿里云OSS --><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>2.5.0</version></dependency><!--集成logstash--><dependency><groupId>net.logstash.logback</groupId><artifactId>logstash-logback-encoder</artifactId><version>4.8</version></dependency><!--lombok依赖--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><version>1.2.0</version><executions><execution><id>build-image</id><phase>package</phase><goals><goal>build</goal></goals><configuration><imageName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</imageName></configuration></execution><execution><id>tag-image</id><phase>package</phase><goals><goal>tag</goal></goals><configuration><image>mall/${project.artifactId}:${project.version}</image><newName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</newName></configuration></execution><execution><id>push-image</id><phase>deploy</phase><goals><goal>push</goal></goals><configuration><imageName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</imageName></configuration></execution></executions><configuration><imageName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</imageName><dockerHost>unix:///var/run/docker.sock</dockerHost>
<!--                    <dockerCertPath>/Users/linyingjie/nexus.linkaixin.com.crt</dockerCertPath>--><baseImage>nexus.linkaixin.com:2443/common-docker-starter:openjdk-8u191-alpine3.8-support-ssh-login-1.0.0</baseImage><entryPoint>["java", "-jar", "-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint><resources><resource><targetPath>/</targetPath><directory>${project.build.directory}</directory><include>${project.build.finalName}.jar</include></resource></resources><serverId>nexus-docker-registry</serverId><!-- <registryUrl>119.145.41.171:8082/v1/</registryUrl> --></configuration></plugin></plugins></build>
</project>

踩到的坑如下:
问题1:
[ERROR] No plugin found for prefix ‘docker’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/linyingjie/.m2/repository), alimaven (http://maven.aliyun.com/nexus/content/groups/public/)] -> [Help 1]

解决办法: .m2/settings.xml 添加 com.spotify

    <!-- pluginGroup| Specifies a further group identifier to use for plugin lookup.<pluginGroup>com.your.plugins</pluginGroup>--><pluginGroup>io.fabric8</pluginGroup><pluginGroup>com.spotify</pluginGroup></pluginGroups>

问题2:
[WARNING] Failed to push nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT, retrying in 10 seconds (1/5).
[INFO] Pushing nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT
The push refers to repository [nexus.linkaixin.com:1443/mall/mall-admin]
214b9fe2899d: Preparing
27e730ca5883: Layer already exists
70eee88b4bab: Layer already exists
4e2f9475b031: Layer already exists
10beb411784d: Layer already exists
aa0369d6605f: Layer already exists
612d6b123925: Waiting
36d9b4d72ec4: Waiting
1de5d7cd5bd1: Waiting
b845f393fc2b: Waiting
dbc783c89851: Waiting
7bff100f35cb: Waiting
[WARNING] Failed to push nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT, retrying in 10 seconds (2/5).

这个原因是因为这个插件优先使用了.docker/config.json,但是这个会超时失败,不知道是什么原因
删除里面的nexus.linkaixin.com 的2条记录,就正常了。

{"auths": {"nexus.linkaixin.com:1443": {},"nexus.linkaixin.com:2443": {}},"HttpHeaders": {"User-Agent": "Docker-Client/18.09.2 (darwin)"},"credsStore": "osxkeychain","stackOrchestrator": "swarm"
}改成下面:
{"auths": {},"HttpHeaders": {"User-Agent": "Docker-Client/18.09.2 (darwin)"},"credsStore": "osxkeychain","stackOrchestrator": "swarm"
}这个其实就是让docker-maven-plugin 在config.json 里找不到关于nexus.linkaixin.com账号配置,
让它搜索settings.xml 里的配置账号信息。

[INFO] — docker-maven-plugin:1.2.0:tag (tag-image) @ mall-admin —
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier, FixedRegistryAuthSupplier]
[INFO] Creating tag nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT from mall/mall-admin:1.0-SNAPSHOT
[INFO] Pushing nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT
The push refers to repository [nexus.linkaixin.com:1443/mall/mall-admin]
e595cf98d1ac: Layer already exists
27e730ca5883: Layer already exists
70eee88b4bab: Layer already exists
4e2f9475b031: Layer already exists
10beb411784d: Layer already exists
aa0369d6605f: Layer already exists
612d6b123925: Layer already exists
36d9b4d72ec4: Layer already exists
1de5d7cd5bd1: Layer already exists
b845f393fc2b: Layer already exists
dbc783c89851: Layer already exists
7bff100f35cb: Layer already exists
1.0-SNAPSHOT: digest: sha256:2d07d5530edcd6436c28068b7c36b78132e49859c2ba4bd78206ed50cdc595d1 size: 2826

Maven编译Docker 镜像并推送到Nexus相关推荐

  1. Java Spring Boot 2.0 实战之制作Docker镜像并推送到Docker Hub和阿里云仓库

    内容摘要:大规模集群快速部署Java应用,需要制作Docker镜像,本次课程详细介绍如何制作Java程序的Docker镜像文件,深入解析DockerFile核心参数,以及实践演练把我们制作的Docke ...

  2. 1.2 SpringBoot构建Docker镜像并推送到Harbor

    1.修改pom 文件中 引入 dockerfile-maven-plugin 插件 1.1 properties 标签中增加如下: <!--项目名,需要和Harbor中的项目名称保持一致 --& ...

  3. 使用dockerfile自定义Docker镜像并推送到dockerhub

    Docker自定义镜像并推送到dockerhub 一.B要条件 二.选择一个操作系统作为基础环境 三.创建dockerfile 四.使用dockerfile构建自己的docker镜像 五.定义要推送的 ...

  4. 使用maven插件构建docker镜像并推送到阿里镜像仓库

    1.阿里云镜像仓库 阿里云提供了docker镜像仓库,可以上传私有的镜像 2.com.spotify 提供的docker构建插件 docker-maven-plugin <plugin>& ...

  5. springboot构建docker镜像并推送到阿里云

    1.构建springboot项目 工程目录如下 欢迎关注个人公众号[好好学技术]交流学习 UserController package com.fandf.test.controller; impor ...

  6. maven集成docker插件进行打包镜像并推送私服

    使用maven进行集成docker打包成镜像以及推送到docker私服 先水几句 我不会开场啊-默默无闻的送码人.不是吧不是吧.现在还有人用maven打包,然后手动上传jar.然后进行部署吗?你out ...

  7. Maven打包SpringBoot项目镜像并推送至私有Harbor仓库

    1 准备工作: 1.1 搭建harbor 因为harbor是基于docker的,并且依赖于docker-compose,所以先要准备好dockers环境再安装好compose工具 1.1.1 dock ...

  8. IDEA中使用Docker插件构建镜像并推送至私服Harbor

    IDEA中使用Docker插件构建镜像并推送至私服Harbor 一.开启Docker服务器的远程访问 1.1 开启2375远程访问 1.2 添加harbor镜像配置 1.3 重启docker服务 二. ...

  9. Docker 使用 docker push 命令 推送本地镜像到远程DockerHub仓库服务器

    Docker 使用 docker push 命令 ----- 推送本地镜像到远程DockerHub仓库服务器 Docker hub注册用户 到官网注册账号:https://hub.docker.com ...

最新文章

  1. python跨行字符串 变量_在Python中有没有在多行字符串中使用变量的方法?
  2. Exchange Server2013 系列十:证书的配置
  3. android中textcolor属性,android – EditText和TextView textColorPrimary不遵循API lt;21的主题颜色...
  4. 用计算机问你叫什么名字,计算器女友与男子谈情说爱 一分钟发出各种甜言蜜语...
  5. Cloudera Manager5安装总结遇到问题及解决办法
  6. 【转】android自定义控件
  7. Bootstraping, bagging, boosting, AdaBoosting, Rand forest 和 gradient boosting
  8. 产品汪们匿名自爆工资,看完心里好难受......
  9. caffeine 弱引用key的实现
  10. Struts1 生成Action请求的几种方式分析
  11. Android之知识总结
  12. Chrome浏览器 js 关闭窗口失效解决方法
  13. 端口与进程-----Window cmd命令
  14. c语言写的fft程序,C语言编写FFT程序.pdf
  15. 2014.12 总结
  16. enum ordinal java_Java Enum ordinal()方法
  17. PS与qq热键设置雷同
  18. MSE 费芮新金融行业标杆案例
  19. 阿里云服务器使用docker安装mysql
  20. 2022最新阿里P6+Java技术体系图

热门文章

  1. springboot下载excel(解决文件损坏问题)
  2. js获取本月初与月底的时间、获取前一天的时间。
  3. JavaWeb专栏之(三):Eclipse创建JavaWeb项目
  4. java hypot_Java StrictMath hypot()方法与示例
  5. mavon-editor编辑器与图片上传
  6. ai智能写作如何快速写文?
  7. java火车票售票系统,火车购票系统,系统实现了用户会员管理、火车车次管理、火车座位管理、系统公告管理、火车票退票、火车票换乘、换乘查询、直达查询、乘车人管理、订单管理、个人中心管理等
  8. 从3dMax导出供threeJS使用的带动作模型与加载(认真修改详尽版)
  9. LeetCode162数组寻峰问题
  10. LocalDate转换成英文格式