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 d2d6ac346637122124f2644feba00362625e87e4 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Aug 22 10:51:37 2024 +0200 CAMEL-21106: camel-jbang - Export to main should keep support for jib/jkube --- .../dsl/jbang/core/commands/ExportCamelMain.java | 2 +- .../main/resources/templates/main-jkube-pom.tmpl | 24 +++ .../resources/templates/main-kubernetes-pom.tmpl | 208 --------------------- .../jbang/core/commands/ExportMainJkubeTest.java | 105 +++++++++++ .../src/test/resources/application-jib.properties | 2 +- ...jib.properties => application-jkube.properties} | 11 +- 6 files changed, 141 insertions(+), 211 deletions(-) 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 507e31157c6..d9d5a86ed61 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 @@ -300,7 +300,7 @@ class ExportCamelMain extends Export { sb2.append(context2); // jkube is only used for kubernetes if (jkube) { - is = ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/main-kubernetes-pom.tmpl"); + is = ExportCamelMain.class.getClassLoader().getResourceAsStream("templates/main-jkube-pom.tmpl"); String context3 = IOHelper.loadText(is); IOHelper.close(is); context3 = context3.replaceFirst("\\{\\{ \\.JkubeMavenPluginVersion }}", diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-jkube-pom.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-jkube-pom.tmpl new file mode 100644 index 00000000000..e016f531a14 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-jkube-pom.tmpl @@ -0,0 +1,24 @@ + <plugin> + <groupId>org.eclipse.jkube</groupId> + <artifactId>kubernetes-maven-plugin</artifactId> + <version>{{ .JkubeMavenPluginVersion }}</version> + <configuration> + <images> + <image> + <name>${jib.to.image}</name> + <build> + </build> + </image> + </images> + <resources> + <labels> + <all> + <property> + <name>${label.runtime}</name> + <value>camel</value> + </property> + </all> + </labels> + </resources> + </configuration> + </plugin> \ No newline at end of file diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-kubernetes-pom.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-kubernetes-pom.tmpl deleted file mode 100644 index 261ca1ca492..00000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/main-kubernetes-pom.tmpl +++ /dev/null @@ -1,208 +0,0 @@ -<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - - <groupId>{{ .GroupId }}</groupId> - <artifactId>{{ .ArtifactId }}</artifactId> - <version>{{ .Version }}</version> - - <properties> - <java.version>{{ .JavaVersion }}</java.version> -{{ .BuildProperties }} - </properties> - - <dependencyManagement> - <dependencies> - <!-- Camel BOM --> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-bom</artifactId> - <version>{{ .CamelVersion }}</version> - <type>pom</type> - <scope>import</scope> - </dependency> - </dependencies> - </dependencyManagement> - -{{ .MavenRepositories }} - - <dependencies> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-main</artifactId> - </dependency> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-health</artifactId> - </dependency> -{{ .CamelDependencies }} - - <!-- for logging in color --> - <dependency> - <groupId>org.fusesource.jansi</groupId> - <artifactId>jansi</artifactId> - <version>2.4.1</version> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-api</artifactId> - <version>2.23.1</version> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-slf4j2-impl</artifactId> - <version>2.23.1</version> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-core</artifactId> - <version>2.23.1</version> - </dependency> - - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-test-junit5</artifactId> - <scope>test</scope> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.13.0</version> - <configuration> - <source>${java.version}</source> - <target>${java.version}</target> - </configuration> - </plugin> - <!-- mvn camel:run or mvn camel:dev --> - <plugin> - <groupId>org.apache.camel</groupId> - <artifactId>camel-maven-plugin</artifactId> - <version>{{ .CamelVersion }}</version> - <configuration> - <mainClass>{{ .MainClassname }}</mainClass> - </configuration> - <executions> - <execution> - <goals> - <goal>prepare-fatjar</goal> - </goals> - </execution> - </executions> - </plugin> - <!-- package as runner jar --> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-assembly-plugin</artifactId> - <version>3.7.1</version> - <configuration> - <descriptors> - <descriptor>src/main/resources/assembly/runner.xml</descriptor> - </descriptors> - <archive> - <manifest> - <mainClass>{{ .MainClassname }}</mainClass> - </manifest> - <manifestEntries> - <Multi-Release>true</Multi-Release> - </manifestEntries> - </archive> - <appendAssemblyId>false</appendAssemblyId> - </configuration> - <executions> - <execution> - <id>make-assembly</id> - <phase>package</phase> - <goals> - <goal>single</goal> - </goals> - </execution> - </executions> - </plugin> - <plugin> - <groupId>org.eclipse.jkube</groupId> - <artifactId>kubernetes-maven-plugin</artifactId> - <version>${camel.main.jkube.version}</version> - <configuration> - <imagePullPolicy>${camel.main.kubernetes.image-pull-policy}</imagePullPolicy> - <buildStrategy>docker</buildStrategy> - <images> - <image> - <name>${camel.main.kubernetes.image-name}</name> - <build> - <from>eclipse-temurin:{{ .JavaVersion }}</from> - <createImageOptions> - <platform>linux/amd64</platform> - <platform>linux/arm64</platform> - </createImageOptions> - <ports> - <port>${camel.main.kubernetes.ports.http.container-port}</port> - </ports> - <entryPoint> - <exec> - <arg>java</arg> - <arg>-jar</arg> - <arg>/maven/${project.artifactId}-${project.version}.jar</arg> - </exec> - </entryPoint> - </build> - </image> - </images> - </configuration> - <executions> - <execution> - <goals> - <goal>build</goal> - <goal>resource</goal> - </goals> - <phase>package</phase> - </execution> - </executions> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-resources-plugin</artifactId> - <executions> - <execution> - <phase>package</phase> - <goals> - <goal>copy-resources</goal> - </goals> - <configuration> - <!-- For symmetry with Quarkus, copy kubernetes.yml to the same target location --> - <outputDirectory>${project.build.directory}/kubernetes</outputDirectory> - <resources> - <resource> - <directory>${project.build.directory}/classes/META-INF/jkube</directory> - </resource> - </resources> - </configuration> - </execution> - </executions> - </plugin> - </plugins> - </build> - - <profiles> - <profile> - <id>camel.debug</id> - <activation> - <property> - <name>camel.debug</name> - <value>true</value> - </property> - </activation> - <dependencies> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-debug</artifactId> - </dependency> - </dependencies> - </profile> - </profiles> - -</project> diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportMainJkubeTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportMainJkubeTest.java new file mode 100644 index 00000000000..b809c68c11c --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportMainJkubeTest.java @@ -0,0 +1,105 @@ +/* + * 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 ExportMainJkubeTest { + + 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 jkube settings + Files.copy(new File("src/test/resources/application-jkube.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 and jkube plugin + Assertions.assertEquals(5, 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()); + p = model.getBuild().getPlugins().get(4); + Assertions.assertEquals("org.eclipse.jkube", p.getGroupId()); + Assertions.assertEquals("kubernetes-maven-plugin", p.getArtifactId()); + Assertions.assertEquals("1.17.0", p.getVersion()); + + 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-jib.properties b/dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jib.properties index ab1a3c700f7..dae0d9e7910 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jib.properties +++ b/dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jib.properties @@ -14,4 +14,4 @@ ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- -jib.label=abc \ No newline at end of file +jib.label=abc diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jib.properties b/dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jkube.properties similarity index 74% copy from dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jib.properties copy to dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jkube.properties index ab1a3c700f7..f9ae1a54da3 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jib.properties +++ b/dsl/camel-jbang/camel-jbang-core/src/test/resources/application-jkube.properties @@ -14,4 +14,13 @@ ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- -jib.label=abc \ No newline at end of file +jib.label=abc +label.runtime=camel-main +jkube.version=1.17.0 +jkube.skip.build=true +jkube.imagePullPolicy=Always +jkube.enricher.jkube-controller.type=Deployment +jkube.enricher.jkube-service.port=80:8080,443:8080 +jkube.enricher.jkube-project-label.group=foo +jkube.enricher.jkube-project-label.provider=asf +jkube.recreate=true \ No newline at end of file
