Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

configure ant for scala

How do I install the antlib.xml for scala to get ant working?

Right now I encounter the following error when I run ant on a build.xml file that contains scala tasks.

[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.

I have unjarred the scala-2.8.1.final/lib/scala-compiler.jar but I don't know where to put the contents.

Here is the corresponding ant code snippet from the 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>

Answer

The following code is important to have in your build.xml file:

  <!-- 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>

My problem was that the $SCALA_HOME environment variable (${env.SCALA_HOME}) was pointing to the wrong place (one level too deep: /usr/local/scala-2.8.1.final/bin/ rather than just /usr/local/scala-2.8.1.final/, and therefore the lib directory could not be found.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The antlib.xml is contained in the scala-compiler.jar. You have to put it into your classpath. To define the scalacant task, put the following definition into your ant build file (this is taken form 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>

To use the scalac task, add the attribute depends="init" to your task, e.g.

<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>

I hope, that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...