为scala配置ant

dsg*_*dsg 19 ant scala

如何为scala安装antlib.xml以使ant工作?

现在,当我ant在包含scala任务的build.xml文件上运行时遇到以下错误.

[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.
Run Code Online (Sandbox Code Playgroud)

我已经解开了,scala-2.8.1.final/lib/scala-compiler.jar但我不知道在哪里放内容.

以下是build.xml中相应的ant代码片段:

  <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>
Run Code Online (Sandbox Code Playgroud)

回答

以下代码在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>
Run Code Online (Sandbox Code Playgroud)

我的问题是$SCALA_HOME环境变量(${env.SCALA_HOME})指向了错误的位置(一个级别太深:/usr/local/scala-2.8.1.final/bin/而不仅仅是/usr/local/scala-2.8.1.final/,因此lib无法找到目录.

Hel*_*ann 20

antlib.xml包含在scala-compiler.jar中.你必须把它放到你的类路径中.要定义scalacant任务,请将以下定义放入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>
Run Code Online (Sandbox Code Playgroud)

要使用该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>
Run Code Online (Sandbox Code Playgroud)

我希望,这有帮助.

  • 我必须在scala-compiler.jar行之前添加它:`<pathelement location ="$ {scala.home} /lib/scala-reflect.jar"/>`(使用Scala 2.10 RC2).在此之前我得到了这个错误:`找不到类scala.tools.ant.FastScalac所需的类:scala/reflect/internal/settings/MutableSettings $ SettingValue使用类加载器AntClassLoader` (5认同)