This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch jib in repository https://gitbox.apache.org/repos/asf/camel.git
commit 0db13757042f816da4ede990d200de14bc9f7e80 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Aug 22 10:25:13 2024 +0200 CAMEL-21106: camel-jbang - Export to main should keep support for jib/jkube --- .../dsl/jbang/core/commands/ExportCamelMain.java | 83 +++++++++++++++++ .../src/main/resources/templates/main-pom.tmpl | 2 + .../dsl/jbang/core/commands/ExportMainJibTest.java | 101 +++++++++++++++++++++ .../src/test/resources/application.properties | 17 ++++ 4 files changed, 203 insertions(+) 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 4208a5af82c..507e31157c6 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 @@ -232,9 +232,92 @@ class ExportCamelMain extends Export { context = context.replaceFirst("\\{\\{ \\.CamelDependencies }}", sb.toString()); + // include docker/kubernetes with jib/jkube + context = enrichMavenPomJib(context, settings, profile); + IOHelper.writeText(context, new FileOutputStream(pom, false)); } + protected String enrichMavenPomJib(String context, File settings, File profile) throws Exception { + StringBuilder sb1 = new StringBuilder(); + StringBuilder sb2 = new StringBuilder(); + + // is kubernetes included? + Properties prop = new CamelCaseOrderedProperties(); + if (profile.exists()) { + RuntimeUtil.loadProperties(prop, profile); + } + // include additional build properties + boolean jib = prop.stringPropertyNames().stream().anyMatch(s -> s.startsWith("jib.")); + boolean jkube = prop.stringPropertyNames().stream().anyMatch(s -> s.startsWith("jkube.")); + // jib is used for docker and kubernetes, jkube is only used for kubernetes + if (jib || jkube) { + // include all jib/jkube/label properties + String fromImage = null; + for (String key : prop.stringPropertyNames()) { + String value = prop.getProperty(key); + if ("jib.from.image".equals(key)) { + fromImage = value; + } + boolean accept = key.startsWith("jkube.") || key.startsWith("jib.") || key.startsWith("label."); + if (accept) { + sb1.append(String.format(" <%s>%s</%s>%n", key, value, key)); + } + } + // from image is mandatory so use a default image if none provided + if (fromImage == null) { + fromImage = "eclipse-temurin:" + javaVersion + "-jre"; + sb1.append(String.format(" <%s>%s</%s>%n", "jib.from.image", fromImage, "jib.from.image")); + } + + InputStream is = ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/main-docker-pom.tmpl"); + String context2 = IOHelper.loadText(is); + IOHelper.close(is); + + context2 = context2.replaceFirst("\\{\\{ \\.JibMavenPluginVersion }}", jibMavenPluginVersion(settings, prop)); + + // image from/to auth + String auth = ""; + if (prop.stringPropertyNames().stream().anyMatch(s -> s.startsWith("jib.from.auth."))) { + is = ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/main-docker-from-auth-pom.tmpl"); + auth = IOHelper.loadText(is); + IOHelper.close(is); + } + context2 = context2.replace("{{ .JibFromImageAuth }}", auth); + auth = ""; + if (prop.stringPropertyNames().stream().anyMatch(s -> s.startsWith("jib.to.auth."))) { + is = ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/main-docker-to-auth-pom.tmpl"); + auth = IOHelper.loadText(is); + IOHelper.close(is); + } + context2 = context2.replace("{{ .JibToImageAuth }}", auth); + // http port setting + int port = httpServerPort(settings); + if (port == -1) { + port = 8080; + } + context2 = context2.replaceFirst("\\{\\{ \\.Port }}", String.valueOf(port)); + sb2.append(context2); + // jkube is only used for kubernetes + if (jkube) { + is = ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/main-kubernetes-pom.tmpl"); + String context3 = IOHelper.loadText(is); + IOHelper.close(is); + context3 = context3.replaceFirst("\\{\\{ \\.JkubeMavenPluginVersion }}", + jkubeMavenPluginVersion(settings, prop)); + sb2.append(context3); + } + } + + // remove empty lines + String s1 = sb1.toString().replaceAll("(\\r?\\n){2,}", "\n"); + String s2 = sb2.toString().replaceAll("(\\r?\\n){2,}", "\n"); + + context = context.replace("{{ .CamelKubernetesProperties }}", s1); + context = context.replace("{{ .CamelKubernetesPlugins }}", s2); + return context; + } + @Override protected Set<String> resolveDependencies(File settings, File profile) throws Exception { Set<String> answer = super.resolveDependencies(settings, profile); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-pom.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-pom.tmpl index 08f5eebc89c..b454ee451ec 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-pom.tmpl +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-pom.tmpl @@ -10,6 +10,7 @@ <properties> <java.version>{{ .JavaVersion }}</java.version> {{ .BuildProperties }} +{{ .CamelKubernetesProperties }} </properties> <dependencyManagement> @@ -125,6 +126,7 @@ </execution> </executions> </plugin> +{{ .CamelKubernetesPlugins }} </plugins> </build> diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportMainJibTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportMainJibTest.java new file mode 100644 index 00000000000..3a021e7fe7f --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportMainJibTest.java @@ -0,0 +1,101 @@ +/* + * 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.camel.dsl.jbang.core.commands; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.stream.Stream; + +import org.apache.camel.dsl.jbang.core.common.RuntimeType; +import org.apache.camel.util.FileUtil; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import picocli.CommandLine; + +class ExportMainJibTest { + + private File workingDir; + private File profile = new File(".", "application.properties"); + + @BeforeEach + public void setup() throws IOException { + workingDir = Files.createTempDirectory("camel-export").toFile(); + } + + @AfterEach + public void end() throws IOException { + // force removing, since deleteOnExit is not removing. + FileUtil.removeDir(workingDir); + FileUtil.deleteFile(profile); + } + + private static Stream<Arguments> runtimeProvider() { + return Stream.of( + Arguments.of(RuntimeType.main)); + } + + @ParameterizedTest + @MethodSource("runtimeProvider") + public void shouldGenerateProjectWithJib(RuntimeType rt) throws Exception { + // prepare as we need application.properties that contains jib settings + Files.copy(new File("src/test/resources/application.properties").toPath(), profile.toPath(), + StandardCopyOption.REPLACE_EXISTING); + + Export command = new ExportCamelMain(new CamelJBangMain()); + CommandLine.populateCommand(command, "--gav=examples:route:1.0.0", "--dir=" + workingDir, + "--runtime=%s".formatted(rt.runtime()), "target/test-classes/route.yaml"); + int exit = command.doCall(); + + Assertions.assertEquals(0, exit); + Model model = readMavenModel(); + Assertions.assertEquals("examples", model.getGroupId()); + Assertions.assertEquals("route", model.getArtifactId()); + Assertions.assertEquals("1.0.0", model.getVersion()); + Assertions.assertEquals("17", model.getProperties().getProperty("java.version")); + Assertions.assertEquals("abc", model.getProperties().getProperty("jib.label")); + Assertions.assertEquals("eclipse-temurin:17-jre", model.getProperties().getProperty("jib.from.image")); + + // should contain jib plugin + Assertions.assertEquals(4, model.getBuild().getPlugins().size()); + Plugin p = model.getBuild().getPlugins().get(3); + Assertions.assertEquals("com.google.cloud.tools", p.getGroupId()); + Assertions.assertEquals("jib-maven-plugin", p.getArtifactId()); + + command.printConfigurationValues("export command"); + } + + private Model readMavenModel() throws Exception { + File f = workingDir.toPath().resolve("pom.xml").toFile(); + Assertions.assertTrue(f.isFile(), "Not a pom.xml file: " + f); + MavenXpp3Reader mavenReader = new MavenXpp3Reader(); + Model model = mavenReader.read(new FileReader(f)); + model.setPomFile(f); + return model; + } + +} diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/resources/application.properties b/dsl/camel-jbang/camel-jbang-core/src/test/resources/application.properties new file mode 100644 index 00000000000..ab1a3c700f7 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/test/resources/application.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +jib.label=abc \ No newline at end of file
