Compiling Scala code with Ant

来源:百度文库 编辑:神马文学网 时间:2024/04/29 00:59:19
Compiling Scala code with AntI'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:"publisher" depends="build-scala,docs-scala" description="Deploys the Publisher and generates its documentation.">
      "${www}/WEB-INF/railo/lib/Publisher.jar" basedir="${build.dir}"/>
      "${scala.home}/lib/scala-library.jar" todir="${www}/WEB-INF/railo/lib"/>
   ${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):"build-scala" depends="init-scala" description="Build the Publisher Scala code.">
      "${build.dir}"/>
      "${sources.dir}" destdir="${build.dir}" classpathref="build.classpath">
         "**/*.scala"/>
      
   

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

         "**/*.scala" />
      
   

And finally the init-scala task sets up the Scala environment for the build / docs tasks:"init-scala" description="Set up the environment for Scala.">
      "scala-library.jar" value="${scala.home}/lib/scala-library.jar"/>
      "build.classpath">
         "${scala-library.jar}"/>
         "${www}/WEB-INF/lib/mysql-connector-java-bin.jar"/>
         "${build.dir}"/>
      
      "scala/tools/ant/antlib.xml">
         
            "${scala.home}/lib/scala-compiler.jar" />
            "${scala-library.jar}" />
         

      
   

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:"scala.home" value="${basedir}/../scala"/>
   "sources.dir" value="${basedir}/../publisher/src"/>
   "build.dir" value="${basedir}/../publisher/build"/>
   "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://corfield.org/blog/index.cfm/do/blog.entry/entry/Compiling_Scala_code_with_Ant