This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.22.x by this push:
new 4ebf332cd20b [BACKPORT-4.22.x] CAMEL-26766: Support custom plugin
configuration path (#30431)
4ebf332cd20b is described below
commit 4ebf332cd20b0ae737dedecaecf228ff8134a2ad
Author: Christoph Deppisch <[email protected]>
AuthorDate: Thu Nov 19 18:56:17 2026 +0100
[BACKPORT-4.22.x] CAMEL-26766: Support custom plugin configuration path
(#30431)
Add support for loading the Camel JBang plugin configuration from a
custom location. The plugin config path is resolved in this order:
1. Explicit Path argument passed to PluginHelper methods
2. CAMEL_CLI_PLUGIN_CONFIG environment variable
3. Default ~/.camel-jbang-plugins.json
The path can point to a JSON file or a directory (in which case the
default filename is resolved inside it).
Plugin commands (add, get, list, delete) expose this via the
--plugin-config CLI option.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
---
.../modules/ROOT/pages/camel-jbang-projects.adoc | 28 ++++
.../jbang-commands/camel-jbang-plugin-add.adoc | 1 +
.../jbang-commands/camel-jbang-plugin-delete.adoc | 1 +
.../jbang-commands/camel-jbang-plugin-get.adoc | 1 +
.../jbang-commands/camel-jbang-plugin-list.adoc | 1 +
.../META-INF/camel-jbang-commands-metadata.json | 2 +-
.../core/commands/plugin/PluginBaseCommand.java | 38 ++++-
.../jbang/core/commands/plugin/PluginCommand.java | 3 +-
.../camel/dsl/jbang/core/common/PluginHelper.java | 87 ++++++++++--
.../jbang/core/commands/plugin/PluginGetTest.java | 61 ++++++++
.../dsl/jbang/core/common/PluginHelperTest.java | 155 +++++++++++++++++++++
11 files changed, 364 insertions(+), 14 deletions(-)
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 d791c714c823..7b395d1d770c 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc
@@ -238,6 +238,34 @@ camel plugin get
camel plugin delete <plugin-name>
----
+=== Custom plugin configuration
+
+By default, the CLI stores the plugin configuration in
`~/.camel-jbang-plugins.json`.
+To use a different location, pass the `--plugin-config` option to any `plugin`
subcommand:
+
+[source,bash]
+----
+camel plugin get --plugin-config /path/to/my-plugins.json
+camel plugin add kubernetes --plugin-config /path/to/my-plugins.json
+----
+
+The path can point to a JSON file, an existing file with any name, or a
directory (in which case the default filename `.camel-jbang-plugins.json` is
resolved inside it).
+A path that does not exist and does not end with `.json` is treated as a
directory and created on first save.
+
+Alternatively, set the `CAMEL_CLI_PLUGIN_CONFIG` environment variable:
+
+[source,bash]
+----
+export CAMEL_CLI_PLUGIN_CONFIG=/path/to/my-plugins.json
+camel plugin get
+----
+
+The resolution order is: `--plugin-config` option > `CAMEL_CLI_PLUGIN_CONFIG`
environment variable > default `~/.camel-jbang-plugins.json`.
+
+NOTE: The `--plugin-config` option only applies to the `plugin` subcommands
(`add`, `get`, `delete`, `list`).
+Running a plugin's own command (e.g. `camel kubernetes ...`) discovers plugins
only through the environment variable or the default file.
+So a plugin added with `camel plugin add --plugin-config X` is not seen unless
`CAMEL_CLI_PLUGIN_CONFIG` is also set.
+
=== Plugin versions
The plugins of the Apache Camel project always use the version of the CLI.
diff --git
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-add.adoc
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-add.adoc
index 3f6cfc53d295..687ee867303c 100644
---
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-add.adoc
+++
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-add.adoc
@@ -25,6 +25,7 @@ camel plugin add [options]
| `--first-version` | First version of this plugin | | String
| `--gav` | Maven group and artifact coordinates. | | String
| `--groupId` | Maven groupId | org.apache.camel | String
+| `--plugin-config` | Path to a custom plugin configuration file or directory.
Only applies to the plugin subcommands; for plugin commands themselves (e.g.
'camel kubernetes'), set CAMEL_CLI_PLUGIN_CONFIG instead. | | String
| `--repo,--repos` | Additional maven repositories to use for downloading the
plugin (Use commas to separate multiple repositories) | | String
| `--version` | Maven artifact version | | String
| `-h,--help` | Display the help and sub-commands | | boolean
diff --git
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-delete.adoc
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-delete.adoc
index 31ebeb92b66c..8d2b0957acf6 100644
---
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-delete.adoc
+++
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-delete.adoc
@@ -19,6 +19,7 @@ camel plugin delete [options]
[cols="2,5,1,2",options="header"]
|===
| Option | Description | Default | Type
+| `--plugin-config` | Path to a custom plugin configuration file or directory.
Only applies to the plugin subcommands; for plugin commands themselves (e.g.
'camel kubernetes'), set CAMEL_CLI_PLUGIN_CONFIG instead. | | String
| `-h,--help` | Display the help and sub-commands | | boolean
|===
diff --git
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-get.adoc
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-get.adoc
index a664283a3f47..d61b28870cab 100644
---
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-get.adoc
+++
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-get.adoc
@@ -19,6 +19,7 @@ camel plugin get [options]
[cols="2,5,1,2",options="header"]
|===
| Option | Description | Default | Type
+| `--plugin-config` | Path to a custom plugin configuration file or directory.
Only applies to the plugin subcommands; for plugin commands themselves (e.g.
'camel kubernetes'), set CAMEL_CLI_PLUGIN_CONFIG instead. | | String
| `--repos` | Display maven repository column | false | boolean
| `-h,--help` | Display the help and sub-commands | | boolean
|===
diff --git
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-list.adoc
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-list.adoc
index a98fbb6a8423..1cdd9a966448 100644
---
a/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-list.adoc
+++
b/docs/user-manual/modules/ROOT/pages/jbang-commands/camel-jbang-plugin-list.adoc
@@ -19,6 +19,7 @@ camel plugin list [options]
[cols="2,5,1,2",options="header"]
|===
| Option | Description | Default | Type
+| `--plugin-config` | Path to a custom plugin configuration file or directory.
Only applies to the plugin subcommands; for plugin commands themselves (e.g.
'camel kubernetes'), set CAMEL_CLI_PLUGIN_CONFIG instead. | | String
| `--repos` | Display maven repository column | false | boolean
| `-h,--help` | Display the help and sub-commands | | boolean
|===
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
b/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
index e155bf6a47e2..9052df77673f 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
+++
b/dsl/camel-jbang/camel-jbang-core/src/generated/resources/META-INF/camel-jbang-commands-metadata.json
@@ -23,7 +23,7 @@
{ "name": "jolokia", "fullName": "jolokia", "description": "Attach Jolokia
JVM Agent to a running Camel integration", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.process.Jolokia", "options": [ {
"names": "--port", "description": "To use a specific port number when attaching
Jolokia JVM Agent (default a free port is found in range 8778-9999)",
"javaType": "int", "type": "integer" }, { "names": "--stop", "description":
"Stops the Jolokia JVM Agent in the running Camel inte [...]
{ "name": "log", "fullName": "log", "description": "Tail logs from running
Camel integrations", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.action.CamelLogAction", "options": [
{ "names": "--find", "description": "Find and highlight matching text (ignore
case).", "javaType": "java.lang.String", "type": "string" }, { "names":
"--follow", "description": "Keep following and outputting new log lines (press
enter to exit).", "defaultValue": "true", "javaType": "boolean", "typ [...]
{ "name": "nano", "fullName": "nano", "description": "Nano editor to edit
file", "sourceClass": "org.apache.camel.dsl.jbang.core.commands.Nano",
"options": [ { "names": "-h,--help", "description": "Display the help and
sub-commands", "javaType": "boolean", "type": "boolean" } ] },
- { "name": "plugin", "fullName": "plugin", "description": "Manage plugins
that add sub-commands to this CLI", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.plugin.PluginCommand", "options": [ {
"names": "-h,--help", "description": "Display the help and sub-commands",
"javaType": "boolean", "type": "boolean" } ], "subcommands": [ { "name": "add",
"fullName": "plugin add", "description": "Add new plugin", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.plugin.PluginA [...]
+ { "name": "plugin", "fullName": "plugin", "description": "Manage plugins
that add sub-commands to this CLI", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.plugin.PluginCommand", "options": [ {
"names": "-h,--help", "description": "Display the help and sub-commands",
"javaType": "boolean", "type": "boolean" } ], "subcommands": [ { "name": "add",
"fullName": "plugin add", "description": "Add new plugin", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.plugin.PluginA [...]
{ "name": "ps", "fullName": "ps", "description": "List running Camel
integrations", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.process.ListProcess", "options": [ {
"names": "--json", "description": "Output in JSON Format", "javaType":
"boolean", "type": "boolean" }, { "names": "--pid", "description": "List only
pid in the output", "javaType": "boolean", "type": "boolean" }, { "names":
"--remote", "description": "Break down counters into remote\/total pairs",
"javaType": [...]
{ "name": "restart", "fullName": "restart", "description": "Restarts
running Camel integrations (stop + re-launch)", "sourceClass":
"org.apache.camel.dsl.jbang.core.commands.process.RestartProcess", "options": [
{ "names": "-h,--help", "description": "Display the help and sub-commands",
"javaType": "boolean", "type": "boolean" } ] },
{ "name": "run", "fullName": "run", "description": "Run as local Camel
integration", "sourceClass": "org.apache.camel.dsl.jbang.core.commands.Run",
"options": [ { "names": "--background", "description": "Run in the background",
"defaultValue": "false", "javaType": "boolean", "type": "boolean" }, { "names":
"--background-wait", "description": "To wait for run in background to startup
successfully, before returning", "defaultValue": "true", "javaType": "boolean",
"type": "boolean" }, { [...]
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginBaseCommand.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginBaseCommand.java
index ecdb433042d2..41369ea4e64b 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginBaseCommand.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginBaseCommand.java
@@ -17,26 +17,58 @@
package org.apache.camel.dsl.jbang.core.commands.plugin;
+import java.nio.file.InvalidPathException;
+import java.nio.file.Path;
+
import org.apache.camel.dsl.jbang.core.commands.CamelCommand;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.PluginHelper;
import org.apache.camel.util.json.JsonObject;
+import picocli.CommandLine;
/**
- * Base command supports Kubernetes client related options such as namespace
or custom kube config option. Automatically
- * applies the options to the Kubernetes client instance that is being used to
run commands.
+ * Base command for plugin subcommands. Provides the {@code --plugin-config}
option that overrides the default plugin
+ * configuration file location.
*/
abstract class PluginBaseCommand extends CamelCommand {
+ @CommandLine.Option(names = { "--plugin-config" },
+ description = "Path to a custom plugin configuration
file or directory. "
+ + "Only applies to the plugin
subcommands; "
+ + "for plugin commands themselves (e.g.
'camel kubernetes'), "
+ + "set CAMEL_CLI_PLUGIN_CONFIG instead.")
+ String pluginConfig;
+
protected PluginBaseCommand(CamelJBangMain main) {
super(main);
}
+ private Path pluginConfigPath() {
+ if (pluginConfig != null) {
+ try {
+ return Path.of(pluginConfig);
+ } catch (InvalidPathException e) {
+ throw new IllegalStateException(
+ "Invalid path in --plugin-config option: " +
pluginConfig, e);
+ }
+ }
+ return null;
+ }
+
JsonObject loadConfig() {
+ Path path = pluginConfigPath();
+ if (path != null) {
+ return PluginHelper.getOrCreatePluginConfig(path);
+ }
return PluginHelper.getOrCreatePluginConfig();
}
void saveConfig(JsonObject plugins) {
- PluginHelper.savePluginConfig(plugins);
+ Path path = pluginConfigPath();
+ if (path != null) {
+ PluginHelper.savePluginConfig(plugins, path);
+ } else {
+ PluginHelper.savePluginConfig(plugins);
+ }
}
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginCommand.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginCommand.java
index 3a1129d128ac..360eec294526 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginCommand.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginCommand.java
@@ -16,12 +16,13 @@
*/
package org.apache.camel.dsl.jbang.core.commands.plugin;
+import org.apache.camel.dsl.jbang.core.commands.CamelCommand;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import picocli.CommandLine;
@CommandLine.Command(name = "plugin",
description = "Manage plugins that add sub-commands to
this CLI")
-public class PluginCommand extends PluginBaseCommand {
+public class PluginCommand extends CamelCommand {
public PluginCommand(CamelJBangMain main) {
super(main);
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
index 0f69270f631c..c4fd0a54b96d 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
@@ -22,6 +22,7 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
@@ -29,6 +30,7 @@ import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@@ -64,6 +66,8 @@ import picocli.CommandLine;
public final class PluginHelper {
public static final String PLUGIN_CONFIG = ".camel-jbang-plugins.json";
+ public static final String PLUGIN_CONFIG_ENV = "CAMEL_CLI_PLUGIN_CONFIG";
+
public static final String PLUGIN_SERVICE_DIR =
"META-INF/services/org/apache/camel/camel-jbang-plugin/";
/**
@@ -83,6 +87,51 @@ public final class PluginHelper {
// prevent instantiation of utility class
}
+ public static Path getCustomPluginConfigPath() {
+ return getCustomPluginConfigPath(null);
+ }
+
+ /**
+ * Resolves the path to the plugin configuration file. Checks, in order:
+ * <ol>
+ * <li>A path set programmatically via method argument</li>
+ * <li>The {@value #PLUGIN_CONFIG_ENV} environment variable</li>
+ * <li>The default {@value #PLUGIN_CONFIG} in the user home directory</li>
+ * </ol>
+ */
+ public static Path getCustomPluginConfigPath(Path configPath) {
+ return resolvePluginConfigPath(configPath,
System.getenv(PLUGIN_CONFIG_ENV));
+ }
+
+ static Path resolvePluginConfigPath(Path configPath, String envValue) {
+ Path pluginConfigPath;
+ if (configPath != null) {
+ pluginConfigPath = configPath;
+ } else {
+ if (envValue != null && !envValue.isBlank()) {
+ try {
+ pluginConfigPath = Path.of(envValue);
+ } catch (InvalidPathException e) {
+ throw new IllegalStateException(
+ "Invalid path in " + PLUGIN_CONFIG_ENV + "
environment variable: " + envValue, e);
+ }
+ } else {
+ return CommandLineHelper.getHomeDir().resolve(PLUGIN_CONFIG);
+ }
+ }
+
+ if (Files.isDirectory(pluginConfigPath)) {
+ return pluginConfigPath.resolve(PLUGIN_CONFIG);
+ }
+ Path fileName = pluginConfigPath.getFileName();
+ if (Files.isRegularFile(pluginConfigPath)
+ || (fileName != null &&
fileName.toString().toLowerCase(Locale.ROOT).endsWith(".json"))) {
+ return pluginConfigPath;
+ }
+ // does not exist and has no .json suffix — treated as a directory
(created on first save)
+ return pluginConfigPath.resolve(PLUGIN_CONFIG);
+ }
+
/**
* Decides whether plugin discovery (classpath scan + JSON config + Maven
resolution) is needed for the current
* invocation. Returns false when the target command is a built-in that
does not consume plugins, skipping all
@@ -579,9 +628,17 @@ public final class PluginHelper {
return
Optional.ofNullable(getPluginConfig()).orElseGet(PluginHelper::createPluginConfig);
}
+ public static JsonObject getOrCreatePluginConfig(Path configPath) {
+ return Optional.ofNullable(getPluginConfig(configPath)).orElseGet(()
-> PluginHelper.createPluginConfig(configPath));
+ }
+
public static JsonObject getPluginConfig() {
+ return getPluginConfig(null);
+ }
+
+ public static JsonObject getPluginConfig(Path configPath) {
try {
- Path f = CommandLineHelper.getHomeDir().resolve(PLUGIN_CONFIG);
+ Path f = getCustomPluginConfigPath(configPath);
if (Files.exists(f)) {
String text = Files.readString(f);
return (JsonObject) Jsoner.deserialize(text);
@@ -594,9 +651,17 @@ public final class PluginHelper {
}
public static JsonObject createPluginConfig() {
- Path f = CommandLineHelper.getHomeDir().resolve(PLUGIN_CONFIG);
+ return createPluginConfig(null);
+ }
+
+ public static JsonObject createPluginConfig(Path configPath) {
+ Path f = getCustomPluginConfigPath(configPath);
JsonObject config = Jsoner.deserialize("{ \"plugins\": {} }", new
JsonObject());
try {
+ Path parent = f.getParent();
+ if (parent != null) {
+ Files.createDirectories(parent);
+ }
Files.writeString(f, config.toJson(),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
@@ -609,7 +674,11 @@ public final class PluginHelper {
}
public static void savePluginConfig(JsonObject plugins) {
- Path f = CommandLineHelper.getHomeDir().resolve(PLUGIN_CONFIG);
+ savePluginConfig(plugins, null);
+ }
+
+ public static void savePluginConfig(JsonObject plugins, Path configPath) {
+ Path f = getCustomPluginConfigPath(configPath);
try {
Files.writeString(f, plugins.toJson(),
StandardOpenOption.CREATE,
@@ -655,12 +724,12 @@ public final class PluginHelper {
JsonObject pluginConfig = PluginHelper.getOrCreatePluginConfig();
JsonObject plugins = pluginConfig.getMap("plugins");
- JsonObject kubePlugin = new JsonObject();
- kubePlugin.put("name", pluginType.getName());
- kubePlugin.put("command", pluginType.getCommand());
- kubePlugin.put("description", pluginType.getDescription());
- kubePlugin.put("firstVersion", pluginType.getFirstVersion());
- plugins.put(pluginType.getName(), kubePlugin);
+ JsonObject newPlugin = new JsonObject();
+ newPlugin.put("name", pluginType.getName());
+ newPlugin.put("command", pluginType.getCommand());
+ newPlugin.put("description", pluginType.getDescription());
+ newPlugin.put("firstVersion", pluginType.getFirstVersion());
+ plugins.putIfAbsent(pluginType.getName(), newPlugin);
PluginHelper.savePluginConfig(pluginConfig);
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginGetTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginGetTest.java
index d3e6f3a3de4d..6f84e9b9c8c1 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginGetTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/plugin/PluginGetTest.java
@@ -16,6 +16,9 @@
*/
package org.apache.camel.dsl.jbang.core.commands.plugin;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelCommandBaseTestSupport;
@@ -27,9 +30,13 @@ import org.apache.camel.util.json.JsonObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
class PluginGetTest extends CamelCommandBaseTestSupport {
+ @TempDir
+ Path customConfigDir;
+
@BeforeEach
public void setup() throws Exception {
super.setup();
@@ -64,6 +71,60 @@ class PluginGetTest extends CamelCommandBaseTestSupport {
output.get(3));
}
+ @Test
+ public void shouldUseCustomPluginConfigFile() throws Exception {
+ Path customConfig = customConfigDir.resolve("custom-plugins.json");
+ String configContent = """
+ {
+ "plugins": {
+ "custom-plugin": {
+ "name": "custom-plugin",
+ "command": "custom",
+ "description": "Custom plugin from custom config"
+ }
+ }
+ }
+ """;
+ Files.writeString(customConfig, configContent,
StandardOpenOption.CREATE);
+
+ PluginGet command = new PluginGet(new
CamelJBangMain().withPrinter(printer));
+ command.pluginConfig = customConfig.toString();
+ command.doCall();
+
+ List<String> output = printer.getLines();
+ Assertions.assertEquals(4, output.size());
+ Assertions.assertTrue(output.get(3).contains("custom-plugin"));
+ Assertions.assertTrue(output.get(3).contains("Custom plugin from
custom config"));
+ }
+
+ @Test
+ public void shouldUseCustomPluginConfigDirectory() throws Exception {
+ Path configDir = customConfigDir.resolve("config-dir");
+ Files.createDirectories(configDir);
+ Path configFile = configDir.resolve(PluginHelper.PLUGIN_CONFIG);
+ String configContent = """
+ {
+ "plugins": {
+ "dir-plugin": {
+ "name": "dir-plugin",
+ "command": "dir",
+ "description": "Plugin from directory config"
+ }
+ }
+ }
+ """;
+ Files.writeString(configFile, configContent,
StandardOpenOption.CREATE);
+
+ PluginGet command = new PluginGet(new
CamelJBangMain().withPrinter(printer));
+ command.pluginConfig = configDir.toString();
+ command.doCall();
+
+ List<String> output = printer.getLines();
+ Assertions.assertEquals(4, output.size());
+ Assertions.assertTrue(output.get(3).contains("dir-plugin"));
+ Assertions.assertTrue(output.get(3).contains("Plugin from directory
config"));
+ }
+
@Test
public void shouldGenerateDependencyAndDescription() throws Exception {
JsonObject pluginConfig = PluginHelper.getOrCreatePluginConfig();
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
index 969a0fa687c1..2b91b851aa10 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginHelperTest.java
@@ -33,6 +33,7 @@ import static
org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -357,6 +358,160 @@ public class PluginHelperTest {
assertEquals("", printer.getOutput());
}
+ @Test
+ public void testDefaultPluginConfigPath() {
+ Path expected =
CommandLineHelper.getHomeDir().resolve(PluginHelper.PLUGIN_CONFIG);
+ assertEquals(expected, PluginHelper.resolvePluginConfigPath(null,
null));
+ }
+
+ @Test
+ public void testCustomPluginConfigPath() {
+ Path custom = tempDir.resolve("custom-plugins.json");
+ assertEquals(custom, PluginHelper.getCustomPluginConfigPath(custom));
+ }
+
+ @Test
+ public void testCustomPluginConfigPathAsDirectory() throws Exception {
+ Path customDir = tempDir.resolve("custom-config-dir");
+ Files.createDirectories(customDir);
+ assertEquals(customDir.resolve(PluginHelper.PLUGIN_CONFIG),
PluginHelper.getCustomPluginConfigPath(customDir));
+ }
+
+ @Test
+ public void testCustomPluginConfigPathUsedByGetPluginConfig() throws
Exception {
+ Path customConfig = tempDir.resolve("custom-plugins.json");
+ String configContent = """
+ {
+ "plugins": {
+ "custom-plugin": {
+ "name": "custom-plugin",
+ "command": "custom",
+ "description": "Custom plugin"
+ }
+ }
+ }
+ """;
+ Files.writeString(customConfig, configContent,
StandardOpenOption.CREATE);
+
+ JsonObject config = PluginHelper.getPluginConfig(customConfig);
+ assertNotNull(config);
+ JsonObject plugins = config.getMap("plugins");
+ assertNotNull(plugins);
+ assertNotNull(plugins.getMap("custom-plugin"));
+ }
+
+ @Test
+ public void testCustomPluginConfigPathUsedByCreatePluginConfig() {
+ Path customConfig =
tempDir.resolve("sub").resolve("custom-plugins.json");
+
+ JsonObject config = PluginHelper.createPluginConfig(customConfig);
+ assertNotNull(config);
+ assertTrue(Files.exists(customConfig));
+ }
+
+ @Test
+ public void testCustomPluginConfigPathUsedBySavePluginConfig() throws
Exception {
+ Path customConfig = tempDir.resolve("save-plugins.json");
+
+ PluginHelper.createPluginConfig(customConfig);
+
+ JsonObject config = PluginHelper.getPluginConfig(customConfig);
+ assertNotNull(config);
+ JsonObject plugins = config.getMap("plugins");
+ plugins.put("saved-plugin", new JsonObject());
+
+ PluginHelper.savePluginConfig(config, customConfig);
+
+ String saved = Files.readString(customConfig);
+ assertTrue(saved.contains("saved-plugin"));
+ }
+
+ @Test
+ public void testCustomPathIsolatesFromDefaultConfig() throws Exception {
+ Path defaultConfig =
CommandLineHelper.getHomeDir().resolve(PluginHelper.PLUGIN_CONFIG);
+ String defaultContent = """
+ {
+ "plugins": {
+ "default-plugin": {
+ "name": "default-plugin",
+ "command": "default",
+ "description": "Default plugin"
+ }
+ }
+ }
+ """;
+ Files.writeString(defaultConfig, defaultContent,
StandardOpenOption.CREATE);
+
+ Path customConfig = tempDir.resolve("isolated-plugins.json");
+ String customContent = """
+ {
+ "plugins": {
+ "isolated-plugin": {
+ "name": "isolated-plugin",
+ "command": "isolated",
+ "description": "Isolated plugin"
+ }
+ }
+ }
+ """;
+ Files.writeString(customConfig, customContent,
StandardOpenOption.CREATE);
+
+ JsonObject config = PluginHelper.getPluginConfig(customConfig);
+ assertNotNull(config);
+ JsonObject plugins = config.getMap("plugins");
+ assertNotNull(plugins.getMap("isolated-plugin"));
+ assertNull(plugins.getMap("default-plugin"));
+ }
+
+ @Test
+ public void testEnvVarPluginConfigPath() {
+ Path result = PluginHelper.resolvePluginConfigPath(null,
"/tmp/camel-test-plugins.json");
+ assertEquals(Path.of("/tmp/camel-test-plugins.json"), result);
+ }
+
+ @Test
+ public void testEnvVarPluginConfigPathAsDirectory() throws Exception {
+ Path customDir = tempDir.resolve("env-config-dir");
+ Files.createDirectories(customDir);
+ Path result = PluginHelper.resolvePluginConfigPath(null,
customDir.toString());
+ assertEquals(customDir.resolve(PluginHelper.PLUGIN_CONFIG), result);
+ }
+
+ @Test
+ public void testExplicitPathTakesPrecedenceOverEnvVar() {
+ Path explicit = tempDir.resolve("explicit-plugins.json");
+ Path result = PluginHelper.resolvePluginConfigPath(explicit,
"/tmp/env-plugins.json");
+ assertEquals(explicit, result);
+ }
+
+ @Test
+ public void testInvalidEnvVarPathThrowsIllegalState() {
+ assertThrows(IllegalStateException.class,
+ () -> PluginHelper.resolvePluginConfigPath(null,
"\u0000invalid"));
+ }
+
+ @Test
+ public void testExistingNonJsonFileUsedAsConfig() throws Exception {
+ Path configFile = tempDir.resolve("plugins.conf");
+ Files.writeString(configFile, "{}", StandardOpenOption.CREATE);
+ Path result = PluginHelper.resolvePluginConfigPath(configFile, null);
+ assertEquals(configFile, result);
+ }
+
+ @Test
+ public void testUpperCaseJsonSuffixTreatedAsFile() {
+ Path configFile = tempDir.resolve("PLUGINS.JSON");
+ Path result = PluginHelper.resolvePluginConfigPath(configFile, null);
+ assertEquals(configFile, result);
+ }
+
+ @Test
+ public void testNonExistingNonJsonPathTreatedAsDirectory() {
+ Path dirPath = tempDir.resolve("custom-config-dir");
+ Path result = PluginHelper.resolvePluginConfigPath(dirPath, null);
+ assertEquals(dirPath.resolve(PluginHelper.PLUGIN_CONFIG), result);
+ }
+
private void writeConfig(JsonObject pluginEntry) throws Exception {
JsonObject plugins = new JsonObject();
plugins.put(pluginEntry.getString("name"), pluginEntry);