为 scala 配置 ant

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4665635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:43:15  来源:igfitidea点击:

configure ant for scala

scalaant

提问by dsg

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

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

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

现在,当我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.

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

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

Here is the corresponding ant code snippet from the build.xml:

这是来自 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>

Answer

回答

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

以下代码对于您的 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>

My problem was that the $SCALA_HOMEenvironment 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 libdirectory could not be found.

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

回答by Helmut Zechmann

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

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>

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

要使用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>

I hope, that helps.

我希望,这有帮助。