gpt4 book ai didi

scala - 为scala配置ant

转载 作者:行者123 更新时间:2023-12-03 12:50:47 24 4
gpt4 key购买 nike

如何为scala安装antlib.xml,以使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.


我已经取消了 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>


回答

以下代码对于在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目录。

最佳答案

antlib.xml包含在scala-compiler.jar中。您必须将其放入类路径中。要定义scalac ant任务,请将以下定义放入您的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>


我希望这会有所帮助。

关于scala - 为scala配置ant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4665635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com