diff --git a/src/main/asciidoc/inc/image/_naming.adoc b/src/main/asciidoc/inc/image/_naming.adoc index fc5afa331..aba725109 100644 --- a/src/main/asciidoc/inc/image/_naming.adoc +++ b/src/main/asciidoc/inc/image/_naming.adoc @@ -2,6 +2,8 @@ ## Image Names When specifying the image name in the configuration with the `` field you can use several placeholders which are replaced during runtime by this plugin. In addition you can use regular Maven properties which are resolved by Maven itself. +Replacements can also be used in `` fields within the the tags of any build configuration. + [cols="1,5"] |=== | Placeholder | Description @@ -20,6 +22,9 @@ When specifying the image name in the configuration with the `` field you | *%t* | If the project version ends with `-SNAPSHOT` this placeholder resolves to `snapshot-` where timestamp has the date format `yyMMdd-HHmmss-SSSS` (eg `snapshot-`). This feature is especially useful during development in oder to avoid conflicts when images are to be updated which are still in use. You need to take care yourself of cleaning up old images afterwards, though. + +| *%T* +| Timestamp with the format `yyMMdd-HHmmss-SSSS`. |=== ifeval::["{plugin}" == "docker"] diff --git a/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java b/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java index bd89a8324..659f83e8a 100644 --- a/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java +++ b/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java @@ -1,16 +1,26 @@ package io.fabric8.maven.docker.config; +import java.io.File; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; + import io.fabric8.maven.docker.util.DeepCopy; import io.fabric8.maven.docker.util.EnvUtil; import io.fabric8.maven.docker.util.Logger; import io.fabric8.maven.docker.util.MojoParameters; import org.apache.maven.plugins.annotations.Parameter; -import javax.annotation.Nonnull; -import java.io.File; -import java.io.Serializable; -import java.util.*; - /** * @author roland * @since 02.09.14 @@ -432,6 +442,12 @@ public File getAbsoluteDockerTarPath(MojoParameters mojoParams) { return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerArchive().getPath()); } + public void initTags(ConfigHelper.NameFormatter nameFormatter) { + if (tags != null) { + tags = tags.stream().map(nameFormatter::format).collect(Collectors.toList()); + } + } + public static class Builder { private final BuildImageConfiguration config; diff --git a/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java b/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java index fd9cde0b2..df46828fa 100644 --- a/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java +++ b/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java @@ -202,6 +202,7 @@ public String initAndValidate(ConfigHelper.NameFormatter nameFormatter, Logger l String minimalApiVersion = null; if (build != null) { minimalApiVersion = build.initAndValidate(log); + build.initTags(nameFormatter); } if (run != null) { minimalApiVersion = EnvUtil.extractLargerVersion(minimalApiVersion, run.initAndValidate()); diff --git a/src/main/java/io/fabric8/maven/docker/util/ImageNameFormatter.java b/src/main/java/io/fabric8/maven/docker/util/ImageNameFormatter.java index e3b8fa52c..20da0d258 100644 --- a/src/main/java/io/fabric8/maven/docker/util/ImageNameFormatter.java +++ b/src/main/java/io/fabric8/maven/docker/util/ImageNameFormatter.java @@ -34,6 +34,7 @@ */ public class ImageNameFormatter implements ConfigHelper.NameFormatter { + public static final String TIMESTAMP_FORMAT = "yyMMdd-HHmmss-SSSS"; private final FormatParameterReplacer formatParamReplacer; @@ -70,6 +71,9 @@ private Map initLookups(final MavenProje lookups.put("v", new DefaultTagLookup(project, DefaultTagLookup.Mode.PLAIN, now)); lookups.put("t", new DefaultTagLookup(project, DefaultTagLookup.Mode.SNAPSHOT_WITH_TIMESTAMP, now)); lookups.put("l", new DefaultTagLookup(project, DefaultTagLookup.Mode.SNAPSHOT_LATEST, now)); + + // Simple Timestamp + lookups.put("T", new DefaultTimestampLookup(project, now)); return lookups; } @@ -107,7 +111,7 @@ public String lookup() { } String groupId = project.getGroupId(); while (groupId.endsWith(".")) { - groupId = groupId.substring(0,groupId.length() - 1); + groupId = groupId.substring(0, groupId.length() - 1); } int idx = groupId.lastIndexOf("."); return sanitizeName(groupId.substring(idx != -1 ? idx + 1 : 0)); @@ -150,7 +154,7 @@ public String doTransform(String tag, Date now) { SNAPSHOT_WITH_TIMESTAMP('t') { public String doTransform(String tag, Date now) { if (tag.endsWith("-SNAPSHOT")) { - return "snapshot-" + new SimpleDateFormat("yyMMdd-HHmmss-SSSS").format(now); + return "snapshot-" + new SimpleDateFormat(TIMESTAMP_FORMAT).format(now); } return tag; } @@ -175,7 +179,7 @@ public String doTransform(String tag, Date now) { public String transform(MavenProject project, String tag, Date now) { // In case the Maven property is also a placeholder, replace it as well if (Strings.isNullOrEmpty(tag) || tag.equals("%" + letter)) { - tag = project.getVersion(); + tag = project.getVersion(); } return doTransform(tag, now); } @@ -193,6 +197,21 @@ public String lookup() { } } + private static class DefaultTimestampLookup extends AbstractLookup { + // timestamp indicating now + private final Date now; + + + private DefaultTimestampLookup(MavenProject project, Date now) { + super(project); + this.now = now; + } + + public String lookup() { + return new SimpleDateFormat(TIMESTAMP_FORMAT).format(now); + } + } + // ========================================================================================== // See also ImageConfiguration#doValidate() diff --git a/src/test/java/io/fabric8/maven/docker/util/ImageNameFormatterTest.java b/src/test/java/io/fabric8/maven/docker/util/ImageNameFormatterTest.java index 78743188d..8dafac1ba 100644 --- a/src/test/java/io/fabric8/maven/docker/util/ImageNameFormatterTest.java +++ b/src/test/java/io/fabric8/maven/docker/util/ImageNameFormatterTest.java @@ -123,6 +123,17 @@ public void tag() throws Exception { assertThat(formatter.format("%g/%a:%t").matches(".*snapshot-[\\d-]+$"), is(true)); } + @Test + public void timestamp() throws Exception { + new Expectations() {{ + project.getArtifactId(); result = "docker-maven-plugin"; + project.getGroupId(); result = "io.fabric8"; + project.getProperties(); result = new Properties(); + }}; + assertThat(formatter.format("%g/%a:%T").matches("^fabric8/docker-maven-plugin:[\\d-]+$"), is(true)); + assertThat(formatter.format("%g/%a:test-%T").matches("^fabric8/docker-maven-plugin:test-[\\d-]+$"), is(true)); + } + @Test public void tagWithDockerImageTagSet() { new Expectations() {{