Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,14 @@ The `karaf:dockerfile` goal creates a ready to use Dockerfile for a given Karaf
|`assembly`
|`File`
|The directory containing the Karaf assembly (as generated by `karaf:assembly`). Default value: ${project.build.directory}/assembly

|`command`
|`String`
|The CMD command instruction to include in the generated Dockerfile. Default value: `["karaf", "run"]`

|`image`
|`String`
|The base Docker image (FROM instruction) to use. Default value: `eclipse-temurin:11-jre`
|===

===== Docker
Expand All @@ -1008,7 +1016,7 @@ This goal requires a local Docker daemon and runs only on Unix. The `docker` com
<execution>
<id>docker</id>
<goals>
<goal>docker</docker>
<goal>docker</goal>
</goals>
</execution>
----
Expand Down
8 changes: 7 additions & 1 deletion tooling/karaf-maven-plugin/src/it/test-dockerfile/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ import java.lang.*;
import java.util.*;

File generated = new File(basedir, "target/Dockerfile");
return generated.exists();
if (!generated.exists()) {
return false;
}
BufferedReader reader = new BufferedReader(new FileReader(generated));
String line = reader.readLine();
reader.close();
return line != null && line.startsWith("FROM eclipse-temurin:11-jre");
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public class DockerfileMojo extends MojoSupport {
@Parameter(defaultValue = "[\"karaf\", \"run\"]")
private String command;

@Parameter(defaultValue = "eclipse-temurin:11-jre", property = "image")
private String image;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Creating Dockerfile");
File dockerFile = new File(destDir, "Dockerfile");
try {
StringBuilder buffer = new StringBuilder();
buffer.append("FROM eclipse-temurin:11-jre").append("\n");
buffer.append("FROM ").append(image).append("\n");
buffer.append("ENV KARAF_INSTALL_PATH /opt").append("\n");
buffer.append("ENV KARAF_HOME $KARAF_INSTALL_PATH/apache-karaf").append("\n");
buffer.append("ENV KARAF_EXEC exec").append("\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.karaf.tooling;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class DockerfileMojoTest {

@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void testDefaultDockerfileGeneration() throws Exception {
DockerfileMojo mojo = new DockerfileMojo();
File destDir = temporaryFolder.newFolder("target");
File assembly = new File(destDir, "assembly");

setPrivateField(mojo, "destDir", destDir);
setPrivateField(mojo, "assembly", assembly);
setPrivateField(mojo, "command", "[\"karaf\", \"run\"]");
setPrivateField(mojo, "image", "eclipse-temurin:11-jre");

mojo.execute();

File dockerfile = new File(destDir, "Dockerfile");
assertTrue("Dockerfile should be created", dockerfile.exists());

List<String> lines = Files.readAllLines(dockerfile.toPath());
assertEquals("FROM eclipse-temurin:11-jre", lines.get(0));
assertEquals("ENV KARAF_INSTALL_PATH /opt", lines.get(1));
assertEquals("ENV KARAF_HOME $KARAF_INSTALL_PATH/apache-karaf", lines.get(2));
assertEquals("ENV KARAF_EXEC exec", lines.get(3));
assertEquals("ENV PATH $PATH:$KARAF_HOME/bin", lines.get(4));
assertEquals("COPY assembly $KARAF_HOME", lines.get(5));
assertEquals("EXPOSE 8101 1099 44444 8181", lines.get(6));
assertEquals("CMD [\"karaf\", \"run\"]", lines.get(7));
}

@Test
public void testCustomImageAndCommand() throws Exception {
DockerfileMojo mojo = new DockerfileMojo();
File destDir = temporaryFolder.newFolder("target-custom");
File assembly = new File(destDir, "custom-dist");

setPrivateField(mojo, "destDir", destDir);
setPrivateField(mojo, "assembly", assembly);
setPrivateField(mojo, "command", "[\"karaf\", \"server\"]");
setPrivateField(mojo, "image", "eclipse-temurin:17-jre");

mojo.execute();

File dockerfile = new File(destDir, "Dockerfile");
assertTrue("Dockerfile should be created", dockerfile.exists());

List<String> lines = Files.readAllLines(dockerfile.toPath());
assertEquals("FROM eclipse-temurin:17-jre", lines.get(0));
assertEquals("COPY custom-dist $KARAF_HOME", lines.get(5));
assertEquals("CMD [\"karaf\", \"server\"]", lines.get(7));
}

private void setPrivateField(Object obj, String fieldName, Object value) throws Exception {
Class<?> aClass = obj.getClass();
while (aClass != null) {
try {
Field field = aClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
return;
} catch (final NoSuchFieldException nsfe) {
aClass = aClass.getSuperclass();
}
}
fail("cant set " + fieldName);
}
}