This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch backport/CAMEL-24483-4.22.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 18ffb93ef133e6277ce15cf288dd055242e2a4d9 Author: Claus Ibsen <[email protected]> AuthorDate: Tue Aug 25 15:17:43 2026 +0200 CAMEL-24483: camel-jbang export - explicit --dep version overrides auto-detected dependency An explicit --dep for a groupId:artifactId now takes precedence over an auto-detected dependency version (e.g. a JDBC driver whose version is inferred from the camel-dependencies BOM). Previously the export dedup kept the first entry by TreeSet sort order and ignored the version, so the auto-detected mvn: entry always won and the user's --dep version was silently dropped. Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> (cherry picked from commit 78eac2a33b10f07f8ee754af6f46d3486c32fea9) --- .../dsl/jbang/core/commands/ExportBaseCommand.java | 23 +++++++++++++++---- .../camel/dsl/jbang/core/commands/ExportTest.java | 26 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java index 272df1fbe6b2..82fa09b3ed5a 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java @@ -811,14 +811,29 @@ public abstract class ExportBaseCommand extends CamelCommand { answer.add("mvn:org.hibernate.orm:hibernate-core"); } - // remove duplicate versions (keep first) - Map<String, String> versions = new HashMap<>(); + // remove duplicate versions (keep first) but an explicit --dep version always wins over + // an auto-detected dependency for the same groupId:artifactId (e.g. a JDBC driver whose + // version is inferred from the camel-dependencies BOM) + Set<String> preferred = new HashSet<>(); + for (String d : dependencies) { + String line = normalizeDependency(d); + MavenGav gav = MavenGav.parseGav(line); + if (gav.getVersion() != null && !gav.getVersion().isBlank()) { + preferred.add(line); + } + } + Map<String, String> kept = new HashMap<>(); Set<String> toBeRemoved = new HashSet<>(); for (String line : answer) { MavenGav gav = MavenGav.parseGav(line); String ga = gav.getGroupId() + ":" + gav.getArtifactId(); - if (!versions.containsKey(ga)) { - versions.put(ga, gav.getVersion()); + String existing = kept.get(ga); + if (existing == null) { + kept.put(ga, line); + } else if (preferred.contains(line) && !preferred.contains(existing)) { + // the user-supplied --dep version takes precedence over the auto-detected one + toBeRemoved.add(existing); + kept.put(ga, line); } else { toBeRemoved.add(line); } diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java index d26c54931dff..45b4eca08aac 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java +++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportTest.java @@ -1130,6 +1130,32 @@ class ExportTest { } } + @Test + void shouldOverrideAutoDetectedDriverVersion() throws Exception { + LOG.info("shouldOverrideAutoDetectedDriverVersion"); + // the bean uses driverClassName org.postgresql.Driver which Camel auto-detects and adds + // org.postgresql:postgresql with the version from the camel-dependencies BOM. An explicit + // --dep for the same groupId:artifactId must override that auto-detected version. + Export command = new Export(new CamelJBangMain()); + CommandLine.populateCommand(command, + "--gav=examples:route:1.0.0", "--dir=" + workingDir, "--quiet", + "--runtime=camel-main", + "--dep=org.postgresql:postgresql:42.7.99", + "src/test/resources/k8s-secret-bean.yaml"); + int exit = command.doCall(); + + assertThat(exit).isZero(); + Model model = readMavenModel(); + + List<Dependency> pg = model.getDependencies().stream() + .filter(d -> "org.postgresql".equals(d.getGroupId()) && "postgresql".equals(d.getArtifactId())) + .toList(); + assertThat(pg) + .as("Explicit --dep version must override the auto-detected postgresql driver version") + .singleElement() + .satisfies(d -> assertThat(d.getVersion()).isEqualTo("42.7.99")); + } + @ParameterizedTest @MethodSource("runtimeProvider") public void shouldExportWithCustomRepos(RuntimeType rt) throws Exception {
