In this short tutorial we will learn how to create a Drools Project using Maven's kie-drools-archetype.

I've seen in many forums the questions which is the archetype for creating a Drools project. As you can see from the Maven's repository ( the correct archetype is the following one (in its latest version):


Let's try it!

mvn archetype:generate -B -DarchetypeGroupId=org.kie -DarchetypeArtifactId=kie-drools-archetype -DarchetypeVersion=7.7.0.Final -DgroupId=com.mastertheboss -DartifactId=drools-project -Dversion=1.0-SNAPSHOT -Dpackage=drools.project

The above command will create the drools-project folder with a Maven project. Let's check that everything is in order:

tree drools-project/
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── drools
    │   │       └── project
    │   │           └──
    │   └── resources
    │       ├── META-INF
    │       │   └── kmodule.xml
    │       └── rules.drl
    └── test
        ├── java
        │   └── drools
        │       └── project
        │           └──
        └── resources

The archetype has create a very basic Rule file (rules.drl) which loads a simple Fact in it:

package drools.project;

global java.util.Set controlSet;

rule "will execute per each Measurement having ID color"
	Measurement( id == "color", $colorVal : val )

A Test class is included in the Archetype, which will insert into a KieSession new Measurement. Then it fires the Rule and expects that the Session contains the three different kind of Measurement inserted:

package drools.project;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.drools.core.time.SessionPseudoClock;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.definition.KiePackage;
import org.kie.api.definition.rule.Rule;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.conf.ClockTypeOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RuleTest {
    static final Logger LOG = LoggerFactory.getLogger(RuleTest.class);

    public void test() {
        KieServices kieServices = KieServices.Factory.get();

        KieContainer kContainer = kieServices.getKieClasspathContainer();
        Results verifyResults = kContainer.verify();
        for (Message m : verifyResults.getMessages()) {
  "{}", m);
        }"Creating kieBase");
        KieBase kieBase = kContainer.getKieBase();"There should be rules: ");
        for ( KiePackage kp : kieBase.getKiePackages() ) {
            for (Rule rule : kp.getRules()) {
      "kp " + kp + " rule " + rule.getName());
        }"Creating kieSession");
        KieSession session = kieBase.newKieSession();"Populating globals");
        Set<String> check = new HashSet<String>();
        session.setGlobal("controlSet", check);"Now running data");

        Measurement mRed= new Measurement("color", "red");

        Measurement mGreen= new Measurement("color", "green");

        Measurement mBlue= new Measurement("color", "blue");
        session.fireAllRules();"Final checks");

        assertEquals("Size of object in Working Memory is 3", 3, session.getObjects().size());
        assertTrue("contains red", check.contains("red"));
        assertTrue("contains green", check.contains("green"));
        assertTrue("contains blue", check.contains("blue"));


In order to build and test the project, the following pom.xml has been included, which uses drools-bom to handle the Drool dependencies version (in this example 7.7.0.Final).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">










That's all. Compile and build the project with:

$ mvn clean install test

And see it in action in the following Drools screen cast: