我如何使用git commit id作为maven中的docker image标签?

我正在试图build立一个Maven Java项目的Docker镜像,并分别在安装和部署阶段推进; 我希望这些图像被标记为当前的git commit id。

我面临的问题是, maven-git-commit-plugin似乎没有正确导出${git.commit.id.abbrev}variables,以便在${git.commit.id.abbrev} -maven-plugin中使用 。

我的父母pom.xml是这样的:

 <?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <name>myproject</name> <groupId>mygroup</groupId> <artifactId>myartifact</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>mymodule</module> </modules> <properties> <!-- dependency versions --> <!-- some stuff here...--> <!-- plugin versions --> <maven.enforcer.plugin.version>1.4.1</maven.enforcer.plugin.version> <maven.shade.plugin.version>2.4.2</maven.shade.plugin.version> <scala.maven.plugin.version>3.2.2</scala.maven.plugin.version> <com.spotify.docker.plugin.version>0.3.8</com.spotify.docker.plugin.version> </properties> <dependencyManagement> <dependencies> <!-- some stuff here...--> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>${com.spotify.docker.plugin.version}</version> <executions> <execution> <id>docker-build</id> <goals> <goal>build</goal> </goals> <phase>install</phase> </execution> <execution> <id>docker-push</id> <goals> <goal>push</goal> </goals> <phase>deploy</phase> </execution> </executions> <configuration> <forceTags>true</forceTags> <baseImage>java</baseImage> </configuration> </plugin> <plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <version>2.2.0</version> <executions> <execution> <goals> <goal>revision</goal> </goals> </execution> </executions> <configuration> <dateFormatTimeZone>${user.timezone}</dateFormatTimeZone> <verbose>true</verbose> </configuration> </plugin> </plugins> </pluginManagement> <plugins> </plugins> </build> </project> 

而我的插件是我的模块pom.xml的一部分:

  <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <imageName>docker-registry.nexus.bazaarvoice.com/${project.parent.artifactId}-${project.artifactId}:${git.commit.id.abbrev}</imageName> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <!-- copy the service's jar file from target into the root directory of the image --> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> <resource> <targetPath>config</targetPath> <directory>${project.parent.basedir}/deploy/config/${project.artifactId}</directory> <include>*</include> </resource> </resources> </configuration> </plugin> 

我收到以下错误:

 [ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.3.8:build (docker-build) on project service: Exception caught: The template variable 'git.commit.id.abbrev' has no value -> [Help 1] 

我试过切换到buildnumber-plugin,但是我得到了同样的错误(使用${buildNumber}而不是${git.commit.id.abbrev} )。

我错过了什么? docker插件在git commit id插件设置variables之前执行吗?

我有一个类似的问题,因为我直接调用插件(我没有限制我的POM中的插件阶段,所以我必须显式调用它):

 mvn -f myModule/pom.xml docker:build -DforceTags -DpushImage 

用插件conf:

  <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <imageName>registry/myModule</imageName> <imageTags> <imageTag>${project.version}</imageTag> <imageTag>latest</imageTag> <imageTag>${git.commit.id.abbrev}</imageTag> </imageTags> <baseImage>java</baseImage> <entryPoint> ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/${project.build.finalName}.jar"] </entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> 

由于maven-git-commit-plugin被绑定到初始化阶段,我不得不调用这个阶段,所以git插件填充了属性

 mvn initialize -f myModule/pom.xml docker:build -DforceTags -DpushImage 

尝试使用mvn package docker:build而不是mvn docker:build