Skip to content

Commit

Permalink
Fixes #957 Simplify configuration
Browse files Browse the repository at this point in the history
+ When only a single image is build with Dockerfile, no need to provide
  too much parameters in configuration; build ImageConfiguration dynamically
  with the given defaults.
+ Updated Changelog.md and asciidoc with related changes
  • Loading branch information
rohanKanojia committed Mar 13, 2018
1 parent 73433ff commit 3a1e6ba
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.md
Expand Up @@ -14,6 +14,8 @@
- Introduced a `removeMode` for selecting the images to remove
- Introduced a `breakOnError` for the `postStart` and `preStop` hooks in the
wait configuration ([#914](https://github.com/fabric8io/docker-maven-plugin/issues/914))
- Simplified Configuration: When only a single image is build with Dockerfile, no need to provide too much parameters
in configuration; build ImageConfiguration dynamically with given defaults.

Please note that `autoPullMode` is deprecated now and the behaviour of the `autoPullMode == always` has been changed slightly so that now, it really always pulls the image from the registry. Also `removeAll` for `docker:remove` is deprecated in favor of `removeMode` (and the default mode has changed slightly). Please refer to the documentation for more information.

Expand Down
22 changes: 22 additions & 0 deletions samples/simple-configuration/pom.xml
@@ -0,0 +1,22 @@
<?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>

<groupId>simple-configuration-sample</groupId>
<artifactId>simple-configuration</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.24-SNAPSHOT</version>
</plugin>
</plugins>
</build>

</project>
1 change: 1 addition & 0 deletions samples/simple-configuration/src/main/docker/Dockerfile
@@ -0,0 +1 @@
FROM busybox:latest
6 changes: 6 additions & 0 deletions src/main/asciidoc/inc/_image-configuration.adoc
Expand Up @@ -12,3 +12,9 @@ include::image/_configuration.adoc[]
Either a `<build>` or `<run>` section must be present. These are explained in details in the corresponding goal sections.

include::image/_example.adoc[]

When only a single image is build with Dockerfile, no need to provide too much parameters in configuration. ImageConfiguration can be dynamically built with the given defaults.

include::image/_simple_configuration_example.adoc[]

It requires a single Dockerfile to be in the base directory or `/src/main/docker`.The image name would default to `${project.groupId}/${project.artifactId}:${project.version}`, but this can be configured with a `docker.autoConfiguration.imageName` flag
@@ -0,0 +1,9 @@

.Simple configuration Example
[source,xml]
----
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
----
3 changes: 3 additions & 0 deletions src/main/java/io/fabric8/maven/docker/AbstractDockerMojo.java
Expand Up @@ -102,6 +102,9 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
@Parameter(property = "docker.removeVolumes", defaultValue = "false")
protected boolean removeVolumes;

@Parameter(property = "docker.autoConfiguration.imageName", defaultValue = "")
protected String imageName;

@Parameter(property = "docker.apiVersion")
private String apiVersion;

Expand Down
49 changes: 41 additions & 8 deletions src/main/java/io/fabric8/maven/docker/BuildMojo.java
@@ -1,6 +1,7 @@
package io.fabric8.maven.docker;

import java.util.Date;
import java.util.List;

import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
Expand All @@ -9,6 +10,7 @@
import io.fabric8.maven.docker.service.ImagePullManager;
import io.fabric8.maven.docker.service.ServiceHub;
import io.fabric8.maven.docker.util.EnvUtil;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -34,15 +36,26 @@ protected void executeInternal(ServiceHub hub) throws DockerAccessException, Moj
if (skipBuild) {
return;
}
for (ImageConfiguration imageConfig : getResolvedImages()) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
List<ImageConfiguration> resolvedImages = getResolvedImages();

if (buildConfig != null) {
if (buildConfig.skip()) {
log.info("%s : Skipped building", imageConfig.getDescription());
} else {
buildAndTag(hub, imageConfig);
}
if(resolvedImages.isEmpty()) {
// No Configuration found, so build one up dynamically.
if(imageName == null) {
/*
* Image name defaults to:
* `${project.groupId}/${project.artifactId}:${project.version}`
*/
imageName = project.getGroupId() + "/" + project.getArtifactId() + ":" +
project.getVersion();
}
ImageConfiguration aDefaultImageConfig = ImageConfiguration.getDefaultImageConfiguration(imageName,
project.getBasedir().getAbsolutePath());
processImageConfig(hub, aDefaultImageConfig);
return;
} else {
// Iterate over all the ImageConfigurations and process one by one
for (ImageConfiguration imageConfig : resolvedImages) {
processImageConfig(hub, imageConfig);
}
}
}
Expand Down Expand Up @@ -71,4 +84,24 @@ protected Date getReferenceDate() throws MojoExecutionException {
private String determinePullPolicy(BuildImageConfiguration buildConfig) {
return buildConfig != null && buildConfig.getImagePullPolicy() != null ? buildConfig.getImagePullPolicy() : imagePullPolicy;
}

/**
* Helper method to process an ImageConfiguration.
*
* @param hub ServiceHub
* @param aImageConfig ImageConfiguration that would be forwarded to build and tag
* @throws DockerAccessException
* @throws MojoExecutionException
*/
private void processImageConfig(ServiceHub hub, ImageConfiguration aImageConfig) throws DockerAccessException, MojoExecutionException {
BuildImageConfiguration buildConfig = aImageConfig.getBuildConfiguration();

if (buildConfig != null) {
if(buildConfig.skip()) {
log.info("%s : Skipped building", aImageConfig.getDescription());
} else {
buildAndTag(hub, aImageConfig);
}
}
}
}
Expand Up @@ -5,6 +5,7 @@

import io.fabric8.maven.docker.util.*;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

/**
* @author roland
Expand Down Expand Up @@ -153,6 +154,16 @@ public String initAndValidate(ConfigHelper.NameFormatter nameFormatter, Logger l
return minimalApiVersion;
}

public static ImageConfiguration getDefaultImageConfiguration(String defaultImageName, String projectBaseDir) {
ImageConfiguration imageConfiguration = new ImageConfiguration.Builder()
.name(defaultImageName)
.buildConfig(new BuildImageConfiguration.Builder()
.dockerFileDir(DockerFileUtil.getDockerFileDirectory(projectBaseDir))
.build())
.build();
return imageConfiguration;
}

// =========================================================================
// Builder for image configurations

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java
Expand Up @@ -119,6 +119,27 @@ public static FixedStringSearchInterpolator createInterpolator(MojoParameters pa
.withExpressionMarkers(delimiters[0], delimiters[1]);
}

/**
* Fetch the location of docker file in case of simple configuration
*
* @param projectBaseDirPath Absolute path to project's base directory.
* @return directory containing the Dockerfile
*/
public static String getDockerFileDirectory(String projectBaseDirPath) {
if(checkFileExists(projectBaseDirPath + "/Dockerfile")) {
return projectBaseDirPath;
} else if(checkFileExists(projectBaseDirPath + "/src/main/docker/Dockerfile")){
return projectBaseDirPath + "/src/main/docker";
} else {
throw new IllegalStateException("Unable to locate Dockerfile");
}
}

private static boolean checkFileExists(String filePath) {
File aFile = new File(filePath);
return aFile.exists() && !aFile.isDirectory();
}

private static String[] extractDelimiters(String filter) {
if (filter == null ||
filter.equalsIgnoreCase("false") ||
Expand Down

0 comments on commit 3a1e6ba

Please sign in to comment.