分享

Compiling Scala code with Ant

 miqi05 2010-04-05
I'm working on a Railo project that is using some Scala code for part of the system and I'm using Ant to drive the build process. The Scala website has a lot of tools and documentation and you can read about compiling Scala with Ant for the basics of how to get started.

What they don't cover there is what is needed to bundle up that compiled Scala code and put it somewhere that Railo can get at.

Here's the Ant task I use to create and deploy a JAR:

<target name="publisher" depends="build-scala,docs-scala" description="Deploys the Publisher and generates its documentation.">
      <jar destfile="${www}/WEB-INF/railo/lib/Publisher.jar" basedir="${build.dir}"/>
      <copy file="${scala.home}/lib/scala-library.jar" todir="${www}/WEB-INF/railo/lib"/>
   </target>
${www} defines my web root and ${scala.home} defines where Scala is installed on my system.

Read on for more details of the build-scala and docs-scala tasks...

The build-scala task compiles the .scala files to .class files (and is very similar to that shown on the Scala website):

<target name="build-scala" depends="init-scala" description="Build the Publisher Scala code.">
      <mkdir dir="${build.dir}"/>
      <scalac srcdir="${sources.dir}" destdir="${build.dir}" classpathref="build.classpath">
         <include name="**/*.scala"/>
      </scalac>
   </target>

The docs-scala task generates JavaDoc-style API documentation from the source code. It's a good idea to do this automatically every time you build the system so the documentation is always up to date. I don't commit the generated docs to git so that no one accidentally gets hold of an old version.

<target name="docs-scala" depends="init-scala" description="Generate documentation for the Publisher.">
      <mkdir dir="${docs.dir}" />
      <scaladoc
            srcdir="${sources.dir}"
            destdir="${docs.dir}"
            deprecation="yes" unchecked="yes" addparams="-access:private"
            windowtitle="Publisher Documentation"
            doctitle="<div>Publisher</div>"
            classpathref="build.classpath">

         <include name="**/*.scala" />
      </scaladoc>
   </target>

And finally the init-scala task sets up the Scala environment for the build / docs tasks:

<target name="init-scala" description="Set up the environment for Scala.">
      <property name="scala-library.jar" value="${scala.home}/lib/scala-library.jar"/>
      <path id="build.classpath">
         <pathelement location="${scala-library.jar}"/>
         <pathelement location="${www}/WEB-INF/lib/mysql-connector-java-bin.jar"/>
         <pathelement location="${build.dir}"/>
      </path>
      <taskdef resource="scala/tools/ant/antlib.xml">
         <classpath>
            <pathelement location="${scala.home}/lib/scala-compiler.jar" />
            <pathelement location="${scala-library.jar}" />
         </classpath>
      </taskdef>
   </target>

Note that we're relying on the MySQL JDBC driver (the Publisher reads large numbers of records from the database and converts them to a specific XML representation that is needed by a search engine).

The last part of the Ant build file is the high-level properties that specify where Scala is installed and the source, build and docs directories:

<property name="scala.home" value="${basedir}/../scala"/>
   <property name="sources.dir" value="${basedir}/../publisher/src"/>
   <property name="build.dir" value="${basedir}/../publisher/build"/>
   <property name="docs.dir" value="${basedir}/../publisher/docs"/>

My repo is structured so the build.xml file is in a build folder at the top level, Scala is installed in a scala folder also at the top level (yes, a minimal Scala installation is under git as part of our build process) and the Publisher code is in the publisher folder at the top level (with the source code under a src folder).

source: http:///blog/index.cfm/do/blog.entry/entry/Compiling_Scala_code_with_Ant

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多