From 7ebe15faacd0928f541c0089a0b9c82751c9a0b0 Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Wed, 4 Oct 2023 13:00:39 +0200 Subject: [PATCH] CAMEL-19939: camel-jbang - dependency copy - Use camel-tooling-maven --- .../modules/ROOT/pages/camel-jbang.adoc | 1 - .../jbang/core/commands/DependencyCopy.java | 127 +++++++----------- 2 files changed, 52 insertions(+), 76 deletions(-) diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc index f3ab6737ffb05..2b8ed14ae2be9 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc @@ -2192,7 +2192,6 @@ org.apache.camel.kamelets:camel-kamelets:0.9.3 === Copying dependency JARs to a specific directory You can use the `camel dependency copy` command to copy the required JARs to a specific folder. -This command reuses Maven by invoking the `mvn dependency:copy-dependencies` command. IMPORTANT: The `camel dependency copy` and `camel dependency list` uses Apache Maven, This requires having Apache Maven installed, and `mvn` command in PATH environment, so Camel JBang diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java index 6be4db2370df1..57f60553f09ca 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopy.java @@ -16,21 +16,28 @@ */ package org.apache.camel.dsl.jbang.core.commands; -import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Properties; -import java.util.concurrent.TimeUnit; +import java.util.List; +import java.util.Set; -import org.apache.camel.dsl.jbang.core.common.RuntimeUtil; -import org.apache.camel.util.CamelCaseOrderedProperties; +import org.apache.camel.tooling.maven.MavenArtifact; +import org.apache.camel.tooling.maven.MavenDownloader; +import org.apache.camel.tooling.maven.MavenDownloaderImpl; +import org.apache.camel.tooling.maven.MavenGav; +import org.apache.camel.tooling.maven.MavenResolutionException; import org.apache.camel.util.FileUtil; import picocli.CommandLine; @CommandLine.Command(name = "copy", description = "Copies all Camel dependencies required to run to a specific directory") -public class DependencyCopy extends Export { +public class DependencyCopy extends DependencyList { + private static final Set EXCLUDED_GROUP_IDS = Set.of("org.fusesource.jansi", "org.apache.logging.log4j"); - protected static final String EXPORT_DIR = ".camel-jbang/export"; + private MavenDownloader downloader; @CommandLine.Option(names = { "--output-directory" }, description = "Directory where dependencies should be copied", defaultValue = "lib", required = true) @@ -40,83 +47,53 @@ public DependencyCopy(CamelJBangMain main) { super(main); } - @Override - public Integer doCall() throws Exception { - this.quiet = true; // be quiet and generate from fresh data to ensure the output is up-to-date - return super.doCall(); - } - - @Override - protected Integer export() throws Exception { - Integer answer = doExport(); - if (answer == 0) { - File buildDir = new File(EXPORT_DIR); - String outputDirectoryParameter = "-DoutputDirectory="; - if (Paths.get(outputDirectory).isAbsolute()) { - outputDirectoryParameter += outputDirectory; + private void createOutputDirectory() { + Path outputDirectoryPath = Paths.get(outputDirectory); + if (Files.exists(outputDirectoryPath)) { + if (Files.isDirectory(outputDirectoryPath)) { + FileUtil.removeDir(outputDirectoryPath.toFile()); } else { - outputDirectoryParameter += "../../" + outputDirectory; + System.err.println("Error creating the output directory: " + outputDirectory + + " is not a directory"); + return; } - Process p = Runtime.getRuntime() - .exec("mvn dependency:copy-dependencies -DincludeScope=compile -DexcludeGroupIds=org.fusesource.jansi,org.apache.logging.log4j " - + outputDirectoryParameter, - null, - buildDir); - boolean done = p.waitFor(60, TimeUnit.SECONDS); - if (!done) { - answer = 1; - } - if (p.exitValue() != 0) { - answer = p.exitValue(); - } - // cleanup dir after complete - FileUtil.removeDir(buildDir); } - return answer; + try { + Files.createDirectories(outputDirectoryPath); + } catch (IOException e) { + throw new UncheckedIOException("Error creating the output directory: " + outputDirectory, e); + } } - protected Integer doExport() throws Exception { - // read runtime and gav from profile if not configured - File profile = new File(getProfile() + ".properties"); - if (profile.exists()) { - Properties prop = new CamelCaseOrderedProperties(); - RuntimeUtil.loadProperties(prop, profile); - if (this.runtime == null) { - this.runtime = prop.getProperty("camel.jbang.runtime"); - } - if (this.gav == null) { - this.gav = prop.getProperty("camel.jbang.gav"); + @Override + protected void outputGav(MavenGav gav) { + try { + List artifacts = getDownloader().resolveArtifacts( + List.of(gav.toString()), Set.of(), true, gav.getVersion().contains("SNAPSHOT")); + for (MavenArtifact artifact : artifacts) { + Path target = Paths.get(outputDirectory, artifact.getFile().getName()); + if (Files.exists(target) || EXCLUDED_GROUP_IDS.contains(artifact.getGav().getGroupId())) { + continue; + } + Files.copy(artifact.getFile().toPath(), target); } - // allow configuring versions from profile - this.javaVersion = prop.getProperty("camel.jbang.javaVersion", this.javaVersion); - this.camelVersion = prop.getProperty("camel.jbang.camelVersion", this.camelVersion); - this.kameletsVersion = prop.getProperty("camel.jbang.kameletsVersion", this.kameletsVersion); - this.localKameletDir = prop.getProperty("camel.jbang.localKameletDir", this.localKameletDir); - this.quarkusGroupId = prop.getProperty("camel.jbang.quarkusGroupId", this.quarkusGroupId); - this.quarkusArtifactId = prop.getProperty("camel.jbang.quarkusArtifactId", this.quarkusArtifactId); - this.quarkusVersion = prop.getProperty("camel.jbang.quarkusVersion", this.quarkusVersion); - this.springBootVersion = prop.getProperty("camel.jbang.springBootVersion", this.springBootVersion); - } - - // use temporary export dir - exportDir = EXPORT_DIR; - if (gav == null) { - gav = "org.apache.camel:camel-jbang-dummy:1.0"; - } - if (runtime == null) { - runtime = "camel-main"; + } catch (MavenResolutionException e) { + System.err.println("Error resolving the artifact: " + gav + " due to: " + e.getMessage()); + } catch (IOException e) { + System.err.println("Error copying the artifact: " + gav + " due to: " + e.getMessage()); } + } - if ("spring-boot".equals(runtime) || "camel-spring-boot".equals(runtime)) { - return export(new ExportSpringBoot(getMain())); - } else if ("quarkus".equals(runtime) || "camel-quarkus".equals(runtime)) { - return export(new ExportQuarkus(getMain())); - } else if ("main".equals(runtime) || "camel-main".equals(runtime)) { - return export(new ExportCamelMain(getMain())); - } else { - System.err.println("Unknown runtime: " + runtime); - return 1; + private MavenDownloader getDownloader() { + if (downloader == null) { + init(); } + return downloader; } + private void init() { + this.downloader = new MavenDownloaderImpl(); + ((MavenDownloaderImpl) downloader).build(); + createOutputDirectory(); + } }