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/04 13:47:24 UTC

[camel] branch main updated: CAMEL-18050: camel-jbang - Package uber jar without first run

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 a7a21391a3d CAMEL-18050: camel-jbang - Package uber jar without first run
a7a21391a3d is described below

commit a7a21391a3d726baf96d38ca1d6b3ac70775bf2b
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed May 4 15:47:09 2022 +0200

    CAMEL-18050: camel-jbang - Package uber jar without first run
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 46 ++++++++++++++++------
 .../camel/dsl/jbang/core/commands/UberJar.java     | 13 ++++++
 2 files changed, 48 insertions(+), 11 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 d842f317301..f780131a9bc 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
@@ -76,6 +76,7 @@ class Run implements Callable<Integer> {
     private CamelContext context;
     private File lockFile;
     private ScheduledExecutorService executor;
+    private boolean silentRun;
 
     @Parameters(description = "The Camel file(s) to run. If no files specified then application.properties is used as source for which files to run.",
                 arity = "0..9")
@@ -166,6 +167,12 @@ class Run implements Callable<Integer> {
         }
     }
 
+    protected Integer runSilent() throws Exception {
+        // just boot silently and exit
+        silentRun = true;
+        return run();
+    }
+
     private void writeSetting(KameletMain main, Properties existing, String key, String value) {
         String val = existing != null ? existing.getProperty(key, value) : value;
         if (val != null) {
@@ -248,18 +255,23 @@ class Run implements Callable<Integer> {
         if (files == null || files.length == 0) {
             // special when no files have been specified then we use application.properties as source to know what to run
             File source = new File("application.properties");
-            if (!source.exists()) {
+            if (source.exists()) {
+                applicationProperties = loadApplicationProperties(source);
+                // logging level may be configured in the properties file
+                loggingLevel = applicationProperties.getProperty("loggingLevel", loggingLevel);
+            } else if (!silentRun && !source.exists()) {
                 System.out.println("Cannot run because application.properties file does not exist");
                 return 1;
+            } else {
+                // silent-run then auto-detect all files
+                files = new File(".").list();
             }
-            applicationProperties = loadApplicationProperties(source);
-
-            // logging level may be configured in the properties file
-            loggingLevel = applicationProperties.getProperty("loggingLevel", loggingLevel);
         }
 
         // configure logging first
-        if (logging) {
+        if (silentRun) {
+            RuntimeUtil.configureLog("off");
+        } else if (logging) {
             RuntimeUtil.configureLog(loggingLevel);
             writeSettings("loggingLevel", loggingLevel);
         } else {
@@ -313,6 +325,12 @@ class Run implements Callable<Integer> {
             }
         }
 
+        if (silentRun) {
+            // do not run for very long in silent run
+            // TODO main.addInitialProperty("camel.main.autoStartup", "false");
+            // need modeline-parser to work
+            main.addInitialProperty("camel.main.durationMaxSeconds", "1");
+        }
         writeSetting(main, applicationProperties, "camel.main.durationMaxMessages",
                 () -> maxMessages > 0 ? String.valueOf(maxMessages) : null);
         writeSetting(main, applicationProperties, "camel.main.durationMaxSeconds",
@@ -536,14 +554,20 @@ class Run implements Callable<Integer> {
     }
 
     private boolean knownFile(String file) throws Exception {
-        String ext = FileUtil.onlyExt(file, true);
-        if (ext != null) {
+        // always include kamelets
+        String ext = FileUtil.onlyExt(file, false);
+        if ("kamelet.yaml".equals(ext)) {
+            return true;
+        }
+
+        String ext2 = FileUtil.onlyExt(file, true);
+        if (ext2 != null) {
             // special for yaml or xml, as we need to check if they have camel or not
-            if ("xml".equals(ext) || "yaml".equals(ext)) {
+            if ("xml".equals(ext2) || "yaml".equals(ext2)) {
                 // load content into memory
                 try (FileInputStream fis = new FileInputStream(file)) {
                     String data = IOHelper.loadText(fis);
-                    if ("xml".equals(ext)) {
+                    if ("xml".equals(ext2)) {
                         return data.contains("<routes") || data.contains("<routeConfiguration") || data.contains("<rests");
                     } else {
                         return data.contains("- from:") || data.contains("- route:") || data.contains("- route-configuration:")
@@ -553,7 +577,7 @@ class Run implements Callable<Integer> {
             }
             // if the ext is an accepted file then we include it as a potential route
             // (java files need to be included as route to support pojos/processors with routes)
-            return Arrays.stream(ACCEPTED_FILE_EXT).anyMatch(e -> e.equalsIgnoreCase(ext));
+            return Arrays.stream(ACCEPTED_FILE_EXT).anyMatch(e -> e.equalsIgnoreCase(ext2));
         } else {
             // assume match as it can be wildcard or dir
             return true;
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 0e6d923dca6..d5c53d67ace 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
@@ -72,6 +72,14 @@ class UberJar implements Callable<Integer> {
     public Integer call() throws Exception {
         // 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 (!settings.exists()) {
+            // allow to automatic build
+            int silent = runSilently();
+            if (silent != 0) {
+                return silent;
+            }
+        }
+        settings = new File(Run.WORK_DIR + "/" + Run.RUN_SETTINGS_FILE);
         if (!settings.exists()) {
             System.out.println("Run Camel first to generate dependency file");
             return 0;
@@ -162,6 +170,11 @@ class UberJar implements Callable<Integer> {
         return 0;
     }
 
+    private Integer runSilently() throws Exception {
+        Run run = new Run();
+        return run.runSilent();
+    }
+
     private void copySettings(File settings) throws Exception {
         // the settings file itself
         File setting = new File(CLASSES_DIR, Run.RUN_SETTINGS_FILE);