This is an automated email from the ASF dual-hosted git repository.
pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 505dc1ca58e feat(jbang): harmonize Dockerfile presence
505dc1ca58e is described below
commit 505dc1ca58e0ba5b55bcbd7c245df80fc938e8ee
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Thu Sep 26 15:12:05 2024 +0200
feat(jbang): harmonize Dockerfile presence
* All the runtimes have a Dockerfile when we export the application
* Added a simple readme.md to show quickly how to build and run an
application
Closes CAMEL-21259
---
.../camel/dsl/jbang/core/commands/Export.java | 31 ++++++++++++++++++++++
.../dsl/jbang/core/commands/ExportCamelMain.java | 4 ++-
.../dsl/jbang/core/commands/ExportQuarkus.java | 14 +++++++---
.../dsl/jbang/core/commands/ExportSpringBoot.java | 4 ++-
.../resources/quarkus-docker/Dockerfile.legacy-jar | 4 +++
.../resources/quarkus-docker/Dockerfile.native | 6 ++++-
.../quarkus-docker/Dockerfile.native-micro | 4 +++
.../Dockerfile.tmpl} | 25 +++++++++--------
.../src/main/resources/templates/readme.md.tmpl | 31 ++++++++++++++++++++++
.../camel/dsl/jbang/core/commands/ExportTest.java | 10 ++++++-
10 files changed, 113 insertions(+), 20 deletions(-)
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java
index 7093f96e92f..e85aaddfd44 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java
@@ -17,6 +17,8 @@
package org.apache.camel.dsl.jbang.core.commands;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
@@ -29,6 +31,7 @@ import org.apache.camel.dsl.jbang.core.common.SourceScheme;
import org.apache.camel.tooling.maven.MavenGav;
import org.apache.camel.util.CamelCaseOrderedProperties;
import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
import picocli.CommandLine.Command;
@Command(name = "export",
@@ -72,6 +75,7 @@ public class Export extends ExportBaseCommand {
return 1;
}
}
+
}
private void doLoadAndInitProfileProperties(File file) throws Exception {
@@ -234,10 +238,37 @@ public class Export extends ExportBaseCommand {
};
}
+ // Maven reproducible builds:
https://maven.apache.org/guides/mini/guide-reproducible-builds.html
protected String getBuildMavenProjectDate() {
// 2024-09-23T10:00:00Z
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
return sdf.format(new Date());
}
+ // Copy the dockerfile into the same Maven project root directory.
+ protected void copyDockerFiles(String buildDir) throws Exception {
+ File docker = new File(buildDir, "src/main/docker");
+ docker.mkdirs();
+ String[] ids = gav.split(":");
+ InputStream is =
ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/Dockerfile.tmpl");
+ String context = IOHelper.loadText(is);
+ IOHelper.close(is);
+
+ String appJar = ids[1] + "-" + ids[2] + ".jar";
+ context = context.replaceAll("\\{\\{ \\.AppJar }}", appJar);
+ IOHelper.writeText(context, new FileOutputStream(new File(docker,
"Dockerfile"), false));
+ }
+
+ // Copy the readme.md into the same Maven project root directory.
+ protected void copyReadme(String buildDir, String appJar) throws Exception
{
+ String[] ids = gav.split(":");
+ InputStream is =
ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/readme.md.tmpl");
+ String context = IOHelper.loadText(is);
+ IOHelper.close(is);
+
+ context = context.replaceAll("\\{\\{ \\.ArtifactId }}", ids[1]);
+ context = context.replaceAll("\\{\\{ \\.Version }}", ids[2]);
+ context = context.replaceAll("\\{\\{ \\.AppRuntimeJar }}", appJar);
+ IOHelper.writeText(context, new FileOutputStream(new File(buildDir,
"readme.md"), false));
+ }
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
index a795f968e5f..542b60c6875 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
@@ -149,7 +149,9 @@ class ExportCamelMain extends Export {
copyMavenWrapper();
}
}
-
+ copyDockerFiles(BUILD_DIR);
+ String appJar = "target" + File.separator + ids[1] + "-" + ids[2] +
".jar";
+ copyReadme(BUILD_DIR, appJar);
if (cleanExportDir || !exportDir.equals(".")) {
// cleaning current dir can be a bit dangerous so only clean if
explicit enabled
// otherwise always clean export-dir to avoid stale data
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
index ebef8da4b2f..35d1306e516 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
@@ -114,7 +114,9 @@ class ExportQuarkus extends Export {
return prop;
});
// copy docker files
- copyDockerFiles();
+ copyDockerFiles(BUILD_DIR);
+ String appJar = "target" + File.separator + "quarkus-app" +
File.separator + "quarkus-run.jar";
+ copyReadme(BUILD_DIR, appJar);
// gather dependencies
Set<String> deps = resolveDependencies(settings, profile);
// copy local lib JARs
@@ -339,16 +341,22 @@ class ExportQuarkus extends Export {
return super.applicationPropertyLine(key, value);
}
- private void copyDockerFiles() throws Exception {
- File docker = new File(BUILD_DIR, "src/main/docker");
+ @Override
+ protected void copyDockerFiles(String buildDir) throws Exception {
+ File docker = new File(buildDir, "src/main/docker");
docker.mkdirs();
// copy files
InputStream is =
ExportQuarkus.class.getClassLoader().getResourceAsStream("quarkus-docker/Dockerfile.jvm");
+ // Deprecated, use Dockerfile instead
IOHelper.copyAndCloseInput(is, new FileOutputStream(new File(docker,
"Dockerfile.jvm")));
+ is =
ExportQuarkus.class.getClassLoader().getResourceAsStream("quarkus-docker/Dockerfile.jvm");
+ IOHelper.copyAndCloseInput(is, new FileOutputStream(new File(docker,
"Dockerfile")));
+ // Deprecated, to be removed in the future
is =
ExportQuarkus.class.getClassLoader().getResourceAsStream("quarkus-docker/Dockerfile.legacy-jar");
IOHelper.copyAndCloseInput(is, new FileOutputStream(new File(docker,
"Dockerfile.legacy-jar")));
is =
ExportQuarkus.class.getClassLoader().getResourceAsStream("quarkus-docker/Dockerfile.native");
IOHelper.copyAndCloseInput(is, new FileOutputStream(new File(docker,
"Dockerfile.native")));
+ // Deprecated, to be removed in the future
is =
ExportQuarkus.class.getClassLoader().getResourceAsStream("quarkus-docker/Dockerfile.native-micro");
IOHelper.copyAndCloseInput(is, new FileOutputStream(new File(docker,
"Dockerfile.native-micro")));
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
index 60b342c73ef..dee37169acb 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
@@ -133,7 +133,9 @@ class ExportSpringBoot extends Export {
copyGradleWrapper();
}
}
-
+ copyDockerFiles(BUILD_DIR);
+ String appJar = "target" + File.separator + ids[1] + "-" + ids[2] +
".jar";
+ copyReadme(BUILD_DIR, appJar);
if (cleanExportDir || !exportDir.equals(".")) {
// cleaning current dir can be a bit dangerous so only clean if
explicit enabled
// otherwise always clean export-dir to avoid stale data
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.legacy-jar
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.legacy-jar
index 3dc54e22384..83e831068ad 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.legacy-jar
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.legacy-jar
@@ -15,6 +15,10 @@
# limitations under the License.
#
+#
+# WARNING: this image is deprecated and won't be used in future versions: use
Dockerfile instead
+#
+
####
# This Dockerfile is used in order to build a container that runs the Quarkus
application in JVM mode
#
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native
index d5c985f5980..b71efa9abb7 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native
@@ -31,7 +31,11 @@
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus
#
###
-FROM registry.access.redhat.com/ubi8/ubi-minimal:8.8
+
+# Use an UBI image
+FROM registry.access.redhat.com/ubi8/ubi-minimal:8.10
+# Use a micro image
+# FROM quay.io/quarkus/quarkus-micro-image:2.0
WORKDIR /work/
RUN chown 1001 /work \
&& chmod "g+rwX" /work \
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native-micro
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native-micro
index ea5c4c969b4..6b0e613988e 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native-micro
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.native-micro
@@ -15,6 +15,10 @@
# limitations under the License.
#
+#
+# WARNING: this image is deprecated and won't be used in future versions: use
Dockerfile.native instead
+#
+
####
# This Dockerfile is used in order to build a container that runs the Quarkus
application in native (no JVM) mode.
# It uses a micro base image, tuned for Quarkus native executables.
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.legacy-jar
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/Dockerfile.tmpl
similarity index 89%
copy from
dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.legacy-jar
copy to
dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/Dockerfile.tmpl
index 3dc54e22384..086309d2edc 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/quarkus-docker/Dockerfile.legacy-jar
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/Dockerfile.tmpl
@@ -20,15 +20,15 @@
#
# Before building the container image run:
#
-# ./mvnw package -Dquarkus.package.type=legacy-jar
+# ./mvnw package
#
# Then, build the image with:
#
-# docker build -f src/main/docker/Dockerfile.legacy-jar -t
quarkus/code-with-quarkus-legacy-jar .
+# docker build -f src/main/docker/Dockerfile.jvm -t
quarkus/code-with-quarkus-jvm .
#
# Then run the container using:
#
-# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-legacy-jar
+# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-jvm
#
# If you want to include the debug port into your docker image
# you will have to expose the debug port (default 5005 being the default) like
this : EXPOSE 8080 5005.
@@ -37,7 +37,7 @@
#
# Then run the container using :
#
-# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-legacy-jar
+# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-jvm
#
# This image uses the `run-java.sh` script to run the application.
# This scripts computes the command line to execute your Java application, and
@@ -94,17 +94,16 @@
# accessed directly. (example: "foo.example.com,bar.example.com")
#
###
-FROM registry.access.redhat.com/ubi8/openjdk-17:1.16
+FROM registry.access.redhat.com/ubi8/openjdk-17:1.20
-ENV LANGUAGE='en_US:en'
+COPY --chown=185 target/{{ .AppJar }} /deployments/
-
-COPY target/lib/* /deployments/lib/
-COPY target/*-runner.jar /deployments/quarkus-run.jar
-
-EXPOSE 8080
+# Uncomment to expose any given port
+# EXPOSE 8080
USER 185
-ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0
-Djava.util.logging.manager=org.jboss.logmanager.LogManager"
-ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
+# Uncomment to provide any Java option
+# ENV JAVA_OPTS=""
+ENV JAVA_APP_JAR="/deployments/{{ .AppJar }}"
ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ]
+
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/readme.md.tmpl
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/readme.md.tmpl
new file mode 100644
index 00000000000..e21e3135e42
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/readme.md.tmpl
@@ -0,0 +1,31 @@
+# How to build and run a Camel application
+
+This project was generated using [Camel
Jbang](https://camel.apache.org/manual/camel-jbang.html). Please, refer the the
online documentation for learning more about how to configure the export of
your Camel application.
+
+This is a brief guide explaining how to build, "containerize" and run your
Camel application.
+
+## Build the Maven project
+
+```bash
+./mvnw clean package
+```
+
+The application could now immediately run:
+
+```bash
+java -jar {{ .AppRuntimeJar }}
+```
+
+## Create a Docker container
+
+You can create a container image directly from the `src/main/docker`
resources. Here you have a precompiled base configuration which can be enhanced
with any further required configuration.
+
+```bash
+docker build -f src/main/docker/Dockerfile -t {{ .ArtifactId }}:{{ .Version }}
.
+```
+
+Once the application is published, you can run it directly from the container:
+
+```bash
+docker run -it {{ .ArtifactId }}:{{ .Version }}
+```
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java
index 97b925a9be2..e3ce99d3681 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java
@@ -172,14 +172,22 @@ class ExportTest {
@ParameterizedTest
@MethodSource("runtimeProvider")
- public void shouldGenerateReproducibleBuild(RuntimeType rt) throws
Exception {
+ public void shouldGenerateContent(RuntimeType rt) throws Exception {
Export command = createCommand(rt, new String[] {
"classpath:route.yaml" },
"--gav=examples:route:1.0.0", "--dir=" + workingDir,
"--quiet");
int exit = command.doCall();
Assertions.assertEquals(0, exit);
+ // In this test we can validate any generic resource that must be
created along the export.
+ // Exporting once to reduce the time to execute the test and the
resource required to test.
+
+ // Reproducible build
Model model = readMavenModel();
Assertions.assertNotNull(model.getProperties().get("project.build.outputTimestamp"));
+ // Dockerfile
+ Assertions.assertTrue(new File(workingDir + "/src/main/docker",
"Dockerfile").exists());
+ // Readme
+ Assertions.assertTrue(new File(workingDir, "readme.md").exists());
}
}