jBPM 5 and Maven example project

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

In the following tutorial we will learn how to create a Maven based jBPM 5 project and run a test process on it.


The prerequisite to this tutorial is that you have installed Maven on your pc or Maven2Eclipse plugin which allows running Maven projects from within your Eclipse environment. (You can read more about Maven and Eclipse integration in this tutorial).


In order to get started you just need a basic Maven archetype which can be used to set up an initial structure for our projects. The maven-archetype-quickstart can be used for this purpose.

From a shell, issue the following command:


mvn archetype:generate -DgroupId=com.sample.app -DartifactId=my-app

       -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Once done with the archetype, let's work on the pom.xml file which contains our Maven project model. The first thing we need to add is the set of jBPM and Drools dependencies (we won't actually use Drools rules in this example, however in the following demo we will inject some rules in our projects).


<properties>
        <drools.version>5.4.0.Final</drools.version>
        <jbpm5.version>5.4.0.Final</jbpm5.version>
</properties>
<dependencies>
        <dependency>
            <groupId>org.jbpm</groupId>
            <artifactId>jbpm-flow</artifactId>
            <version>${jbpm5.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jbpm</groupId>
            <artifactId>jbpm-bpmn2</artifactId>
            <version>${jbpm5.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jbpm</groupId>
            <artifactId>jbpm-flow-builder</artifactId>
            <version>${jbpm5.version}</version>
        </dependency>
</dependencies>


Ok, now we need to design a simple process to test jBPM 5. For this purpose we will create a Greeting process which merely dumps a process variable and use a J2SE library to print the current date.

Hint: You can use the jBPM - Eclipse plugin in order to design your process or, as an alternative, consider using the Web editor which is part of jBPM 5 installation.

 The following process will be created into the src/main/resources folder so that it will be visible to the application's classpath:

jbpm 5 demo maven jboss tutorial eclipse

This process is merely composed of a Start Node, a Script Task and an End Node. Click on the Script Task node and enter the Action Editor. From there enter a simple Java script:


System.out.println("Welcome " + kcontext.getVariable("name"));
System.out.println("=============================");
System.out.println("Today is  " + new java.util.Date().toString());

This script just prints the "name" process variable from the Knowledge Session.

And now, let's code a simple Main class which will instantiate our process:

package com.sample.app;

import java.util.HashMap;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public class Test1 {

    public static final void main(String[] args) {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("demo.bpmn"), ResourceType.BPMN2);
        KnowledgeBase kbase = kbuilder.newKnowledgeBase();
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("name", "Francesco");
        
        ksession.startProcess("greeting.process",params);
        ksession.dispose();
    }
}


Now last thing in order to run our main class is a maven compiler plugin and the maven exec plugin which is configured to run as main class com.sample.app.Test1


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.sample.app.Test1</mainClass>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>jboss</id>
            <name>jboss</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>


You can compile and test your example using:

mvn install test


The expected output is:

Welcome Francesco
=============================
Today is  Mon Apr 22 17:30:47 CEST 2013
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.649s
[INFO] Finished at: Mon Apr 22 17:30:47 CEST 2013
[INFO] Final Memory: 15M/109M
[INFO] ------------------------------------------------------------------------

Download the demo example 


Advertisement