Skip to content

Commit

Permalink
Allow replacements in build <tags>
Browse files Browse the repository at this point in the history
Fixes #1367

Signed-off-by: Tobias Günther <Xyaren@users.noreply.github.com>
  • Loading branch information
Xyaren authored and rohanKanojia committed Sep 24, 2021
1 parent ea3b51d commit 44716a5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/main/asciidoc/inc/image/_naming.adoc
Expand Up @@ -2,6 +2,8 @@
## Image Names
When specifying the image name in the configuration with the `<name>` 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 `<tag>` fields within the the tags of any build configuration.

[cols="1,5"]
|===
| Placeholder | Description
Expand All @@ -20,6 +22,9 @@ When specifying the image name in the configuration with the `<name>` field you

| *%t*
| If the project version ends with `-SNAPSHOT` this placeholder resolves to `snapshot-<timestamp>` 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"]
Expand Down
@@ -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
Expand Down Expand Up @@ -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;

Expand Down
Expand Up @@ -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());
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/io/fabric8/maven/docker/util/ImageNameFormatter.java
Expand Up @@ -34,6 +34,7 @@
*/
public class ImageNameFormatter implements ConfigHelper.NameFormatter {

public static final String TIMESTAMP_FORMAT = "yyMMdd-HHmmss-SSSS";

private final FormatParameterReplacer formatParamReplacer;

Expand Down Expand Up @@ -70,6 +71,9 @@ private Map<String, FormatParameterReplacer.Lookup> 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;
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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()
Expand Down
Expand Up @@ -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() {{
Expand Down

0 comments on commit 44716a5

Please sign in to comment.