Setting up a Camel project with Maven

In this Camel tutorial we will learn how to set up a basic Camel project using its Maven archetypes which can generate both Java DSL and Spring XML based projects.

There are a few different approaches to configuring components and endpoints.

1. You can explicitly configure a Component using Java code as shown in this example:

context.addRoutes(new RouteBuilder() {
  public void configure() {

2. You can configure your Component or Endpoint instances in your Spring XML. When using Spring the CamelContext can be pre configured based on defined beans in spring XML.

The advantage of using Spring based Camel project is that you can build complex Camel routes declaratively, that is just by filling up the Camel Spring configuration file, named camel-context.xml

We will show here how to build example projects using Java DSL or Spring XML and Maven.

Creating a Camel Java DSL Project using Maven

You can create your Camel project as follows from the Command Line:

mvn archetype:generate \  -DarchetypeGroupId=org.apache.camel.archetypes \  -DarchetypeArtifactId=camel-archetype-java \  -DarchetypeVersion=3.0.0 \  -DgroupId=com.mastertheboss.camel \  -DartifactId=camel-helloworld \  -Dversion=1.0-SNAPSHOT

The project camel-helloworld will be created. Now import it in your favorite IDE. In our case we import it into IntelliJ Idea Community edition.

We will just edit the MainApp class to remove deprecated code and use Camel Main class instead. Here is how it should be like:

package com.mastertheboss.camel;
import org.apache.camel.main.Main;
public class MainApp {
  public static void main(String...args) throws Exception {
    // use Camels Main class         
    Main main = new Main();

The Class MyRouteBuilder which has been created for you is a Java DSL class to route messages.

package com.mastertheboss.camel;

import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {
  public void configure() {
        .when(xpath("/person/city = 'London'"))
        .log("UK message")
        .log("Other message")

As you can see, all this Class does is to process the input files (leaving them in place, as configured from the ‘noop’ flag) and then perform Content Based routing using an XPath expression.

The XML files are picked up from the folder ‘data’ which is included in your project.

Finally, within the file you can set custom definitions for your Loggers:

appender.out.type = Console = out appender.out.layout.type = PatternLayout appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n rootLogger.level = INFO rootLogger.appenderRef.out.ref = out

The project file (pom.xml) includes the following dependencies in it:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="" xmlns:xsi=""          xsi:schemaLocation="">    <modelVersion>4.0.0</modelVersion>    <groupId>com.mastertheboss.camel</groupId>   <artifactId>camel-helloworld</artifactId>   <packaging>jar</packaging>   <version>1.0-SNAPSHOT</version>    <name>A Camel Route</name>    <properties>     <>UTF-8</>     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>   </properties>    <dependencyManagement>     <dependencies>       <!-- Camel BOM -->       <dependency>         <groupId>org.apache.camel</groupId>         <artifactId>camel-parent</artifactId>         <version>3.0.0</version>         <scope>import</scope>         <type>pom</type>       </dependency>     </dependencies>   </dependencyManagement>    <dependencies>      <dependency>       <groupId>org.apache.camel</groupId>       <artifactId>camel-core</artifactId>     </dependency>     <dependency>       <groupId>org.apache.camel</groupId>       <artifactId>camel-main</artifactId>     </dependency>      <!-- logging -->     <dependency>       <groupId>org.apache.logging.log4j</groupId>       <artifactId>log4j-api</artifactId>       <scope>runtime</scope>     </dependency>     <dependency>       <groupId>org.apache.logging.log4j</groupId>       <artifactId>log4j-core</artifactId>       <scope>runtime</scope>     </dependency>     <dependency>       <groupId>org.apache.logging.log4j</groupId>       <artifactId>log4j-slf4j-impl</artifactId>       <scope>runtime</scope>     </dependency>      <!-- testing -->     <dependency>       <groupId>org.apache.camel</groupId>       <artifactId>camel-test</artifactId>       <scope>test</scope>     </dependency>   </dependencies>    <build>     <defaultGoal>install</defaultGoal>      <plugins>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-compiler-plugin</artifactId>         <version>3.8.1</version>         <configuration>           <source>1.8</source>           <target>1.8</target>         </configuration>       </plugin>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-resources-plugin</artifactId>         <version>3.1.0</version>         <configuration>           <encoding>UTF-8</encoding>         </configuration>       </plugin>        <!-- Allows the example to be run via 'mvn compile exec:java' -->       <plugin>         <groupId>org.codehaus.mojo</groupId>         <artifactId>exec-maven-plugin</artifactId>         <version>1.6.0</version>         <configuration>           <mainClass>com.mastertheboss.camel.MainApp</mainClass>           <includePluginDependencies>false</includePluginDependencies>         </configuration>       </plugin>      </plugins>   </build>  </project>

Run the Main class and check from the logs that the files have been routed:

[1) thread #2 - file://src/data] route1                         INFO  UK message [1) thread #2 - file://src/data] route1                         INFO  Other message

Congratulations! you have just run your first Camel project with Maven

Source code for Camel Hello world project:

Creating a Camel Spring Project using Maven

If you want to create your Spring version of the same example you can use the maven archetype:

mvn archetype:generate                   \   -DarchetypeGroupId=org.apache.camel.archetypes  \   -DarchetypeArtifactId=camel-archetype-spring   \   -DarchetypeVersion=3.0.0 \   -DgroupId=com.mastertheboss.camel \   -DartifactId=camel-helloworld-spring \   -Dversion=1.0-SNAPSHOT

The main difference is that the project will not include Java resources but the following camel-context.xml file with the definition of the Camel Context and the route:

<?xml version="1.0" encoding="UTF-8"?><!-- Configures the Camel Context--><beans xmlns="" xmlns:xsi="" xsi:schemaLocation="">
   <camelContext xmlns="">
      <!-- here is a sample which processes the input files          (leaving them in place - see the 'noop' flag)          then performs content based routing on the message using XPath -->
         <from uri="file:src/data?noop=true"/>
               <xpath>/person/city = 'London'</xpath>
               <log message="UK message"/>
               <to uri="file:target/messages/uk"/>
               <log message="Other message"/>
               <to uri="file:target/messages/others"/>

Here is the project tree:

src ├── data │   ├── message1.xml │   └── message2.xml ├── main │   ├── java │   │   └── com │   │       └── mastertheboss │   │           └── camel │   └── resources │       ├── │       └── META-INF │           └── spring │               └── camel-context.xml └── test     ├── java     │   └── com     │       └── mastertheboss     │           └── camel     └── resources 

You can run the project with:

mvn install camel:run

Source code for the Camel Hello World Spring: