使用ANT编译和打包scala程序

首先系统中需要指定SCALA_HOME的环境变量

下面是build.xml文件

<?xml version="1.0" encoding="utf-8"?> 

<project name="HelloWorld" default="jar">

<!-- global properties -->
        <property name="scala.home" value="/data1/local/scala-2.10.3" />
        <property name="scala.library" value="${scala.home}/lib/scala-library.jar" />
        <property name="src.dir" value="src" />
        <property name="build.dir" value="bin/classes"/>
        <property name="lib" value="lib"/>
        <property name="jarname" value="jar/MLlibCF.jar"/>
        <property name="mainclass" value="src.spark.MLlibCF.MLlibCF"/>

<!-- class path settings -->
<path id="build.classpath">
    <fileset dir="${lib}">
        <include name="*.jar"/>
    </fileset>
    <pathelement location="${build.dir}" />
    <pathelement location="${scala.library}" />
</path>



<!-- define scalac, fsc, sbaz ... tasks -->
<target name="scala.tasks">
    <taskdef resource="scala/tools/ant/antlib.xml">
            <classpath>
            <pathelement location="${scala.home}/lib/scala-reflect.jar"   />
            <pathelement location="${scala.home}/lib/scala-compiler.jar" />
            <pathelement location="${scala.library}" />
            </classpath>
    </taskdef>
</target>

<target name="compile" depends="scala.tasks">
      <mkdir dir="${build.dir}"/>
      <fsc srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath">
        <include name="**/*.scala" />
      </fsc>
</target>

        <!--jar-->
    <target name="build" depends="compile" description="make jar file">

        <pathconvert property="libs" pathsep=" ">
                <mapper>
                <chainedmapper>                               
                <!-- jar包文件只留文件名,去掉目录信息 -->

                  <flattenmapper/>

                  <!-- add lib/ prefix -->

                 <globmapper from="*" to="../lib/*"/>

                </chainedmapper>
                </mapper>
            <path refid="build.classpath"/>            
       </pathconvert>



        <jar destfile="${jarname}" basedir="${build.jar}">
          <manifest>
               <attribute name="Main-class" value="${mainclass}"/>
               <attribute name="Class-Path" value="${libs}"/>
          </manifest>
        </jar>
    </target>

</project>

将项目中需要编译的类准备好,就可以编译并打好包了

你可能感兴趣的:(使用ANT编译和打包scala程序)