2011-01-12 24 views
19

Làm cách nào để cài đặt antlib.xml cho scala để nhận kiến ​​hoạt động?định cấu hình kiến ​​cho scala

Ngay bây giờ tôi gặp phải lỗi sau khi tôi chạy ant trên tệp build.xml chứa các tác vụ scala.

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

Tôi chưa hủy scala-2.8.1.final/lib/scala-compiler.jar nhưng tôi không biết đặt nội dung ở đâu.

Dưới đây là đoạn mã ant tương ứng từ 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> 

trả lời

Các mã sau đây là rất quan trọng để có trong file build.xml của bạn:

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

Vấn đề của tôi là biến môi trường $SCALA_HOME (${env.SCALA_HOME}) đã trỏ đến sai địa điểm (một cấp quá sâu: /usr/local/scala-2.8.1.final/bin/ thay vì chỉ /usr/local/scala-2.8.1.final/ và do đó không thể tìm thấy thư mục lib.

+0

Có lẽ bạn nên đăng đoạn mã ant có liên quan. Có lẽ bạn không có một typedef cho scalac trong build.xml của bạn – Raghuram

Trả lời

20

Antlib.xml được chứa trong tệp scala-compiler.jar. Bạn phải đặt nó vào classpath của bạn. Để xác định các nhiệm vụ kiến ​​scalac, đặt định nghĩa sau đây vào kiến ​​xây dựng tập tin của bạn (điều này được thực hiện dưới hình thức 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> 

Để sử dụng nhiệm vụ scalac, thêm thuộc tính depends="init" đến nhiệm vụ của bạn, ví dụ

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

Tôi hy vọng điều đó sẽ hữu ích.

+5

Tôi đã phải thêm điều này trước dòng scala-compiler.jar: ' ' (Sử dụng Scala 2.10 RC2). trước khi tôi nhận được lỗi này: 'Một lớp học cần thiết bởi lớp scala.tools.ant.FastScalac không thể tìm thấy: scala/reflect/internal/settings/MutableSettings $ SettingValue bằng cách sử dụng trình nạp lớp AntClassLoader' –

+1

Khi cài đặt scala bằng Brew, bạn có thể cần thay đổi/lib/thành/libexec/lib / –

Các vấn đề liên quan