You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/10/04 11:01:07 UTC

[camel] branch CAMEL-19939/jbang-dependency-copy created (now 6eae9d2e02f)

This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a change to branch CAMEL-19939/jbang-dependency-copy
in repository https://gitbox.apache.org/repos/asf/camel.git


      at 6eae9d2e02f CAMEL-19939: camel-jbang - dependency copy - Use camel-tooling-maven

This branch includes the following new commits:

     new 6eae9d2e02f CAMEL-19939: camel-jbang - dependency copy - Use camel-tooling-maven

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[camel] 01/01: CAMEL-19939: camel-jbang - dependency copy - Use camel-tooling-maven

Posted by nf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch CAMEL-19939/jbang-dependency-copy
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6eae9d2e02f120b1a4f4dddfb0d4b4cd9a58424d
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Wed Oct 4 13:00:39 2023 +0200

    CAMEL-19939: camel-jbang - dependency copy - Use camel-tooling-maven
---
 .../modules/ROOT/pages/camel-jbang.adoc            |   1 -
 .../dsl/jbang/core/commands/DependencyCopy.java    | 126 +++++++++------------
 2 files changed, 51 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 f3ab6737ffb..2b8ed14ae2b 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 6be4db2370d..98679576686 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,27 @@
  */
 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 {
 
-    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 +46,53 @@ public class DependencyCopy extends Export {
         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<MavenArtifact> 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)) {
+                    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();
+    }
 }