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 e1acea463ad9 CAMEL-26931: camel-jbang - camel dependency copy resolves 
the dependencies together, so lib/ has one version of each library (#30017)
e1acea463ad9 is described below

commit e1acea463ad9696603e34f6f5ddba72399d900c7
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 16:05:54 2026 +0100

    CAMEL-26931: camel-jbang - camel dependency copy resolves the dependencies 
together, so lib/ has one version of each library (#30017)
    
    …ncies together, so lib/ has one version of each library
    
    camel dependency copy resolved each dependency on its own and copied every 
JAR whose file name was not there yet, so
    a route with kafka, https, aws2-s3 and mongodb gave 97 JARs with two 
versions of httpclient5, httpcore5, httpcore5-h2
    and slf4j-api. The dependencies are now resolved together, as one 
dependency graph (as camel run does since
    CAMEL-26901): 93 JARs, one version of each. When they cannot be resolved 
together, the ones that cannot be resolved are
    printed on stderr (exit 1) and the others are resolved together and copied.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  3 +
 .../modules/ROOT/pages/camel-jbang-projects.adoc   |  3 +-
 .../dsl/jbang/core/commands/DependencyCopy.java    | 72 +++++++++++++++++-----
 .../jbang/core/commands/DependencyCopyTest.java    | 40 +++++++++---
 4 files changed, 91 insertions(+), 27 deletions(-)

diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index d07f463c80fc..dde82b1575b0 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -3358,6 +3358,9 @@ file), the command asks there whether to overwrite the 
file.
 * `camel dependency copy` deletes the JAR files an earlier copy left in 
`--output-directory`, and keeps its other files:
   it deleted the whole directory (with `--output-directory=.` the project). It 
exits with `1` when a dependency cannot
   be resolved or copied (it printed the error and exited with `0`), and with 
`2` when `--output-directory` is a file.
+* `camel dependency copy` resolves the dependencies together, as one 
dependency graph, so `--output-directory` gets
+  one version of each library. It resolved each dependency on its own, and 
could copy two versions of a library (such
+  as `httpclient5` 5.6.4 and 5.6.5, or `slf4j-api` 1.7.36 and 2.0.20).
 * `camel update run --runtime=quarkus --download=false` without 
`--quarkusMavenPluginVersion` exits with `2`, and a
   version without upgrade recipes exits with `1`: both failed with an internal 
error (`70`).
 * `camel get error --last` shows the newest error of all the integrations (it 
showed the newest error of the integration
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
index 48b387e9dde2..2505529eee5c 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
@@ -233,7 +233,8 @@ Defaults to CycloneDX format (`sbom.json`) with 
`camel-main` runtime.
 
 === Copying dependency JARs
 
-Copy required JARs to a folder (defaults to `lib/`):
+Copy required JARs to a folder (defaults to `lib/`). The dependencies are 
resolved together, as one dependency graph,
+so the folder has one version of each library:
 
 [source,bash]
 ----
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 7c6aefc171be..5f50aa17f56e 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
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Stream;
@@ -29,7 +30,6 @@ import org.aesh.command.option.Option;
 import org.apache.camel.dsl.jbang.core.common.CliException;
 import org.apache.camel.dsl.jbang.core.common.CliExitCode;
 import org.apache.camel.tooling.maven.MavenArtifact;
-import org.apache.camel.tooling.maven.MavenDownloader;
 import org.apache.camel.tooling.maven.MavenGav;
 import org.apache.camel.tooling.maven.MavenResolutionException;
 
@@ -42,8 +42,6 @@ public class DependencyCopy extends DependencyList {
 
     private static final Set<String> EXCLUDED_GROUP_IDS = 
Set.of("org.fusesource.jansi", "org.apache.logging.log4j");
 
-    private MavenDownloader downloader;
-
     @Option(name = "output-directory", description = "Directory where 
dependencies should be copied", defaultValue = "lib")
     protected String outputDirectory;
 
@@ -52,6 +50,9 @@ public class DependencyCopy extends DependencyList {
     // the artifacts that could not be resolved or copied
     private int failures;
 
+    // the dependencies to copy
+    private final List<MavenGav> gavs = new ArrayList<>();
+
     public DependencyCopy(CamelJBangMain main) {
         super(main);
     }
@@ -94,23 +95,62 @@ public class DependencyCopy extends DependencyList {
 
     @Override
     protected void outputGav(MavenGav gav, int index, int total) {
-        try {
+        // the dependencies are resolved together, once all are known, as one 
dependency graph: resolved one at a time,
+        // each picks the versions of its own dependencies, and lib/ gets 
several versions of a library (CAMEL-26931)
+        gavs.add(gav);
+        if (index == total - 1) {
             createOutputDirectory();
-            List<MavenArtifact> artifacts = 
mavenResolver.downloader().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;
+            copy(resolve(gavs));
+        }
+    }
+
+    // the artifacts of the dependencies, resolved together; when they cannot 
be, the ones that can be
+    private List<MavenArtifact> resolve(List<MavenGav> dependencies) {
+        try {
+            return resolveTogether(dependencies);
+        } catch (MavenResolutionException e) {
+            // find the dependencies that cannot be resolved, which are 
printed on stderr, and resolve the others together
+            List<MavenGav> resolvable = new ArrayList<>();
+            for (MavenGav gav : dependencies) {
+                try {
+                    resolveTogether(List.of(gav));
+                    resolvable.add(gav);
+                } catch (MavenResolutionException ex) {
+                    failures++;
+                    printer().printErr("Error resolving the artifact: " + gav 
+ " due to: " + ex.getMessage());
                 }
+            }
+            if (resolvable.isEmpty()) {
+                return List.of();
+            }
+            try {
+                return resolveTogether(resolvable);
+            } catch (MavenResolutionException ex) {
+                failures++;
+                printer().printErr("Error resolving the artifacts: " + 
resolvable + " due to: " + ex.getMessage());
+                return List.of();
+            }
+        }
+    }
+
+    private List<MavenArtifact> resolveTogether(List<MavenGav> dependencies) 
throws MavenResolutionException {
+        List<String> list = 
dependencies.stream().map(MavenGav::toString).toList();
+        boolean snapshot = dependencies.stream().anyMatch(g -> g.getVersion() 
!= null && g.getVersion().contains("SNAPSHOT"));
+        return mavenResolver.downloader().resolveArtifacts(list, Set.of(), 
true, snapshot);
+    }
+
+    private void copy(List<MavenArtifact> artifacts) {
+        for (MavenArtifact artifact : artifacts) {
+            Path target = Paths.get(outputDirectory, 
artifact.getFile().getName());
+            if (Files.exists(target) || 
EXCLUDED_GROUP_IDS.contains(artifact.getGav().getGroupId())) {
+                continue;
+            }
+            try {
                 Files.copy(artifact.getFile().toPath(), target);
+            } catch (IOException e) {
+                failures++;
+                printer().printErr("Error copying the artifact: " + 
artifact.getGav() + " due to: " + e.getMessage());
             }
-        } catch (MavenResolutionException e) {
-            failures++;
-            printer().printErr("Error resolving the artifact: " + gav + " due 
to: " + e.getMessage());
-        } catch (IOException e) {
-            failures++;
-            printer().printErr("Error copying the artifact: " + gav + " due 
to: " + e.getMessage());
         }
     }
 
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopyTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopyTest.java
index b70d69896cc2..a01214243d33 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopyTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/DependencyCopyTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.dsl.jbang.core.commands;
 
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Stream;
 
@@ -37,8 +38,11 @@ import static 
org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyList;
 import static org.mockito.ArgumentMatchers.anySet;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
 
 /**
  * camel dependency copy: the JARs of the dependencies of the integration, 
copied to a directory. The dependencies are
@@ -70,6 +74,17 @@ class DependencyCopyTest extends CamelCommandBaseTestSupport 
{
         assertThat(jars()).hasSizeGreaterThan(1).allMatch(name -> 
name.endsWith("-4.11.0.jar"));
     }
 
+    @Test
+    void resolvesTheDependenciesTogether() throws Exception {
+        MavenDownloader downloader = resolver(null);
+
+        assertThat(command(downloader).doCall()).isZero();
+
+        // one dependency graph, so lib/ gets one version of each library 
(CAMEL-26931)
+        verify(downloader).resolveArtifacts(argThat(gavs -> gavs.size() > 1
+                && gavs.contains("org.apache.camel:camel-main:4.11.0")), 
anySet(), eq(true), eq(false));
+    }
+
     @Test
     void replacesTheJarsOfAnEarlierCopyAndKeepsTheOtherFiles() throws 
Exception {
         Files.writeString(lib.resolve("camel-main-4.10.0.jar"), "older");
@@ -92,6 +107,7 @@ class DependencyCopyTest extends CamelCommandBaseTestSupport 
{
                 .contains("Error resolving the artifact: 
org.apache.camel:camel-main:4.11.0 due to: not in the repository");
         // the other dependencies are copied
         
assertThat(jars()).isNotEmpty().doesNotContain("camel-main-4.11.0.jar");
+        assertThat(printer.getStderr()).containsOnlyOnce("Error resolving");
     }
 
     @Test
@@ -130,21 +146,25 @@ class DependencyCopyTest extends 
CamelCommandBaseTestSupport {
         return command;
     }
 
-    // resolves each dependency to a JAR of its own, except the one with the 
artifact id that is missing (the export
-    // before the copy resolves the rest as usual)
+    // resolves each dependency to a JAR of its own, and fails a request with 
the artifact id that is missing (the
+    // export before the copy resolves the rest as usual)
     private MavenDownloader resolver(String missing) throws Exception {
         MavenDownloader downloader = spy(new 
MavenResolverMixin().downloader());
         doAnswer(inv -> {
             List<String> gavs = inv.getArgument(0);
-            MavenGav gav = MavenGav.parseGav(gavs.get(0));
-            if (gav.getArtifactId().equals(missing)) {
-                throw new MavenResolutionException("not in the repository");
-            }
-            Path jar = repo.resolve(gav.getArtifactId() + "-" + 
gav.getVersion() + ".jar");
-            if (!Files.exists(jar)) {
-                Files.writeString(jar, gav.toString());
+            List<MavenArtifact> answer = new ArrayList<>();
+            for (String text : gavs) {
+                MavenGav gav = MavenGav.parseGav(text);
+                if (gav.getArtifactId().equals(missing)) {
+                    throw new MavenResolutionException("not in the 
repository");
+                }
+                Path jar = repo.resolve(gav.getArtifactId() + "-" + 
gav.getVersion() + ".jar");
+                if (!Files.exists(jar)) {
+                    Files.writeString(jar, gav.toString());
+                }
+                answer.add(new MavenArtifact(gav, jar.toFile()));
             }
-            return List.of(new MavenArtifact(gav, jar.toFile()));
+            return answer;
         }).when(downloader).resolveArtifacts(anyList(), anySet(), 
anyBoolean(), anyBoolean());
         return downloader;
     }

Reply via email to