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 43ddcb5a4fba CAMEL-27546: camel-jbang - let an export plugin remove 
dependencies (#30792)
43ddcb5a4fba is described below

commit 43ddcb5a4fba8a8dfe37b65b7f2494c4aa65ed2e
Author: Jiří Ondrušek <[email protected]>
AuthorDate: Wed Nov 25 21:10:12 2026 +0100

    CAMEL-27546: camel-jbang - let an export plugin remove dependencies (#30792)
    
    PluginExporter gets getDependenciesToRemove(RuntimeType), a no-op by
    default. The export removes the returned entries after all dependencies
    are collected (routes, --dep, plugins, runtime exporter) and before the
    project files are generated, matching by groupId:artifactId in any
    notation. A Camel component is named org.apache.camel:camel-x (or
    camel:x), not by its runtime artifact. An absent dependency is ignored,
    nothing is removed with --skip-plugins, and each removal is printed.
    
    The plugin exporters are looked up once per export.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../ExportPluginRemoveDependenciesTest.java        | 145 +++++++++++++++++++++
 .../camel/dsl/jbang/run/common/PluginExporter.java |  20 +++
 .../camel/dsl/jbang/run/export/Exporter.java       |  39 +++++-
 3 files changed, 202 insertions(+), 2 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportPluginRemoveDependenciesTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportPluginRemoveDependenciesTest.java
new file mode 100644
index 000000000000..a92f27a739f9
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ExportPluginRemoveDependenciesTest.java
@@ -0,0 +1,145 @@
+/*
+ * 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.nio.file.Path;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.camel.dsl.jbang.core.common.CommandLineTestHelper;
+import org.apache.camel.dsl.jbang.run.common.PluginExporter;
+import org.apache.camel.dsl.jbang.run.common.Printer;
+import org.apache.camel.dsl.jbang.run.common.RuntimeType;
+import org.apache.camel.dsl.jbang.run.export.ExportContext;
+import org.apache.maven.model.Model;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * An export plugin can remove dependencies: of the routes, of --dep and of 
another plugin (CAMEL-27546).
+ */
+class ExportPluginRemoveDependenciesTest extends ExportBaseTestSupport {
+
+    @Test
+    void pluginRemovesDependencyAddedByAnotherPlugin() throws Exception {
+        PluginExporter adder = new TestExporter() {
+            @Override
+            public Set<String> getDependencies(RuntimeType runtimeType) {
+                // mvn:g:a:v, camel:component and plain g:a (no version) 
notations
+                return Set.of("mvn:org.acme:acme-model:1.0.0", 
"mvn:org.acme:acme-keep:1.0.0",
+                        "camel:xslt", "camel:jsonpath", "org.acme:acme-plain");
+            }
+        };
+        PluginExporter remover = new TestExporter() {
+            @Override
+            public Set<String> getDependenciesToRemove(RuntimeType 
runtimeType) {
+                // the second one is not present, and is ignored
+                return Set.of("org.acme:acme-model", 
"org.apache.camel:camel-xslt", "org.acme:acme-plain",
+                        "org.acme:acme-absent");
+            }
+        };
+
+        Export command = new CamelJBangMain().command(new 
PluginsExport(List.of(adder, remover)));
+        CommandLineTestHelper.parse(command, "--gav=examples:route:1.0.0", 
"--dir=" + workingDir, "--quiet",
+                "--runtime=" + RuntimeType.quarkus.runtime(), 
CamelCommandBaseTestSupport.quarkusExtRegistry());
+        command.files = List.of("classpath:route.yaml");
+
+        assertThat(command.doCall()).isZero();
+
+        Model model = readMavenModel();
+        assertThat(model.getDependencies())
+                .extracting(d -> d.getGroupId() + ":" + d.getArtifactId())
+                .contains("org.acme:acme-keep", 
"org.apache.camel.quarkus:camel-quarkus-jsonpath",
+                        "org.apache.camel.quarkus:camel-quarkus-timer")
+                .doesNotContain("org.acme:acme-model", 
"org.apache.camel.quarkus:camel-quarkus-xslt",
+                        "org.acme:acme-plain", "org.acme:acme-absent");
+    }
+
+    @Test
+    void pluginRemovesDependencyOfTheRoutesAndOfDep() throws Exception {
+        // camel-timer comes from the route, acme-dep from --dep; neither is 
added by a plugin
+        PluginExporter remover = new TestExporter() {
+            @Override
+            public Set<String> getDependenciesToRemove(RuntimeType 
runtimeType) {
+                return Set.of("camel:timer", "org.acme:acme-dep");
+            }
+        };
+
+        Export command = new CamelJBangMain().command(new 
PluginsExport(List.of(remover)));
+        CommandLineTestHelper.parse(command, "--gav=examples:route:1.0.0", 
"--dir=" + workingDir, "--quiet",
+                "--dep=mvn:org.acme:acme-dep:1.0.0", "--runtime=" + 
RuntimeType.quarkus.runtime(),
+                CamelCommandBaseTestSupport.quarkusExtRegistry());
+        command.files = List.of("classpath:route.yaml");
+
+        assertThat(command.doCall()).isZero();
+
+        Model model = readMavenModel();
+        assertThat(model.getDependencies())
+                .extracting(d -> d.getGroupId() + ":" + d.getArtifactId())
+                .contains("org.apache.camel.quarkus:camel-quarkus-yaml-dsl")
+                
.doesNotContain("org.apache.camel.quarkus:camel-quarkus-timer", 
"org.acme:acme-dep");
+    }
+
+    @Test
+    void skipPluginsRemovesNothing() throws Exception {
+        AtomicBoolean called = new AtomicBoolean();
+        PluginExporter remover = new TestExporter() {
+            @Override
+            public Set<String> getDependenciesToRemove(RuntimeType 
runtimeType) {
+                called.set(true);
+                return Set.of("camel:timer", "org.acme:acme-dep");
+            }
+        };
+
+        Export command = new CamelJBangMain().command(new 
PluginsExport(List.of(remover)));
+        CommandLineTestHelper.parse(command, "--gav=examples:route:1.0.0", 
"--dir=" + workingDir, "--quiet",
+                "--skip-plugins", "--dep=mvn:org.acme:acme-dep:1.0.0", 
"--runtime=" + RuntimeType.quarkus.runtime(),
+                CamelCommandBaseTestSupport.quarkusExtRegistry());
+        command.files = List.of("classpath:route.yaml");
+
+        assertThat(command.doCall()).isZero();
+
+        assertThat(called).isFalse();
+        Model model = readMavenModel();
+        assertThat(model.getDependencies())
+                .extracting(d -> d.getGroupId() + ":" + d.getArtifactId())
+                .contains("org.apache.camel.quarkus:camel-quarkus-timer", 
"org.acme:acme-dep");
+    }
+
+    private static class PluginsExport extends Export {
+        private final List<PluginExporter> exporters;
+
+        PluginsExport(List<PluginExporter> exporters) {
+            this.exporters = exporters;
+        }
+
+        @Override
+        protected ExportContext exportContext() {
+            return super.exportContext()
+                    .withPlugins(repos -> new 
ExportContext.ActivePlugins(List.of(), exporters));
+        }
+    }
+
+    private abstract static class TestExporter implements PluginExporter {
+        @Override
+        public void addSourceFiles(Path buildDir, String packageName, Printer 
printer) {
+            // noop
+        }
+    }
+}
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PluginExporter.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PluginExporter.java
index 98e3c4e6759b..714a10fa000f 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PluginExporter.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PluginExporter.java
@@ -47,6 +47,26 @@ public interface PluginExporter {
         return Collections.emptySet();
     }
 
+    /**
+     * Provide dependencies to remove from the exported project. They are 
removed after the dependencies of the routes,
+     * of {@code --dep} and of all plugins are collected, and before the 
project files are generated.
+     * <p>
+     * An entry is matched by groupId:artifactId against the dependencies as 
collected, whatever notation ({@code mvn:},
+     * {@code camel:} or plain groupId:artifactId) and version either side 
uses. A Camel component is named
+     * {@code org.apache.camel:camel-x} (or {@code camel:x}), not by the 
artifact of the runtime: an entry such as
+     * {@code org.apache.camel.quarkus:camel-quarkus-x} or a Spring Boot 
{@code camel-x-starter} does not match. A
+     * dependency that is not present is ignored.
+     * <p>
+     * Known limit: with the camel-main runtime, the export can add {@code 
camel:health} after this removal (when it
+     * enables the embedded HTTP server), so that one cannot be removed.
+     *
+     * @param  runtimeType Type of runtime (defined by parameter --runtime)
+     * @return             set of groupId:artifactId, in any of the notations 
above
+     */
+    default Set<String> getDependenciesToRemove(RuntimeType runtimeType) {
+        return Collections.emptySet();
+    }
+
     /**
      * Checks if the plugin exporter should perform. Implementations may add 
explicit logic to determine if the plugin
      * exporter should be skipped.
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
index 2d1607b05aef..23811029f7fd 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/Exporter.java
@@ -94,6 +94,7 @@ abstract class Exporter {
     private static final Set<String> EXCLUDED_GROUP_IDS = 
Set.of("org.fusesource.jansi", "org.apache.logging.log4j");
 
     private Printer quietPrinter;
+    private Set<PluginExporter> pluginExporters;
     // Map to store deferred symbolic links: target path in the build dir → 
original source path
     private Map<Path, Path> deferredSymlinks = new HashMap<>();
 
@@ -309,10 +310,40 @@ abstract class Exporter {
      */
     protected final Set<String> resolveDependencies(Path settings, Path 
profile) throws Exception {
         Set<String> answer = doResolveDependencies(settings, profile);
+        removePluginDependencies(answer);
         removeDuplicateDependencies(answer);
         return answer;
     }
 
+    /**
+     * Removes the dependencies the plugins ask to remove, after all 
dependencies are collected (routes, --dep, plugins
+     * and the runtime exporter). Matched by groupId:artifactId on the 
coordinates as collected, in any notation, see
+     * {@link PluginExporter#getDependenciesToRemove(RuntimeType)}. The 
camel-main runtime can still add camel:health
+     * afterwards.
+     */
+    private void removePluginDependencies(Set<String> answer) {
+        if (request.skipPlugins) {
+            return;
+        }
+        Set<String> toBeRemoved = new HashSet<>();
+        for (PluginExporter exporter : pluginExporters()) {
+            for (String d : exporter.getDependenciesToRemove(request.runtime)) 
{
+                MavenGav gav = MavenGav.parseGav(d);
+                toBeRemoved.add(gav.getGroupId() + ":" + gav.getArtifactId());
+            }
+        }
+        if (!toBeRemoved.isEmpty()) {
+            answer.removeIf(d -> {
+                MavenGav gav = MavenGav.parseGav(d);
+                boolean remove = toBeRemoved.contains(gav.getGroupId() + ":" + 
gav.getArtifactId());
+                if (remove) {
+                    printer().printf("Removing dependency %s (asked by an 
export plugin)%n", d);
+                }
+                return remove;
+            });
+        }
+    }
+
     /**
      * Collects the dependencies of the exported project. A runtime exporter 
adds its own dependencies by overriding
      * this method and calling super first. The duplicates are removed 
afterwards by
@@ -1509,10 +1540,14 @@ abstract class Exporter {
     }
 
     /**
-     * The exporters of the active plugins.
+     * The exporters of the active plugins, looked up once per export (a 
plugin that cannot be linked is warned about
+     * once).
      */
     protected Set<PluginExporter> pluginExporters() {
-        return new 
LinkedHashSet<>(exportContext.getPlugins(request.mavenResolver.repos()).exporters());
+        if (pluginExporters == null) {
+            pluginExporters = new 
LinkedHashSet<>(exportContext.getPlugins(request.mavenResolver.repos()).exporters());
+        }
+        return pluginExporters;
     }
 
     protected Printer outPrinter() {

Reply via email to