Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Fix for #292: Skip plugin execution when a Dockerfile is not present #293

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mvn clean package -Ddockerfile.skip
| `dockerfile.contextDirectory` | Directory containing the Dockerfile to build. | yes | none |
| `dockerfile.repository` | The repository to name the built image | no | none |
| `dockerfile.tag` | The tag to apply when building the Dockerfile, which is appended to the repository. | no | latest |
| `dockerfile.failedOnMissingDockerfile` | Failed maven build when there is no default Dockerfile in the project/module. | no | true
| `dockerfile.build.pullNewerImage` | Updates base images automatically. | no | true |
| `dockerfile.build.noCache` | Do not use cache when building the image. | no | false |
| `dockerfile.build.cacheFrom` | Docker image used as cache-from. Pulled in advance if not exist locally or `pullNewerImage` is `false` | no | none |
Expand Down
62 changes: 62 additions & 0 deletions plugin/src/it/multi-module/c/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
-/-/-
Dockerfile Maven Plugin
%%
Copyright (C) 2015 - 2016 Spotify AB
%%
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-\-\-
-->

<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>

<parent>
<groupId>com.spotify.it</groupId>
<artifactId>multi-module</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>c</artifactId>

<description>The third module</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
<configuration>
<repository>test/multi-module-c</repository>
<tag>unstable</tag>
<failedOnMissingDockerfile>false</failedOnMissingDockerfile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
1 change: 1 addition & 0 deletions plugin/src/it/multi-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<modules>
<module>a</module>
<module>b</module>
<module>c</module>
</modules>

<build>
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/it/multi-module/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ assert tagFile.text == "unstable\n"

File imageNameFile = new File(basedir, "b/target/docker-info-deps/META-INF/docker/com.spotify.it/a/image-name")
assert imageNameFile.text == "test/multi-module-a:unstable\n"

String buildLog = new File("${basedir}/build.log").getText("UTF-8")
assert buildLog.contains("docker-maven plugin execution is skipped for:")
36 changes: 24 additions & 12 deletions plugin/src/main/java/com/spotify/plugin/dockerfile/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public class BuildMojo extends AbstractDockerMojo {
@Parameter(property = "dockerfile.build.skip", defaultValue = "false")
private boolean skipBuild;

/**
* Failed the build when Default docker file is not present in the context directory.
*/
@Parameter(property = "dockerfile.build.failedOnMissingDockerfile", defaultValue = "true")
private boolean failedOnMissingDockerfile;

/**
* Updates base images automatically.
*/
Expand Down Expand Up @@ -132,6 +138,24 @@ public void execute(DockerClient dockerClient)
if (dockerfile != null) {
dockerfilePath = dockerfile.toPath();
}

log.info("Path(dockerfile): " + dockerfilePath);
log.info("Path(contextDirectory): " + contextDirectory.toPath());

if (dockerfilePath == null
&& !Files.exists(contextDirectory.toPath().resolve("Dockerfile"))
&& !Files.exists(contextDirectory.toPath().resolve("dockerfile"))) {
// user did not override the default value
log.warn("Missing Dockerfile in context directory: " + contextDirectory.toPath());
if (failedOnMissingDockerfile) {
throw new MojoFailureException("Missing Dockerfile in context directory: "
+ contextDirectory);
} else {
log.warn("docker-maven plugin execution is skipped for: " + contextDirectory.toPath());
return;
}
}

final String imageId = buildImage(
dockerClient, log, verbose, contextDirectory.toPath(), dockerfilePath, repository, tag,
pullNewerImage, noCache, buildArgs, cacheFrom, squash);
Expand Down Expand Up @@ -262,18 +286,6 @@ private static void requireValidDockerFilePath(@Nonnull Log log,
@Nullable Path dockerfile)
throws MojoFailureException {

log.info("Path(dockerfile): " + dockerfile);
log.info("Path(contextDirectory): " + contextDirectory);

if (dockerfile == null
&& !Files.exists(contextDirectory.resolve("Dockerfile"))
&& !Files.exists(contextDirectory.resolve("dockerfile"))) {
// user did not override the default value
log.error("Missing Dockerfile in context directory: " + contextDirectory);
throw new MojoFailureException("Missing Dockerfile in context directory: "
+ contextDirectory);
}

if (dockerfile != null) {
if (!Files.exists(dockerfile)) {
log.error("Missing Dockerfile at " + dockerfile);
Expand Down