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 2023/02/01 18:24:34 UTC

[camel] branch main updated: CAMEL-18990: camel-jbang - Export to Quarkus should add resources for native compilation

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 38bd4f93b6d CAMEL-18990: camel-jbang - Export to Quarkus should add resources for native compilation
38bd4f93b6d is described below

commit 38bd4f93b6dfb062d51f7b6f5e5992f6524170ed
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Feb 1 19:24:15 2023 +0100

    CAMEL-18990: camel-jbang - Export to Quarkus should add resources for native compilation
---
 .../dsl/jbang/core/commands/ExportBaseCommand.java | 18 ++++---
 .../dsl/jbang/core/commands/ExportQuarkus.java     | 61 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 6 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 a652b6f86f1..0995c7a9403 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
@@ -370,13 +370,16 @@ abstract class ExportBaseCommand extends CamelCommand {
         if (profile.exists()) {
             RuntimeUtil.loadProperties(prop2, profile);
         }
+        prop2.putAll(prop);
+        prepareApplicationProperties(prop2);
 
         for (Map.Entry<Object, Object> entry : prop.entrySet()) {
             String key = entry.getKey().toString();
-            boolean skip = "camel.main.routesCompileDirectory".equals(key)
+            boolean skip = !key.startsWith("camel.main")
+                    || "camel.main.routesCompileDirectory".equals(key)
                     || "camel.main.routesReloadEnabled".equals(key);
-            if (!skip && key.startsWith("camel.main")) {
-                prop2.put(entry.getKey(), entry.getValue());
+            if (skip) {
+                prop2.remove(key);
             }
         }
 
@@ -398,10 +401,9 @@ abstract class ExportBaseCommand extends CamelCommand {
                 // files are now loaded in classpath
                 v = v.replaceAll("file:", "classpath:");
                 if ("camel.main.routesIncludePattern".equals(k)) {
-                    // camel.main.routesIncludePattern should remove all .java as we use spring boot
-                    // to load them
+                    // camel.main.routesIncludePattern should remove all .java as we use move them to regular src/main/java
                     // camel.main.routesIncludePattern should remove all file: classpath: as we copy
-                    // them to src/main/resources/camel where camel auto-load from
+                    // them to src/main/resources/camel where camel autoload from
                     v = Arrays.stream(v.split(","))
                             .filter(n -> !n.endsWith(".java") && !n.startsWith("file:") && !n.startsWith("classpath:"))
                             .collect(Collectors.joining(","));
@@ -417,6 +419,10 @@ abstract class ExportBaseCommand extends CamelCommand {
         }
     }
 
+    protected void prepareApplicationProperties(Properties properties) {
+        // noop
+    }
+
     protected void copyMavenWrapper() throws Exception {
         File wrapper = new File(BUILD_DIR, ".mvn/wrapper");
         wrapper.mkdirs();
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 cc44f627417..bdfcda47383 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
@@ -20,9 +20,13 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.StringJoiner;
+import java.util.stream.Collectors;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -143,6 +147,63 @@ class ExportQuarkus extends Export {
         return 0;
     }
 
+    @Override
+    protected void prepareApplicationProperties(Properties properties) {
+        // quarkus native compilation only works if we specify each resource explicit
+
+        StringJoiner sj = new StringJoiner(",");
+        StringJoiner sj2 = new StringJoiner(",");
+        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
+            String k = entry.getKey().toString();
+            String v = entry.getValue().toString();
+
+            if ("camel.main.routesIncludePattern".equals(k)) {
+                v = Arrays.stream(v.split(","))
+                        .map(ExportQuarkus::removeScheme) // remove scheme and routes are in camel sub-folder
+                        .map(s -> "camel/" + s)
+                        .collect(Collectors.joining(","));
+                sj.add(v);
+            }
+            // extra classpath files
+            if ("camel.jbang.classpathFiles".equals(k)) {
+                v = Arrays.stream(v.split(","))
+                        .map(ExportQuarkus::removeScheme) // remove scheme
+                        .collect(Collectors.joining(","));
+                sj2.add(v);
+            }
+        }
+
+        String routes = sj.length() > 0 ? sj.toString() : null;
+        String extra = sj2.length() > 0 ? sj2.toString() : null;
+
+        if (routes != null || extra != null) {
+            sj = new StringJoiner(",");
+            String e = properties.getProperty("quarkus.native.resources.includes");
+            if (e != null) {
+                sj.add(e);
+            }
+            if (routes != null) {
+                sj.add(routes);
+            }
+            if (extra != null) {
+                sj.add(extra);
+            }
+            if (sj.length() > 0) {
+                properties.setProperty("quarkus.native.resources.includes", sj.toString());
+            }
+            if (routes != null) {
+                properties.setProperty("camel.main.routes-include-pattern", routes);
+            }
+        }
+    }
+
+    private static String removeScheme(String s) {
+        if (s.contains(":")) {
+            return StringHelper.after(s, ":");
+        }
+        return s;
+    }
+
     private void createGradleProperties(File output) throws Exception {
         InputStream is = ExportQuarkus.class.getClassLoader().getResourceAsStream("templates/quarkus-gradle-properties.tmpl");
         String context = IOHelper.loadText(is);