Tuesday, July 12, 2011

MULE & MAVEN - Creating a Mule Project using Maven

After some analysis and struggle I was able to create a Mule Application with Maven and run it on Eclipse. I am providing some version details below and directly moving to the steps to create a Mule Application with Maven.

Mule Version: 2.2.1
Apache Maven Version: 3.0.3

1. The JBoss Maven repository was deprecated and JBoss stopped providing access to http://repository.jboss.org/maven2. So in the Maven Settings.xml which we can find usually under MAVEN_HOME/conf/ location, we need to update the new JBoss repository location. This would indicate Maven to use the new JBoss repository.

<mirror>
            <id>jboss-public</id>
            <name>JBoss Public Nexus Repository</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
            <mirrorOf>jboss</mirrorOf>
        </mirror>


        <profile>
            <id>jboss-public-repository</id>
            <repositories>
                <repository>
                    <id>jboss-public-repository-group</id>
                    <name>JBoss Public Maven Repository Group</name>
                    <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
                    <layout>default</layout>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
                <repository>
                    <id>maven-nuxeo</id>
                    <name>Maven Nuxeo Repository</name>
                    <url>https://maven.nuxeo.org/nexus/content/groups/public/</url>
                    <layout>default</layout>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>jboss-public-repository-group</id>
                    <name>JBoss Public Maven Repository Group</name>
                    <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
                    <layout>default</layout>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>

2. For Mule, we need to add the Codehaus repository and the Mule plugin to the settings.xml

    <pluginGroups>
        <pluginGroup>org.mule.tools</pluginGroup>
        <pluginGroup>org.jboss.maven.plugins</pluginGroup>
    </pluginGroups>

      <profile>
            <id>codehaus</id>
            <repositories>
                <repository>
                    <id>codehaus.org</id>
                    <name>CodeHaus Snapshots</name>
                    <url>http://snapshots.repository.codehaus.org</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>codehaus-release-repo</id>
                    <name>Codehaus Release Repo</name>
                    <url>http://repository.codehaus.org</url>
                </repository>
                <repository>
                    <id>codehaus-snapshot-repo</id>
                    <name>Codehaus Snapshot Repo</name>
                    <url>http://snapshots.repository.codehaus.org</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>codehaus.org</id>
                    <name>CodeHaus Plugin Snapshots</name>
                    <url>http://snapshots.repository.codehaus.org</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>

    <activeProfiles>
        <activeProfile>jboss-public-repository</activeProfile>
        <activeProfile>codehaus</activeProfile>
    </activeProfiles>

3. Create a simple Maven Project in Eclipse.

4. Replace the pom.xml with the below contents that contains the basic jar files required for a simple Mule Application.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.mybusiness</groupId>
    <artifactId>ShippingService</artifactId>
    <version>1.0</version>


    <repositories>
        <repository>
            <id>MuleRepo</id>
            <name>Mule Repository</name>
            <url>http://repository.muleforge.org/</url>
            <layout>default</layout>
            <releases>
                <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
        <repository>
            <id>codehaus-repo</id>
            <name>Codehaus Repository</name>
            <url>http://dist.codehaus.org/mule/dependencies/maven2</url>
            <layout>default</layout>
            <releases>
                <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Maven Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>


    <dependencies>
        <!-- Mule Dependencies Start -->
        <dependency>
            <groupId>org.mule</groupId>
            <artifactId>mule-core</artifactId>
            <version>2.2.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!--
            Mule Transports dependency Start We can include the needed
            transports based on the project need
        -->
        <dependency>
            <groupId>org.mule.transports</groupId>
            <artifactId>mule-transport-stdio</artifactId>
            <version>2.2.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.mule.transports</groupId>
            <artifactId>mule-transport-file</artifactId>
            <version>2.2.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <properties>
        <mule.version>2.2.1</mule.version>
    </properties>
</project>

5. Create a Mule Config file in your project. Since the project do not have the Mule Libraries by default in its classpath, we cannot directly run the application.

Instead Go to Run->Run Configurations
Double click on Java Application.
In the new configuration, provide your project name and in the main class, select the org.mule.MuleServer
In the Arguments tab, provide the input as   -config src/main/resources/mule-config.xml

This will start the Mule Application.

6. To see the Mule logs on the Eclipse console, create a log4j.properties file in the source folder with the following contents.

#------------------------------------------------------------------------------
#
#  The following properties set the logging levels and log appender.  The
#  log4j.rootCategory variable defines the default log level and one or more
#  appenders.  For the console, use 'C'.  For the daily rolling file, use 'R'.
#  For an HTML formatted log, use 'H'.
#
#  To override the default (rootCategory) log level, define a property of the
#  form (see below for available values):
#
#        log4j.logger. =
#
#    Possible Log Levels:
#      FATAL, ERROR, WARN, INFO, DEBUG
#
#------------------------------------------------------------------------------
log4j.rootCategory=INFO, C


#------------------------------------------------------------------------------
#  Also we can define the levels for different packages.
#------------------------------------------------------------------------------
log4j.logger.org.mule=INFO


#------------------------------------------------------------------------------
#
#  The following properties configure the console (stdout) appender.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.C = org.apache.log4j.ConsoleAppender
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

With this you can see the logs getting displayed on the Eclipse console when the application gets started.

Based on Need: To get the dependencies list for each Mule transport/module refer to the following link.

No comments:

Post a Comment