Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Building a Mule Application with Maven outside Studio

This guide walks you through creating  a Mule Application project from scratch using a Maven archetype. It covers generating a project template, revising the configuration to create a simple "Hello World" application, manually maintaining its POM file, then building and deploying the project. 

Assumptions

The document assumes you have downloaded Apache Maven and are familiar with using it to build and maintain projects. Read more about Using Maven with Mule.

Configuring Maven

Add the two blocks of code to the file settings.xml (usually in your Maven conf or $HOME/.m2 directory)

This will have Maven allow you to execute Mule plugins:

settings.xml


         
      
1
2
3
4
5
6
<settings>
    <pluginGroups>
        <pluginGroup>org.mule.tools</pluginGroup>
    </pluginGroups>
    ...
</settings>

This will tell your Maven instance where to find the Mule repositories:

settings.xml


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<profile>
    <id>Mule Org</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <repositories>
        <repository>
            <id>mulesoft-releases</id>
            <name>MuleSoft Repository</name>
            <url>https://repository-master.mulesoft.org/releases/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>mulesoft-snapshots</id>
            <name>MuleSoft Snapshot Repository</name>
            <url>https://repository-master.mulesoft.org/snapshots/</url>
            <layout>default</layout>
        </repository>
    </repositories>
</profile>
If you need to add dependencies that are not included (for example to the Salesforce connector), or if you’re using Mule Enterprise and need the enterprise repositories, see Configuring Maven POM Files and Settings.

Creating a Project Template

  1. Open a command shell and navigate to the directory where you want to create your project.

  2. Execute the mule-project-archetype as shown. If this is your first time running this command, Maven downloads the archetype for you.

    
                
             
    1
    
    mvn mule-project-archetype:create -DartifactId=helloWorld -DmuleVersion=3.5.0

    Running the archetype

    Maven uses by default the latest available version of the archetype. This can cause problems if you want to create a project for an earlier version of Mule. In this case, run the mule-project-archetype specifying the full version of the plugin like this:

    
                     
                  
    1
    
    mvn org.mule.tools:mule-project-archetype:3.1.1:create ...

    At minimum, you pass in two system parameters:

    • artifactId: The short name for the project (such as myApp). This should be a single word in lower camel case with no spaces, periods, hyphens, etc. 

      artifactId

      The artifactId can contain characters such as underscore or hyphen. However, the plug-in will convert the name into a usable form suitable for Java. For example, if the argument is specified as -DartifactId=My#Awesome-Mule_Project, the project will be created in a directory of that name, but the project name will be MyAwesomeMuleProject and the package name will be .myawesomemuleproject
    • muleVersion: The version of the Mule project archetype you want to use. This will also be the default Mule version used for the generated artifact.

  3. The plugin asks a series of questions. The table below shows a set of possible responses.

    Question Answer

    Provide a description of what the project does.

    This is a basic "Hello, World" test project.

    Which version of Mule is this project targeted at?

    3.5.0

    What is the base Java package path for this project?

    com/mycompany/project

    Which Mule transports do you want to include in this project?

    http

    Which Mule modules to you want to include in this project?

    (Leave blank.)

    You can also choose to run this archetype in "silent mode" by including -DinteractiveMode=false, then passing in any of the following parameters.

    Name Example Default Value

    groupId

    -DgroupId=org.mule.application.<artifactId>

    org.mule.application.<artifactId>

    packagePath

    -DpackagePath=org/mule/application

    none

    transports

    -Dtransports=http,vm

    cxf,file,http,jdbc,jms,stdio,vm

    muleVersion

    -DmuleVersion=3.5.0

    none

    packageName

    -DpackageName=project

    none

    description

    -Ddescription="My Hello World app"

    none

    modules

    -Dmodules=xml,scripting

    client,management,scripting,sxc,xml

    basedir

    -Dbasedir=/projects/mule/tools

    <current dir>

    package

    -Dpackage=org/mycompany/project

    none

    artifactId

    -DartifactId=helloWorld

    <artifactId>

    version

    -Dversion=1.0-SNAPSHOT

    <muleVersion>

  4. After you have answered all the questions, the archetype creates a directory using the project name you specified. This directory includes:

    1. a POM file for building with Maven

    2. a Mule configuration file ( src\main\app\mule-config.xml ) that includes the namespaces for the transports and modules you specified and has placeholder elements for creating your first flow

    3. a package.html file under src\main\java using the package path you specified, i.e. src\main\java\com\mycompany\project\package.html

    4. some template files under src\test to help you get started creating a unit test for the project

    5. A new MULE-README.txt file in the root of your project, explaining what files were created

Creating a Mule Application from the Template

  1. Open src/main/app/mule-config.xml. Delete the flow that exists by default and add this one:

    
                
             
    1
    2
    3
    4
    5
    
    <flow name="exampleFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost"
    port="8081" />
        <set-payload value="Hello World!"/>
    </flow>
  2. Save this mule-config.xml. You now have a working project that can be packaged and deployed. 

Managing Dependencies

If you add logic into your project that relies on a dependency that isn’t already included in the POM file generated by the archetype, you have to modify your POM file manually to add this dependency, as described here. This example walks you through revising your project test case in a way that requires an additional Maven dependency: the REST-assured DSL .

  1. Open the example test case file, src/test/java/com/mycompany/project/helloWorld/HelloWorldTestCase.java and replace the existing code with the following:

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    package com.mycompany.project.helloworld;
     
    import static org.junit.Assert.*;
    import static com.jayway.restassured.RestAssured.*;
    import org.junit.Test;
    import org.mule.tck.junit4.FunctionalTestCase;
    import org.apache.commons.cli.ParseException;
     
    public class HelloWorldTestCase extends FunctionalTestCase {
        private static String MESSAGE = "Hello world!";
         
        @Override
        protected String getConfigResources()
        {
        return "mule-config.xml";
        }
         
        @Test
        public void dependencyClientTestCase() throws Exception
        {
        String response = get("http://localhost:8081").body().asString();
        assertEquals(MESSAGE, response);
        }
    }

    Notes:
    * Line 4: Imports REST-assured
    * Lines 21-22: use REST-assured

  2. Add the following dependencies to your pom.xml file, located in the application’s root folder.

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    <dependency>
      <groupId>com.jayway.restassured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>2.3.1</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.codehaus.groovy</groupId>
          <artifactId>groovy</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.2.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-cli</groupId>
      <artifactId>commons-cli</artifactId>
      <version>1.1</version>
    </dependency>
  3. While in the directory where the pom.xml file resides, package your Mule project from the command line by executing the following syntax:

    mvn package

    The maven-mule-plugin packages the project in a . zip file.  Copy the .zip file to the apps folder of your standalone Mule ESB instance to run the application. 

  4. The console should print BUILD SUCCESS

    build-success-image%281%29

See Also