如何通过 ant 构建脚本编译 .drl 文件 [英] How to compile a .drl file through an ant build script

查看:31
本文介绍了如何通过 ant 构建脚本编译 .drl 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Drools 的新手.我想知道是否可以使用某种可以在 Windows 命令行(shell/cmd)中输入的命令来编译 .drl 文件.我查看了 drools 发行版附带的二进制文件,但我无法找到编译 .drl 文件的方法.我对这样的命令感兴趣的原因是我想编写一个 ant 构建文件,它将编译我的 java 类和规则并创建一个 jar.这个 jar 应该是自给自足的,即从命令行运行 jar 应该运行主程序,该程序在会话中传递事实,从而自动执行对这些事实进行操作的规则.

I am new to Drools. I want to know if it is possible to compile a .drl file using some kind of a command that can be entered in the windows command line (shell/cmd). I looked through the binaries that come with the drools distribution but I am unable to figure out a way to compile a .drl file. The reason I am interested in such a command is that I want to write an ant build file which will compile my java classes and rules and create a jar. This jar should be self sufficient, i.e running the jar from the command line should run the main program, which passes facts in the session causing the rules that operate on these facts to automatically be executed.

推荐答案

DroolsCompilerAntTask 曾经是执行此操作的方法.它将获取您所有的各种规则文件并将它们编译成一个序列化文件.它似乎在 5.3 中存在一些错误,但我目前正在尝试解决这些错误.同时,这里有一个说明性的构建文件,可用于创建基于 Drools 的可执行 JAR.如果无法编译规则,则构建将失败.

The DroolsCompilerAntTask used to be the way to do this. It would take all your various rule files and compile them into a serialized file. It appears to have some bugs in 5.3 though which I am currently trying to work out. In the meantime, here is an illustrative build file that can be used for creating an executable JAR based on Drools. The build will fail if the rules cannot be compiled.

<project name="DroolsProto" default="dist" basedir=".">
  <property name="build.src" location="src"/>
  <property name="build.target" location="target"/>
  <property name="build.dist"  location="dist"/>
  <property name="build.artifact" value="droolsproto"/>
  <property name="one-jar.dist.dir" value="~/Work/Data/Misc/OneJar"/>   
  <property name="one-jar.version" value="0.97"/>
  <property name="one-jar.ant.jar" value="${one-jar.dist.dir}/one-jar-ant-task-${one-jar.version}.jar"/>

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

  <taskdef name="one-jar" classname="com.simontuffs.onejar.ant.OneJarTask"
      classpath="${one-jar.ant.jar}" onerror="report"/>

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

  <target name="clean">
    <delete dir="${build.target}"/>
    <delete dir="${build.dist}"/>
  </target>

  <target name="init">
    <tstamp/>
    <mkdir dir="${build.target}"/>
    <mkdir dir="${build.dist}"/>
  </target>

  <target name="compile" depends="init">
    <mkdir dir="${build.target}/classes"/>
    <javac srcdir="${build.src}/main/java" destdir="${build.target}/classes">
      <classpath refid="build.lib.path"/>
      <include name="**/*.java"/>
      <exclude name="**/*Test.java"/>
    </javac>
  </target>

  <target name="verify-rules">
    <droolscompiler srcDir="${build.src}/main/resources" toFile="${build.target}/classes/foo.foo">
      <classpath refid="build.lib.path"/>
    </droolscompiler>
  </target>

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

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

  <target name="dist" depends="compile, bundle-resources">
    <one-jar destfile="${build.dist}/${build.artifact}.jar">
      <manifest>
        <attribute name="One-Jar-Main-Class" value="org.drools.examples.HelloWorldExample"/>
      </manifest>
      <main>
        <fileset dir="${build.target}/classes"/>
      </main>
      <lib>
        <fileset dir="${build.src}/lib">
          <include name="**/*.jar"/>
        </fileset>
      </lib>
    </one-jar>
  </target>
</project>

请注意,构建使用 One-Jar 来创建自包含的可执行文件,您不妨将其替换为您选择的Super Jar™"工具.还有一个 DroolsVerifierAntTask 据称可以检查您的规则中的逻辑错误(而不是语法错误),但我没有相关经验.

Note that the build uses One-Jar in order to create the self-contained executable, you may wish to substitute this with your 'Super Jar™' tool of choice. There is also a DroolsVerifierAntTask which allegedly can check logical errors in your rules (as opposed to syntactical ones), but I have no hands on experience with it.

这篇关于如何通过 ant 构建脚本编译 .drl 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆