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/05/23 09:48:50 UTC

[camel] 05/05: CAMEL-18117: camel-jbang - Use profile for commands such as run, uber-jar, which allows the profile to control which properties file to use with settings.

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

commit 82ca7fb67be7fcf4e5298d1822814c68d0c2232a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon May 23 11:48:21 2022 +0200

    CAMEL-18117: camel-jbang - Use profile for commands such as run, uber-jar, which allows the profile to control which properties file to use with settings.
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 13 ++++----
 .../camel/dsl/jbang/core/commands/UberJar.java     | 35 +++++++++++++++++-----
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 1e9fd5f42c7..f7469387d45 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -23,6 +23,7 @@ import java.awt.datatransfer.UnsupportedFlavorException;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.FileSystems;
@@ -73,7 +74,7 @@ class Run extends CamelCommand {
     public static final String RUN_SETTINGS_FILE = "camel-jbang-run.properties";
 
     private static final String[] ACCEPTED_FILE_EXT
-            = new String[] { "properties", "java", "groovy", "js", "jsh", "kts", "xml", "yaml" };
+            = new String[] { "java", "groovy", "js", "jsh", "kts", "xml", "yaml" };
 
     private static final String OPENAPI_GENERATED_FILE = ".camel-jbang/generated-openapi.yaml";
     private static final String CLIPBOARD_GENERATED_FILE = ".camel-jbang/generated-clipboard";
@@ -287,8 +288,8 @@ class Run extends CamelCommand {
                     System.out.println("Cannot run because " + getProfile() + ".properties file does not exist");
                     return 1;
                 } else {
-                    // silent-run then auto-detect all files
-                    files = new File(".").list();
+                    // silent-run then auto-detect all files (except properties as they are loaded explicit or via profile)
+                    files = new File(".").list((dir, name) -> !name.endsWith(".properties"));
                 }
             }
         }
@@ -296,7 +297,7 @@ class Run extends CamelCommand {
         // configure logging first
         configureLogging();
 
-        final KameletMain main = createMainInstnace();
+        final KameletMain main = createMainInstance();
 
         final Set<String> downloaded = new HashSet<>();
         main.setDownloadListener((groupId, artifactId, version) -> {
@@ -394,7 +395,7 @@ class Run extends CamelCommand {
                     file = loadFromClipboard(file);
                 } else if (skipFile(file)) {
                     continue;
-                } else if (!knownFile(file)) {
+                } else if (!knownFile(file) && !file.endsWith(".properties")) {
                     // non known files to be added on classpath
                     sjClasspathFiles.add(file);
                     continue;
@@ -602,7 +603,7 @@ class Run extends CamelCommand {
         return file;
     }
 
-    private KameletMain createMainInstnace() {
+    private KameletMain createMainInstance() {
         KameletMain main;
         if (localKameletDir == null) {
             main = new KameletMain();
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java
index 4785ae6b6d9..ef14f4322f5 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java
@@ -29,6 +29,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.function.Predicate;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
 import java.util.jar.JarOutputStream;
@@ -72,6 +73,8 @@ class UberJar extends CamelCommand {
 
     @Override
     public Integer call() throws Exception {
+        File profile = new File(getProfile() + ".properties");
+
         // the settings file has information what to package in uber-jar so we need to read it from the run command
         File settings = new File(Run.WORK_DIR + "/" + Run.RUN_SETTINGS_FILE);
         if (fresh || !settings.exists()) {
@@ -100,8 +103,8 @@ class UberJar extends CamelCommand {
         copySourceFiles(settings, buildDir);
         // work sources
         copyWorkFiles(Run.WORK_DIR, buildDir);
-        // settings
-        copySettings(settings);
+        // settings and profile
+        copySettingsAndProfile(settings, profile);
         // log4j configuration
         InputStream is = UberJar.class.getResourceAsStream("/log4j2.properties");
         safeCopy(is, new File(CLASSES_DIR, "log4j2.properties"));
@@ -177,17 +180,32 @@ class UberJar extends CamelCommand {
         return code;
     }
 
-    private void copySettings(File settings) throws Exception {
+    private void copySettingsAndProfile(File settings, File profile) throws Exception {
         // the settings file itself
-        File setting = new File(CLASSES_DIR, Run.RUN_SETTINGS_FILE);
+        doCopySettingsAndProfile(settings, new File(CLASSES_DIR, Run.RUN_SETTINGS_FILE), null);
+        // and profile if exists
+        if (profile.exists()) {
+            // skip these
+            Predicate<String> skip = line -> line.startsWith("camel.main.routesIncludePattern")
+                    || line.startsWith("camel.jbang.classpathFiles")
+                    || line.startsWith("camel.component.kamelet.location")
+                    || line.startsWith("camel.component.properties.location");
+            doCopySettingsAndProfile(profile, new File(CLASSES_DIR, profile.getName()), skip);
+        }
+    }
 
+    private void doCopySettingsAndProfile(File source, File target, Predicate<String> skipKeys) throws Exception {
         // need to adjust file: scheme to classpath as the files are now embedded in the uber-jar directly
-        List<String> lines = Files.readAllLines(settings.toPath());
-        FileOutputStream fos = new FileOutputStream(setting, false);
+        List<String> lines = Files.readAllLines(source.toPath());
+        FileOutputStream fos = new FileOutputStream(target, false);
         for (String line : lines) {
+            line = line.trim();
             if (line.startsWith("camel.main.routesCompileDirectory")) {
                 continue; // skip as uber-jar should not compile to disk
             }
+            if (skipKeys != null && skipKeys.test(line)) {
+                continue;
+            }
             for (String k : SETTINGS_PROP_SOURCE_KEYS) {
                 line = fileToClasspath(line, k);
             }
@@ -328,11 +346,12 @@ class UberJar extends CamelCommand {
     }
 
     private void copyWorkFiles(String work, File target) throws Exception {
+        String profile = getProfile() + ".properties";
         File[] files = new File(work).listFiles();
         if (files != null) {
             for (File source : files) {
-                // only copy files and skip settings file as we do this later specially
-                if (source.isDirectory() || source.getName().equals(Run.RUN_SETTINGS_FILE)) {
+                // only copy files and skip settings/profile file as we do this later specially
+                if (source.isDirectory() || source.getName().equals(Run.RUN_SETTINGS_FILE) || source.getName().equals(profile)) {
                     continue;
                 }
                 File out = new File(target, source.getName());