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 e4c504ef760598ee6b65ca1ff9e7a18d8a8c94eb Author: Claus Ibsen <[email protected]> AuthorDate: Thu Apr 21 18:38:27 2022 +0200 CAMEL-17998: camel-jbang - fat-jar command --- .../dsl/jbang/core/commands/CamelJBangMain.java | 1 - .../dsl/jbang/core/commands/DependencyTree.java | 72 ---------------------- .../camel/dsl/jbang/core/commands/FarJar.java | 66 +++++++++++++++----- .../apache/camel/dsl/jbang/core/commands/Run.java | 51 ++++++++++++--- 4 files changed, 93 insertions(+), 97 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java index 57a4c2d0ed8..ed8eaa64caa 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java @@ -38,7 +38,6 @@ public class CamelJBangMain implements Callable<Integer> { .addSubcommand("others", new SearchOthers())) .addSubcommand("create", new CommandLine(new Create()) .addSubcommand("fat-jar", new FarJar()) - .addSubcommand("dependency-tree", new DependencyTree()) .addSubcommand("project", new Project())) .addSubcommand("generate", new CommandLine(new CodeGenerator()) .addSubcommand("rest", new CodeRestGenerator())); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyTree.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyTree.java deleted file mode 100644 index 54cd9bd85ca..00000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyTree.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.net.URI; -import java.nio.file.Files; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; - -import groovy.grape.Grape; -import groovy.lang.GroovyClassLoader; -import org.apache.camel.main.KameletMain; -import org.apache.camel.main.MavenGav; -import picocli.CommandLine.Command; -import picocli.CommandLine.Option; - -@Command(name = "dependency-tree", description = "Displays the dependency tree") -@Deprecated -class DependencyTree implements Callable<Integer> { - - //CHECKSTYLE:OFF - @Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands") - private boolean helpRequested = false; - //CHECKSTYLE:ON - - @Override - public Integer call() throws Exception { - File file = new File(Run.DEPENDENCY_FILE); - if (!file.exists()) { - System.out.println("Run Camel first to generate dependency file"); - return 0; - } - - ClassLoader parentCL = KameletMain.class.getClassLoader(); - final GroovyClassLoader gcl = new GroovyClassLoader(parentCL); - final List<String> lines = Files.readAllLines(file.toPath()); - for (String l : lines) { - MavenGav gav = MavenGav.parseGav(null, l); - - Map<String, Object> map = new HashMap<>(); - map.put("classLoader", gcl); - map.put("group", gav.getGroupId()); - map.put("module", gav.getArtifactId()); - map.put("version", gav.getVersion()); - map.put("classifier", ""); - - URI[] u = Grape.resolve(map, map); - System.out.println(Arrays.asList(u)); - } - - return 0; - } - -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/FarJar.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/FarJar.java index 07b189026c7..cfa5e58580b 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/FarJar.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/FarJar.java @@ -17,6 +17,7 @@ package org.apache.camel.dsl.jbang.core.commands; import java.io.File; +import java.io.FileInputStream; import java.net.URI; import java.nio.file.Files; import java.util.HashMap; @@ -30,6 +31,8 @@ import groovy.lang.GroovyClassLoader; import org.apache.camel.main.KameletMain; import org.apache.camel.main.MavenGav; import org.apache.camel.util.FileUtil; +import org.apache.camel.util.OrderedProperties; +import org.apache.camel.util.StringHelper; import picocli.CommandLine.Command; import picocli.CommandLine.Option; @@ -43,8 +46,8 @@ class FarJar implements Callable<Integer> { @Override public Integer call() throws Exception { - File file = new File(Run.DEPENDENCY_FILE); - if (!file.exists()) { + File settings = new File(Run.RUN_SETTINGS_FILE); + if (!settings.exists()) { System.out.println("Run Camel first to generate dependency file"); return 0; } @@ -57,34 +60,67 @@ class FarJar implements Callable<Integer> { ClassLoader parentCL = KameletMain.class.getClassLoader(); final GroovyClassLoader gcl = new GroovyClassLoader(parentCL); - List<String> lines = Files.readAllLines(file.toPath()); + List<String> lines = Files.readAllLines(settings.toPath()); // include camel-kamelet-main as its a core dependency needed Optional<MavenGav> first = lines.stream() - .map(l -> MavenGav.parseGav(null, l)) + .filter(l -> l.startsWith("dependency=")) + .map(l -> MavenGav.parseGav(null, StringHelper.after(l, "dependency="))) .filter(g -> "org.apache.camel".equals(g.getGroupId())) .findFirst(); if (first.isPresent()) { String v = first.get().getVersion(); - lines.add(0, "mvn:org.apache.camel:camel-kamelet-main:" + v); + lines.add(0, "dependency=mvn:org.apache.camel:camel-kamelet-main:" + v); } for (String l : lines) { - MavenGav gav = MavenGav.parseGav(null, l); - Map<String, Object> map = new HashMap<>(); - map.put("classLoader", gcl); - map.put("group", gav.getGroupId()); - map.put("module", gav.getArtifactId()); - map.put("version", gav.getVersion()); - map.put("classifier", ""); - - URI[] u = Grape.resolve(map, map); - copyJars(u, target); + if (l.startsWith("dependency=")) { + l = StringHelper.after(l, "dependency="); + MavenGav gav = MavenGav.parseGav(null, l); + Map<String, Object> map = new HashMap<>(); + map.put("classLoader", gcl); + map.put("group", gav.getGroupId()); + map.put("module", gav.getArtifactId()); + map.put("version", gav.getVersion()); + map.put("classifier", ""); + + URI[] u = Grape.resolve(map, map); + copyJars(u, target); + } } + // copy route files + // TODO: 3rd party .jar should be in lib + target = new File("target/camel-app/"); + copyFiles(settings, target); + + // copy settings also (but not as hidden file) + File out = new File(target, Run.RUN_SETTINGS_FILE.substring(1)); + Files.copy(settings.toPath(), out.toPath()); + return 0; } + private void copyFiles(File settings, File target) throws Exception { + // read the settings file and find the files to copy + OrderedProperties prop = new OrderedProperties(); + prop.load(new FileInputStream(settings)); + + String files = prop.getProperty("camel.main.routesIncludePattern"); + if (files != null) { + for (String f : files.split(",")) { + if (f.startsWith("file:")) { + f = f.substring(5); + File s = new File(f); + if (s.exists() && s.isFile()) { + File out = new File(target, s.getName()); + Files.copy(s.toPath(), out.toPath()); + } + } + } + } + } + private void copyJars(URI[] uris, File target) throws Exception { for (URI u : uris) { File f = new File(u.toURL().getFile()); 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 4a35717a858..d81260e2fd2 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 @@ -47,11 +47,12 @@ import static org.apache.camel.dsl.jbang.core.commands.GitHubHelper.fetchGithubU @Command(name = "run", description = "Run Camel") class Run implements Callable<Integer> { - public static final String DEPENDENCY_FILE = ".camel-jbang-runtime-dependency-tree.yaml"; + 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" }; + private FileOutputStream settings; private CamelContext context; private File lockFile; private ScheduledExecutorService executor; @@ -161,11 +162,15 @@ class Run implements Callable<Integer> { } private int run() throws Exception { + settings = new FileOutputStream(RUN_SETTINGS_FILE, false); + // configure logging first if (logging) { RuntimeUtil.configureLog(loggingLevel); + writeSettings("loggingLevel", loggingLevel); } else { RuntimeUtil.configureLog("off"); + writeSettings("loggingLevel", "off"); } KameletMain main; @@ -174,28 +179,30 @@ class Run implements Callable<Integer> { main = new KameletMain(); } else { main = new KameletMain("file://" + localKameletDir); + writeSettings("localKameletDir", localKameletDir); } - final FileOutputStream fos = new FileOutputStream(DEPENDENCY_FILE, false); main.setDownloadListener((groupId, artifactId, version) -> { String line = "mvn:" + groupId + ":" + artifactId + ":" + version; - try { - fos.write(line.getBytes(StandardCharsets.UTF_8)); - fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8)); - } catch (Exception e) { - // ignore - } + writeSettings("dependency", line); }); main.setAppName("Apache Camel (JBang)"); main.addInitialProperty("camel.main.name", name); + writeSettings("camel.main.name", name); + // shutdown quickly main.addInitialProperty("camel.main.shutdownTimeout", "5"); + writeSettings("camel.main.shutdownTimeout", "5"); + // turn off lightweight if we have routes reload enabled main.addInitialProperty("camel.main.routesReloadEnabled", reload ? "true" : "false"); main.addInitialProperty("camel.main.sourceLocationEnabled", "true"); + writeSettings("camel.main.sourceLocationEnabled", "true"); main.addInitialProperty("camel.main.tracing", trace ? "true" : "false"); + writeSettings("camel.main.tracing", trace ? "true" : "false"); main.addInitialProperty("camel.main.modeline", modeline ? "true" : "false"); + writeSettings("camel.main.modeline", modeline ? "true" : "false"); // command line arguments if (property != null) { @@ -204,39 +211,50 @@ class Run implements Callable<Integer> { String v = StringHelper.after(p, "="); if (k != null && v != null) { main.addArgumentProperty(k, v); + writeSettings(k, v); } } } if (maxMessages > 0) { main.addInitialProperty("camel.main.durationMaxMessages", String.valueOf(maxMessages)); + writeSettings("camel.main.durationMaxMessages", String.valueOf(maxMessages)); } if (maxSeconds > 0) { main.addInitialProperty("camel.main.durationMaxSeconds", String.valueOf(maxSeconds)); + writeSettings("camel.main.durationMaxSeconds", String.valueOf(maxSeconds)); } if (maxIdleSeconds > 0) { main.addInitialProperty("camel.main.durationMaxIdleSeconds", String.valueOf(maxIdleSeconds)); + writeSettings("camel.main.durationMaxIdleSeconds", String.valueOf(maxIdleSeconds)); } if (port > 0) { main.addInitialProperty("camel.jbang.platform-http.port", String.valueOf(port)); + writeSettings("camel.jbang.platform-http.port", String.valueOf(port)); } if (console) { main.addInitialProperty("camel.jbang.console", "true"); + writeSettings("camel.jbang.console", "true"); } if (health) { main.addInitialProperty("camel.jbang.health", "true"); + writeSettings("camel.jbang.health", "true"); } if (jfr) { main.addInitialProperty("camel.jbang.jfr", "jfr"); + writeSettings("camel.jbang.jfr", "jfr"); } if (jfrProfile != null) { // turn on jfr if a profile was specified main.addInitialProperty("camel.jbang.jfr", "jfr"); + writeSettings("camel.jbang.jfr", "jfr"); main.addInitialProperty("camel.jbang.jfr-profile", jfrProfile); + writeSettings("camel.jbang.jfr-profile", jfrProfile); } if (dependencies != null) { main.addInitialProperty("camel.jbang.dependencies", dependencies); + writeSettings("camel.jbang.dependencies", dependencies); } if (fileLock) { @@ -345,11 +363,14 @@ class Run implements Callable<Integer> { sjReload.add(file.substring(5)); } } + if (js.length() > 0) { main.addInitialProperty("camel.main.routesIncludePattern", js.toString()); + writeSettings("camel.main.routesIncludePattern", js.toString()); } if (sjClasspathFiles.length() > 0) { main.addInitialProperty("camel.jbang.classpathFiles", sjClasspathFiles.toString()); + writeSettings("camel.jbang.classpathFiles", sjClasspathFiles.toString()); } if (sjKamelets.length() > 0) { @@ -360,6 +381,7 @@ class Run implements Callable<Integer> { loc = sjKamelets.toString(); } main.addInitialProperty("camel.component.kamelet.location", loc); + writeSettings("camel.component.kamelet.location", loc); } // we can only reload if file based @@ -392,6 +414,7 @@ class Run implements Callable<Integer> { loc = locations.toString(); } main.addInitialProperty("camel.component.properties.location", loc); + writeSettings("camel.component.properties.location", loc); } main.start(); @@ -400,7 +423,7 @@ class Run implements Callable<Integer> { main.run(); - IOHelper.close(fos); + IOHelper.close(settings); int code = main.getExitCode(); return code; @@ -454,4 +477,14 @@ class Run implements Callable<Integer> { return false; } + private void writeSettings(String key, String value) { + String line = key + "=" + value; + try { + settings.write(line.getBytes(StandardCharsets.UTF_8)); + settings.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8)); + } catch (Exception e) { + // ignore + } + } + }
