2011-01-12 57 views
19

如何爲scala安裝antlib.xml以獲得ant工作?爲scala配置ant

現在我在包含scala任務的build.xml文件上運行ant時遇到以下錯誤。

[taskdef] Could not load definitions from resource scala/tools/ant/antlib.xml. It could not be found. 
/scalala/scalala-read-only/scalala/build.xml:36: Problem: failed to create task or type scalac 

Cause: The name is undefined. 
Action: Check the spelling. 
Action: Check that any custom tasks/types have been declared. 
Action: Check that any <presetdef>/<macrodef> declarations have taken place. 

我已經unjarred的scala-2.8.1.final/lib/scala-compiler.jar,但我不知道往哪裏放的內容。

下面是從build.xml文件對應螞蟻的代碼片段:

<target name="compile" depends=""> 
    <mkdir dir="${build.dir}"/> 

    <scalac srcdir="${src.dir}" destdir="${build.dir}" 
      classpathref="project.classpath" force="changed"> 
      <!-- addparams="-Yclosure-elim -optimise" --> 
     <include name="**/*.scala"/> 
    </scalac> 
    </target> 

回答

下面的代碼是很重要的在你的build.xml文件:

<!-- Define project CLASSPATH. --> 
    <path id="project.classpath"> 
    <pathelement location="${build.dir}" /> 

    <fileset dir="${env.SCALA_HOME}/lib/"> <include name="*.jar" /> </fileset> 

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

    <!-- Define scala compiler, scaladoc, etc command --> 
    <taskdef resource="scala/tools/ant/antlib.xml"> 
    <classpath> 
     <pathelement location="${env.SCALA_HOME}/lib/scala-compiler.jar" /> 
     <pathelement location="${env.SCALA_HOME}/lib/scala-library.jar" /> 
    </classpath> 
    </taskdef> 

我的問題是$SCALA_HOME環境變量(${env.SCALA_HOME})指向錯誤(一個級別太深:/usr/local/scala-2.8.1.final/bin/而不僅僅是/usr/local/scala-2.8.1.final/,因此找不到lib目錄。

+0

也許你應該發佈相關的ant代碼片段。也許你不具備scalac一個typedef在你的build.xml – Raghuram 2011-01-12 05:35:42

回答

20

antlib.xml包含在scala-compiler.jar中。你必須把它放到你的類路徑中。要定義scalac螞蟻任務,把下面的定義爲Ant生成文件(這被認爲是形式http://www.scala-lang.org/node/98):

<target name="init"> 
    <property 
    name="scala-library.jar" 
    value="${scala.home}/lib/scala-library.jar" 
    /> 
    <path id="build.classpath"> 
    <pathelement location="${scala-library.jar}" /> 
    <!--<pathelement location="${your.path}" />--> 
    <pathelement location="${build.dir}" /> 
    </path> 
    <taskdef resource="scala/tools/ant/antlib.xml"> 
    <classpath> 
     <pathelement location="${scala.home}/lib/scala-compiler.jar" /> 
     <!-- NEW: For scala 2.10.2 you need scala-reflect: --> 
     <pathelement location="${scala.home}/lib/scala-reflect.jar" /> 
     <pathelement location="${scala-library.jar}" /> 
    </classpath> 
    </taskdef> 
</target> 

要使用scalac任務,屬性depends="init"添加到您的任務,例如

<target name="compile" depends="init"> 
    <mkdir dir="${build.dir}"/> 

    <scalac srcdir="${src.dir}" destdir="${build.dir}" 
      classpathref="project.classpath" force="changed"> 
      <!-- addparams="-Yclosure-elim -optimise" --> 
    <include name="**/*.scala"/> 
    </scalac> 
</target> 

我希望幫助。

+5

我有斯卡拉 - compiler.jar線之前,補充一點:`` (使用Scala 2.10 RC2)。在此之前,我得到這個錯誤:'按類scala.tools.ant.FastScalac需要一個類不能被發現:使用ClassLoader AntClassLoader` – 2012-11-14 03:42:39