jBPM 5 and Maven example project

This tutorial has been written for an old version of jBPM which is now deprecated.

If you want to get started quickly with jBPM, we recommend the following quickstarts:

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).


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("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");

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





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] ------------------------------------------------------------------------
[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