You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/11/09 06:05:18 UTC
[camel] branch main updated: Add jkube to springboot export (#8693)
This is an automated email from the ASF dual-hosted git repository.
davsclaus 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 61447d837f8 Add jkube to springboot export (#8693)
61447d837f8 is described below
commit 61447d837f8d2b9bbad158844229c2a4cbf7e2cc
Author: Marat Gubaidullin <ma...@gmail.com>
AuthorDate: Wed Nov 9 01:05:12 2022 -0500
Add jkube to springboot export (#8693)
---
.../dsl/jbang/core/commands/ExportBaseCommand.java | 2 +-
.../dsl/jbang/core/commands/ExportSpringBoot.java | 53 +++++++++++++++++-----
.../resources/templates/apache-snapshot-maven.tmpl | 13 ++++++
.../main/resources/templates/jkube-profiles.tmpl | 50 ++++++++++++++++++++
.../main/resources/templates/spring-boot-pom.tmpl | 5 ++
5 files changed, 111 insertions(+), 12 deletions(-)
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
index 25a93183df2..3f2f8d1ab7a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
@@ -381,7 +381,7 @@ abstract class ExportBaseCommand extends CamelCommand {
String k = entry.getKey().toString();
String v = entry.getValue().toString();
- boolean skip = k.startsWith("camel.jbang.");
+ boolean skip = k.startsWith("camel.jbang.") || k.startsWith("jkube.");
if (skip) {
continue;
}
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 56c08900c55..7bbfa905a2c 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
@@ -18,12 +18,15 @@ package org.apache.camel.dsl.jbang.core.commands;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.catalog.DefaultCamelCatalog;
@@ -103,7 +106,7 @@ class ExportSpringBoot extends Export {
// gather dependencies
Set<String> deps = resolveDependencies(settings, profile);
if ("maven".equals(buildTool)) {
- createMavenPom(settings, new File(BUILD_DIR, "pom.xml"), deps);
+ createMavenPom(settings, profile, new File(BUILD_DIR, "pom.xml"), deps);
if (mavenWrapper) {
copyMavenWrapper();
}
@@ -132,12 +135,10 @@ class ExportSpringBoot extends Export {
IOHelper.writeText(text, new FileOutputStream(file, false));
}
- private void createMavenPom(File settings, File pom, Set<String> deps) throws Exception {
+ private void createMavenPom(File settings, File profile, File pom, Set<String> deps) throws Exception {
String[] ids = gav.split(":");
- InputStream is = ExportSpringBoot.class.getClassLoader().getResourceAsStream("templates/spring-boot-pom.tmpl");
- String context = IOHelper.loadText(is);
- IOHelper.close(is);
+ String context = readResourceTemplate("templates/spring-boot-pom.tmpl");
CamelCatalog catalog = loadSpringBootCatalog();
String camelVersion = catalog.getCatalogVersion();
@@ -149,8 +150,21 @@ class ExportSpringBoot extends Export {
context = context.replaceFirst("\\{\\{ \\.JavaVersion }}", javaVersion);
context = context.replaceFirst("\\{\\{ \\.CamelVersion }}", camelVersion);
+ // Convert jkube properties to maven properties
+ Properties allProps = new CamelCaseOrderedProperties();
+ RuntimeUtil.loadProperties(allProps, profile);
+
+ StringBuilder jkubeProperties = new StringBuilder();
+ allProps.stringPropertyNames().stream().filter(p -> p.startsWith("jkube")).forEach(key -> {
+ String value = allProps.getProperty(key);
+ jkubeProperties.append(" <").append(key).append(">").append(value).append("</").append(key).append(">\n");
+ });
+ context = context.replaceFirst(Pattern.quote("{{ .jkubeProperties }}"), Matcher.quoteReplacement(jkubeProperties.toString()));
+
+
Properties prop = new CamelCaseOrderedProperties();
RuntimeUtil.loadProperties(prop, settings);
+
String repos = prop.getProperty("camel.jbang.repos");
if (repos == null) {
context = context.replaceFirst("\\{\\{ \\.MavenRepositories }}", "");
@@ -206,15 +220,27 @@ class ExportSpringBoot extends Export {
}
context = context.replaceFirst("\\{\\{ \\.CamelDependencies }}", sb.toString());
+ // add apache snapshot repository if camel version ends with SNAPSHOT
+ String contextSnapshot = "";
+ if (camelVersion.endsWith("SNAPSHOT")) {
+ contextSnapshot = readResourceTemplate("templates/apache-snapshot-maven.tmpl");
+ }
+ context = context.replaceFirst(Pattern.quote("{{ .snapshotRepository }}"), Matcher.quoteReplacement(contextSnapshot));
+
+ // add jkube profiles if there is jkube version property
+ String jkubeProfiles = "";
+ if (allProps.getProperty("jkube.version") != null) {
+ jkubeProfiles = readResourceTemplate("templates/jkube-profiles.tmpl");
+ }
+ context = context.replaceFirst(Pattern.quote("{{ .jkubeProfiles }}"), Matcher.quoteReplacement(jkubeProfiles));
+
IOHelper.writeText(context, new FileOutputStream(pom, false));
}
private void createBuildGradle(File settings, File gradleBuild, Set<String> deps) throws Exception {
String[] ids = gav.split(":");
- InputStream is = ExportSpringBoot.class.getClassLoader().getResourceAsStream("templates/spring-boot-build-gradle.tmpl");
- String context = IOHelper.loadText(is);
- IOHelper.close(is);
+ String context = readResourceTemplate("templates/spring-boot-build-gradle.tmpl");
CamelCatalog catalog = loadSpringBootCatalog();
String camelVersion = catalog.getCatalogVersion();
@@ -293,9 +319,7 @@ class ExportSpringBoot extends Export {
}
private void createMainClassSource(File srcJavaDir, String packageName, String mainClassname) throws Exception {
- InputStream is = ExportSpringBoot.class.getClassLoader().getResourceAsStream("templates/spring-boot-main.tmpl");
- String context = IOHelper.loadText(is);
- IOHelper.close(is);
+ String context = readResourceTemplate("templates/spring-boot-main.tmpl");
context = context.replaceFirst("\\{\\{ \\.PackageName }}", packageName);
context = context.replaceAll("\\{\\{ \\.MainClassname }}", mainClassname);
@@ -355,4 +379,11 @@ class ExportSpringBoot extends Export {
return answer;
}
+ private String readResourceTemplate(String name) throws IOException {
+ InputStream is = ExportSpringBoot.class.getClassLoader().getResourceAsStream(name);
+ String text = IOHelper.loadText(is);
+ IOHelper.close(is);
+ return text;
+ }
+
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/apache-snapshot-maven.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/apache-snapshot-maven.tmpl
new file mode 100644
index 00000000000..251e3f10ffa
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/apache-snapshot-maven.tmpl
@@ -0,0 +1,13 @@
+ <repositories>
+ <repository>
+ <id>apache.snapshots</id>
+ <name>Apache Development Snapshot Repository</name>
+ <url>https://repository.apache.org/content/repositories/snapshots/</url>
+ <releases>
+ <enabled>false</enabled>
+ </releases>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
\ No newline at end of file
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/jkube-profiles.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/jkube-profiles.tmpl
new file mode 100644
index 00000000000..b683f0db5ef
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/jkube-profiles.tmpl
@@ -0,0 +1,50 @@
+ <profiles>
+ <profile>
+ <id>kubernetes</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.eclipse.jkube</groupId>
+ <artifactId>kubernetes-maven-plugin</artifactId>
+ <version>${jkube.version}</version>
+ <configuration>
+ <resources>
+ <labels>
+ <all>
+ <property>
+ <name>app.kubernetes.io/runtime</name>
+ <value>camel</value>
+ </property>
+ </all>
+ </labels>
+ </resources>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ <profile>
+ <id>openshift</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.eclipse.jkube</groupId>
+ <artifactId>openshift-maven-plugin</artifactId>
+ <version>${jkube.version}</version>
+ <configuration>
+ <resources>
+ <labels>
+ <all>
+ <property>
+ <name>app.openshift.io/runtime</name>
+ <value>camel</value>
+ </property>
+ </all>
+ </labels>
+ </resources>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/spring-boot-pom.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/spring-boot-pom.tmpl
index 416556690b6..18a185a7415 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/spring-boot-pom.tmpl
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/spring-boot-pom.tmpl
@@ -16,6 +16,7 @@
<properties>
<java.version>{{ .JavaVersion }}</java.version>
+{{ .jkubeProperties }}
</properties>
<dependencyManagement>
@@ -82,4 +83,8 @@
</plugins>
</build>
+{{ .snapshotRepository }}
+
+{{ .jkubeProfiles }}
+
</project>