Dependencies and Component Resolution
Camel K tries to resolve automatically a wide range of dependencies that are required to run your integration code.
For example, take the following integration:
from("imap://admin@myserver.com")
.to("seda:output")
Since the integration has a endpoint starting with the "imap:" prefix, Camel K is able to automatically add the "camel-mail" component to the list of required dependencies. The seda:
endpoint belongs to camel-core
that is automatically added to all integrations, so Camel K will not add additional dependencies for it. This dependency resolution mechanism is transparent to the user, that will just see the integration running.
Automatic resolution is also a nice feature in dev mode, because you are allowed to add all components you need without exiting the dev loop.
Camel K won’t be able to resolve automatically the dependencies when your routes specify dynamic URIs. |
Import path dependencies
While developing your route, you will start including references to Camel dependencies via Java (or any other DSL) import mechanism. Camel K is able to scan and detect such dependencies as well. Take the following example:
import org.apache.camel.builder.RouteBuilder;
...
import org.apache.camel.component.kafka.KafkaComponent;
public class Test extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:java?period={{time:1000}}").
...
}
}
Camel K will be able to include camel-kafka
dependency as it discover scanning the import paths. No need to specify it in such circumstances.
Add explicit dependencies
You can explicitly add dependency using the -d
flag (short name of the flag --dependency
) of the kamel run
command. This is useful when you need to use dependencies that are not included in the Camel catalog or when the URI of your routes cannot be automatically discovered (see Dynamic URIs). For example:
kamel run -d mvn:com.google.guava:guava:26.0-jre -d camel:http Integration.java
With that command you will add a dependency of Guava and the Camel HTTP component. This feature can also be disabled if needed (although we discourage you from doing it) by disabling the dependencies trait (-t dependencies.enabled=false
).
Kind of dependencies
The -d
flag of the kamel run
command is flexible and support multiple kind of dependencies.
Camel dependencies can be added directly using the -d
flag like this:
kamel run -d camel:http Integration.java
In this case, the dependency will be added with the correct version. Note that the standard notation for specifying a Camel dependency is camel:xxx
, while kamel
also accepts camel-xxx
for usability.
While resolving Camel dependencies (camel:xxx
or camel-xxx
) the Camel K operator tries to find the dependency in the Camel catalog. In case the dependency is not listed in the catalog for some reason you will be provided with am error. Please make sure to use Camel dependencies listed in the catalog as these components are eligible to being used in Camel K (e.g. due to proper version resolving and runtime optimization). Using Camel dependencies not listed in the catalog may lead to unexpected behavior and is not supported. In case you do have a custom Camel component that you want to use as part of an Integration you can add this as an external Maven dependency using the respective Maven coordinates of your project. Please do not use one of the reserved the Camel groupIds (org.apache.camel
) in that case.
External dependencies can be added using the -d
flag, the mvn
prefix, and the maven coordinates:
kamel run -d mvn:com.google.guava:guava:26.0-jre Integration.java
Note that if your dependencies belong to a private repository, this repository needs to be defined. See Configure maven.
Local and remote dependencies
Local dependencies can be added using the -d
flag and the file://
prefix:
kamel run -d file://path/to/integration-dep.jar Integration.java
You can also specify data files and directories which will be mounted in the running container relative to the working directory:
kamel run -d file://path/to/data.csv?targetPath=/in/container/data.csv Integration.java
If the data files needs to be present at build time, you can also set classpath
option to true
:
kamel run -d file://path/to/data.csv?targetPath=/in/container/data.csv&classpath=true Integration.java
Similarly, remote dependencies can be added with the http(s)://
prefix:
kamel run -d https://raw.githubusercontent.com/example/data.csv|targetPath=/tmp/foo&classpath=true Integration.java
For a full list of options checkout kamel run --help
.
Important note: this will upload the dependencies and data files to the Image Registry. The CLI and Operator need to be setup correctly, especially if authentication or custom certificates are required.
Jitpack dependencies
If your dependency is not published in a maven
repository you will find very useful Jitpack as a way to provide any custom dependency to your runtime Integration environment. In certain occasion you will find useful to include not only your route definition, but also some helper class or any other class which has to be used while defining the Integration behavior. With Jitpack you will be able to compile on the fly a java project hosted in a remote repository and use the produced package as a dependency of your Integration.
The usage is the same as defined above for any maven dependency. It can be added using the -d
flag, but, this time, you need to define the prefix as expected for the project repository you are using (ie, github
). It has to be provided in the form repository-kind:user/repo/version
. As an example, you can provide the Apache Commons CSV dependency by executing:
kamel run -d github:apache/commons-csv/1.1 Integration.java
We support the most important public code repositories:
github:user/repo/version
gitlab:user/repo/version
bitbucket:user/repo/version
gitee:user/repo/version
azure:user/repo/version
The version
can be omitted when you are willing to use the main
branch. Otherwise it will represent the branch or tag used in the project repo.
Dynamic URIs
Unfortunately, Camel K won’t be able to always discover all your dependencies. When you are creating an URI dynamically, then you will also need to instruct Camel K which component to load (via -d
parameter). An example is illustrated in the following code snippet:
...
String myTopic = "purchases"
from("kafka:" + myTopic + "? ... ")
.to(...)
...
Here the from
URI is dynamically created from some variables that will be resolved at runtime. In cases like this, you will need to specify the component and the related dependency to be loaded in the Integration
.