maven docker push harbor
docker-maven-plugin

一.在m2 setting.xml的servers节点增加一个server节点

<server>
      <!--maven的pom中可以根据这个id找到这个server节点的配置-->  
      <id>docker-harbor</id>
      <username>your username</username>
      <password>your passwd</password>
      <configuration>
            <email>your email</email>
      </configuration>
    </server>

二.在环境变量增加
DOCKER_HOST:tcp://172.16.9.6:2375
如果本机没有docker daemon环境,找一台已经部署了docker并打开api remote接口设置。
参考 https://www.cnblogs.com/520playboy/p/7921633.html

三.在你的maven项目新建 src/main/docker/Dockerfile 如图
在这里插入图片描述
FROM tomcat:8
COPY crms.war /usr/local/tomcat/webapps/app.war
CMD [“catalina.sh”, “run”]

四:在pom.xml的properties节点增加
<docker.image.prefix>chezhibao</docker.image.prefix>
<docker.repostory>harbor.mychebao.com</docker.repostory>
<docker.registry.name>cicd</docker.registry.name>

五:在pom.xml的build节点增加

 <build>
        <finalName>crms</finalName>
        <plugins>
            <!-- war plugin config: 对静态资源进行过滤,替换变量。 如果你不需要,请移除这一段 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>${project.artifactId}</warName>
                    <packagingExcludes>
                    </packagingExcludes>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/*.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
              <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>0.4.13</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>tag-image</id>
                            <phase>package</phase>
                            <goals>
                                <goal>tag</goal>
                            </goals>
                            <configuration>
                                <image>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</image>
                                <newName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</newName>
                            </configuration>
                        </execution>
                        <execution>
                            <id>push-image</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>push</goal>
                            </goals>
                            <configuration>
                                <imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <serverId>docker-harbor</serverId>
                        <registryUrl>${docker.repostory}</registryUrl>
                        <forceTags>true</forceTags>
                        <pushImage>true</pushImage>
                        <dockerDirectory>src/main/docker</dockerDirectory>
                        <imageName>
                            ${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
                        </imageName>
                        <imageTags>
                            <imageTag>${project.version}</imageTag>
                        </imageTags>
                        <resources>
                            <rescource>
                                <targetPath></targetPath>
                                <directory>target/</directory>
                                <include>${project.build.finalName}.war</include>
                            </rescource>
                        </resources>
                    </configuration>
                </plugin>
            </plugins>
    </build>

六.看一下结果
在这里插入图片描述

在这里插入图片描述

七.docker pull harbor.****.com/cicd/crms:CRED-499-SNAPSHOT

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐