dockerfile-maven-plugin使用当前时间作为镜像tag

2022-03-28 21:10:47 浏览数 (1)

dockerfile-maven-plugin是一款用于maven打包docker镜像的插件,其功能类似于docker client,负责将dockerfile中命令发送到docker守护进程,所以即使在Windows上使用也没有问题,我们完全可以借助Linux部署的docker环境来完成docker镜像的构建过程,这部分搭建过程可以参考:https://cloud.tencent.com/developer/article/1965662

好了下面直接给出pom.xml文件相关配置:

代码语言:javascript复制
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.10</version>
        <executions>
          <execution>
            <id>timestamp-property</id>
            <goals>
              <goal>timestamp-property</goal>
            </goals>
            <configuration>
              <name>build.time</name>
              <pattern>yyyyMMddHHmmss</pattern>
              <locale>en_US</locale>
              <timeZone>GMT 8</timeZone>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>build-docker-admin-image</id>
            <phase>package</phase>
            <goals>
              <goal>build</goal>
              <goal>tag</goal>
            </goals>
            <configuration>
              <skip>${dockerfile.skip}</skip>
              <repository>${docker.repo}/${docker.image}</repository>
              <verbose>true</verbose>
              <tag>${build.time}</tag>
              <googleContainerRegistryEnabled>false</googleContainerRegistryEnabled>
              <contextDirectory>${basedir}</contextDirectory>
            </configuration>
          </execution>
          <execution>
            <id>push-docker-admin-image</id>
            <phase>package</phase>
            <goals>
              <goal>push</goal>
            </goals>
            <configuration>
              <skip>${dockerfile.skip}</skip>
              <repository>${docker.repo}/${docker.image}</repository>
              <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

这里面需要关注的是build-helper-maven-plugin以及dockerfile-maven-plugin两个插件,build-helper-maven-plugin用于解决maven.build.timestamp时区问题,maven.build.timestamp使用的是UTC时间,及伦敦时间,为了解决这个问题,我们使用build-helper-maven-plugin来创建北京时间,即GMT 8

PS:

需要注意build-helper-maven-plugin插件中的configuration配置的name标签值要与dockerfile-maven-plugin插件tag的名字相同!!!

参考文章:

1、https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables

2、https://github.com/spotify/dockerfile-maven/blob/master/docs/usage.md

3、http://www.mojohaus.org/build-helper-maven-plugin/timestamp-property-mojo.html#timeZone

0 人点赞