Sans Pareil Technologies, Inc.

Key To Your Business

Scrooge with Ant

Scrooge is a thrift code generator for generating Scala (also Java, but that is already handled by thrift) sources from thrift definitions. The scrooge project provides integration with SBT or Maven, however documentation related to performing the task with Ant is lacking. Here we will illustrate using the scrooge command line interface to perform code generation from ant using ivy for dependency management.

All we do is use the java ant task to execute the com.twitter.scrooge.Main class with the appropriate options to generate scala sources from the thrift definitions.
The following ivy.xml file should provide your project all the dependencies needed for scrooge.
<ivy-module version="2.2">
  <info organisation="com.sptci" module="thrift"/>

    <conf name="compile"  description="Compile the source tree"/>
    <conf name="test" extends="compile" visibility="private" description="Test application"/>

    <dependency org="org.scala-lang" name="scala-library" rev="${scala.version}">
      <artifact name="scala-library" type="jar" />

    <dependency org="org.scala-lang" name="scala-reflect" rev="${scala.version}">
      <artifact name="scala-reflect" type="jar" />

    <dependency org="org.scala-lang" name="scala-compiler" rev="${scala.version}">
      <artifact name="scala-compiler" type="jar" />

    <dependency org="com.twitter" name="scrooge-core_2.10" rev="3.16.3">
      <artifact name="scrooge-core_2.10" type="jar" />

    <dependency org="com.twitter" name="scrooge_2.10" rev="3.16.3">
      <artifact name="scrooge_2.10" type="jar" />

    <dependency org="com.twitter" name="scrooge-generator_2.10" rev="3.16.3">
      <artifact name="scrooge-generator_2.10" type="jar" />

    <dependency org="org.apache.thrift" name="libthrift" rev="0.9.1">
      <artifact name="libthrift" type="jar" />

    <exclude org="org.xerial.snappy" module="snappy-java" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="org.apache.commons" module="commons-lang3" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="javax.servlet" module="servlet-api" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="org.apache.httpcomponents" module="httpclient" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="org.apache.httpcomponents" module="httpcore" type="*" ext="*" conf="" matcher="exact" />

    <exclude org="javax.mail" module="mail" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="javax.activation" module="activation" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="javax.jms" module="jms" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="com.sun.jdmk" module="jmxtools" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="com.sun.jmx" module="jmxri" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="jline" module="jline" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="net.sf.jopt-simple" module="jopt-simple" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="org.hamcrest" module="hamcrest-core" type="*" ext="*" conf="" matcher="exact" />

    <exclude org="org.scala-lang" module="jline" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="org.fusesource.jansi" module="jansi" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="com.twitter" module="util.{0,}" type="*" ext="*" conf="" matcher="regexp" />
    <exclude org="" module="guava" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="" module="jsr305" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="io.netty" module="netty" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="" module="compiler" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="org.codehaus.plexus" module="plexus-utils" type="*" ext="*" conf="" matcher="exact" />
    <exclude org="commons-cli" module="commons-cli" type="*" ext="*" conf="" matcher="exact" />


The following properties file is used to configure the ant build system.
scala.version                 2.10.4

debug                              yes

ivy.home                         ${user.home}/.ivy2
ivy.jar.dir                         ${ivy.home}/lib
ivy.jar.file                        ${ivy.jar.dir}/ivy.jar
ivy.install.version          2.3.0

dir.lib                               lib

dir.src                              src                      ${dir.src}/app
dir.src.test                       ${dir.src}/test
dir.config                        config                          build
dir.deploy                       deploy

dir.src.thrift                     ${dir.src}/definitions
Build Script 
The following build script can be used to build your project. Note that we use a generate task as a dependency on the main compile task, which will ensure that the scala sources are generated from the thrift definitions prior to compiling the sources.
<?xml version="1.0"?>

<project name="ScroogeThrift" default="compile" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">

  <property environment="env" />
  <property file="" />

  <target name="clean"
          description="Clears all generated files, including build directories, distributables, and documentation.">
    <delete dir="${}" />
    <delete dir="${dir.deploy}" />

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

  <target name="init" description="Initialise build system" depends="init-ivy, resolve">
    <property name="scala-library.jar"
      value="${dir.lib}/scala-library-${scala.version}.jar" />

    <taskdef resource="scala/tools/ant/antlib.xml">
        <path refid="compile.classpath"/>

  <target name="generate" depends="init">
    <fileset id="fileset" dir="${dir.src.thrift}">
      <include name="*.thrift" />
      <exclude name="common.thrift" />
    <pathconvert property="files" refid="fileset" pathsep=" " />
    <java classname="com.twitter.scrooge.Main" classpathref="compile.classpath"
          fork="true" failonerror="true" maxmemory="512m">
      <arg line="-d ${} -s ${files}" />

  <target name="compile" depends="generate" description="Compile the source files for the library">
    <mkdir dir="${}" />

    <scalac destdir="${}" target="jvm-1.7"
      deprecation="yes" addparams="-feature -language:postfixOps">
      <compilerarg value="-Xlint:deprecation"/>
        <path path="${}" />
        <path refid="compile.classpath"/>
      <include name="**"/>


  <!-- automatically configure download and install ivy if required. -->
  <target name="download-ivy" unless="offline">
    <mkdir dir="${ivy.jar.dir}"/>
    <get src="${ivy.install.version}/ivy-${ivy.install.version}.jar"
         dest="${ivy.jar.file}" usetimestamp="true"/>

  <target name="init-ivy" depends="download-ivy">
    <path id="ivy.lib.path">
      <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
                 uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>

  <target name="resolve" description="--> retrieve dependencies with ivy">
    <ivy:retrieve />

We have illustrated how you can use scrooge to generate scala sources from thrift definitions using an ant task. In future there may be a proper ant task contributed to the scrooge project which will make the process even simpler.