Compile a Drools 5.5 Package Using ANT

Compile a Drools 5.5 Package Using ANT

Recently, when working Drools 5.5.0.Final into an application, I had need to create an ANT build script to verify and compile rules into a serialized package for distribution, and I wanted to share the script I used to accomplish that.

NOTE: This tutorial assumes that the Drools release packages for Drools 5.5.0.Final have been downloaded and extracted to their necessary locations, specifically the distribution and jbpm-tools-distribution packages as both are required for this build script to function correctly. It also assumes that you have created and setup a Drools project in your workspace, and have generated the necessary rules for compilation.

If you need to download any packages listed in this tutorial you can find them at http://download.jboss.org/drools/release/5.5.0.Final/.

1. Create a build.properties File

Begin by creating a build.properties file to be referenced in the ANT build script containing properties that are needed to execute the build script. The proper locations to which the downloaded packages were installed will need to be referenced in the appropriate locations. The build.properties file for this tutorial can be found below:

# Project References
project.lib=./lib

# Build Path
drools55.lib=../drools-distribution-5.5.0.Final/binaries
drools55.tools.lib=../droolsjbpm-tools-distribution-5.5.0.Final/binaries

# Build Properties
build.src=src
build.target=bin
build.dist=dist

2. Create a build.xml Build File

Once a build.properties file has been created, and the correct locations referenced in the appropriate properties, a build.xml file needs to be created. The build.xml file, will leverage the DroolsCompilerAntTask found in the jbpm-tools-distribution package, to verify and compile the rules into a serialized package. The build.xml file will also compile any necessary java classes, and compile the compiled classes and serialized packages into a JAR file.

The build.xml build script for this tutorial, found below, executes the following targets:

  • compile : Compiles the Java classes in the Drools project from the src directory to the appropriate target directory.
  • verify-rules : Compiles the Drools rules files into a serialized package, and copies it to the target directory.
  • verify-resources : Performs any resource validation, if necessary, prior to executing the bundle-resources target.
  • bundle-resources : Copies the necessary resources, if any exist, to the target directory.
  • dist : Creates a JAR file from files and resources located in the target directory and copies it to the dist directory.
  • copy : Copies the packaged JAR file and distributes it to the appropriate locations for use within applicable projects.
<?xml version="1.0" encoding="UTF-8"?>
<project name="compile-drools-package" default="copy" basedir=".">

	<property file="build.properties" />

	<path id="build.lib.path">
		<fileset dir="${drools55.lib}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<path id="buildtools.lib.path">
		<fileset dir="${drools55.tools.lib}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<path id="build.model.path">
		<pathelement location="${build.target}" />
	</path>

	<taskdef name="droolscompiler" classname="org.drools.contrib.DroolsCompilerAntTask">
		<classpath refid="buildtools.lib.path" />
	</taskdef>

	<target name="compile">
		<mkdir dir="${build.target}"/>
		<javac srcdir="${build.src}/main/java" destdir="${build.target}" includeantruntime="no">
			<classpath refid="build.lib.path"/>
			<include name="**/*.java"/>
		</javac>
	</target>

	<target name="verify-rules">
		<droolscompiler srcdir="${build.src}/main/resources" tofile="${build.target}/crossview-proxy-businessrules.pkg" 
				binformat="package" classpathref="build.model.path">
			<include name="**/*.drl" />
		</droolscompiler>
	</target>

	<target name="verify-resources" depends="verify-rules" />

	<target name="bundle-resources" depends="verify-resources">
		<copy todir="${build.target}">
			<fileset dir="${build.src}/main/resources"/>
		</copy>
	</target>

	<target name="dist" depends="compile, bundle-resources">
		<jar destfile="${build.dist}/serialized-drools-rules.jar">
			<fileset dir="${build.target}" />
		</jar>
	</target>

	<target name="copy" depends="dist">
		<!-- Copy Newly Built Rules JAR -->
		<copy file="${build.dist}/serialized-drools-rules.jar" todir="${project.lib}" verbose="true" />
	</target>
	
</project>

3. Verify Project Configuration

At this point you should have a serialized package compiled and copied to the destination project! Ensure that the destination project contains the copied distribution JAR, and create applicable application logic to retrieve the serialized rules package from the project classpath to pickup and leverage your Drools rules.

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *