工具
插件名称 | 官方地址 |
---|---|
docker-maven-plugin | https://github.com/spotify/docker-maven-plugin |
在pom.xml中添加下面这段(Dockerfile构建)
<!-- Docker镜像构建 mvn docker:build https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<!-- Dockerfile文件位置 -->
<dockerDirectory>${project.basedir}</dockerDirectory>
<!-- 镜像仓库:版本 -->
<imageName>registry.cn-shanghai.aliyuncs.com/ycmm/${project.artifactId}:${project.version}</imageName>
<!-- 强制覆盖 -->
<forceTags>true</forceTags>
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
<!-- 资源 -->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 上传仓库认证 需要在Maven settiing.xml中配置 -->
<serverId>docker-ali</serverId>
<registryUrl>registry.cn-shanghai.aliyuncs.com</registryUrl>
</configuration>
</plugin>
构建命令
mvn clean package docker:build
将插件绑定在某个phase执行
- mvn install 时能够docker:build
- mvn deploy 时能够docker:push
我们只需要将插件的goal 绑定在某个phase即可。
所谓的phase和goal,可以这样理解:maven命令格式是:mvn phase:goal ,例如mvn package docker:build 那么,package 和docker 都是phase,build 则是goal 。
修改POM文件
<!-- Docker镜像构建 mvn docker:build https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</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>
<!-- Dockerfile文件位置 -->
<dockerDirectory>${project.basedir}</dockerDirectory>
<!-- 镜像仓库:版本 -->
<imageName>registry.cn-shanghai.aliyuncs.com/ycmm/${project.artifactId}:${project.version}</imageName>
<!-- 强制覆盖 -->
<!-- <forceTags>true</forceTags>
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags> -->
<!-- 资源 -->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 上传仓库认证 需要在Maven settiing.xml中配置 -->
<serverId>docker-ali</serverId>
<registryUrl>registry.cn-shanghai.aliyuncs.com</registryUrl>
</configuration>
</plugin>