在ant build.xml中包含Jar

时间:2015-01-28 19:20:02

标签: java ant

我正在尝试使用build.xml脚本编译我的项目。目前它看起来像这样:

<?xml version="1.0"?>
<project name="vml" default="main" basedir=".">
  <!-- Sets variables which can later be used. -->
  <!-- The value of a property is accessed via ${} -->
  <property name="src.dir" location="src" />
  <property name="build.dir" location="bin" />
  <property name="libs.dir" location="libs" />
  <property name="dist.dir" location="build/jar" />
  <property name="docs.dir" location="build/docs" />

  <!-- Deletes the existing build, docs and dist directory-->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <!-- Creates the  build, docs and dist directory-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

  <!-- Compiles the java code (including the usage of library for JUnit -->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
    </javac>

  </target>

  <!-- Creates Javadoc -->
  <target name="docs" depends="compile">
    <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
      <!-- Define which files / directory should get included, we include all -->
       <fileset dir="${src.dir}">
                <include name="**" />
           </fileset>
    </javadoc>
  </target>

  <!--Creates the deployable jar file  -->
  <target name="jar" depends="compile">
    <jar destfile="${dist.dir}\test.jar" basedir="${build.dir}">
      <manifest>
        <attribute name="Main-Class" value="vml.coding.test.Test" />
      </manifest>
    </jar>
  </target>

  <target name="main" depends="compile, jar, docs">
    <description>Main target</description>
  </target>

</project> 

它没有用,因为我使用了几个外部库(.jar),所以我得到了这个编译错误:

error: package com.opencsv does not exist
    [javac] import com.opencsv.CSVReader;

这是我的文件夹层次结构:

project
|
|-src
|-build
|-libs

那些jar在libs里面。

我尝试将此添加到build.xml中,但没有运气:

    ...
    <path id="build-classpath">
      <fileset dir="${libs.dir}">
        <include name="*.jar"/>
      </fileset>
    </path>
    ...
    <target name="jar" depends="compile">
      <jar destfile="${dist.dir}\test.jar" basedir="${build.dir}">
        <manifest>
          <attribute name="Main-Class" value="vml.coding.test.Test" />
          <attribute name="Build-Path" value="${build-classpath}" />
        </manifest>
      </jar>
    </target>

我该如何解决?

2 个答案:

答案 0 :(得分:1)

问题出现在编译目标而不是jar目标。

你是对的,你需要使用path将jar添加到类路径中,但是你需要在编译目标javac任务中引用它:

<javac srcdir="${src.dir}" destdir="${build.dir}" 
            classpathref="build-classpath"
 />

您可能还想构建构建目录,以便类可以互相引用?

<path id="build-classpath">
  <fileset dir="${libs.dir}">
    <include name="*.jar"/>
  </fileset>
  <pathelement path= "${build.dir}"/>
</path>

当您尝试执行jar时,您需要确保这些jar再次出现在类路径中。

答案 1 :(得分:0)

确定, dkatzel指出我正确的方向。最后,我设法让它工作,这就是它的样子:

<?xml version="1.0" encoding="UTF-8"?>
<project name="vml" default="main" basedir=".">
    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->
    <property name="src.dir" location="src" />
    <property name="build.dir" location="bin" />
    <property name="libs.dir" location="libs" />
    <property name="dist.dir" location="build/jar" />
    <property name="docs.dir" location="build/docs" />
    <path id="build-classpath">
        <fileset dir="${libs.dir}">
            <include name="*.jar" />
        </fileset>
    </path>
    <!-- Deletes the existing build, docs and dist directory-->
    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${docs.dir}" />
        <delete dir="${dist.dir}" />
    </target>
    <!-- Creates the  build, docs and dist directory-->
    <target name="makedir">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${docs.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>
    <!-- Compiles the java code (including the usage of library for JUnit -->
    <target name="compile" depends="clean, makedir">
        <javac srcdir="${src.dir}" destdir="${build.dir}">
            <classpath refid="build-classpath" />
        </javac>
    </target>
    <!-- Creates Javadoc -->
    <target name="docs" depends="compile">
        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
            <!-- Define which files / directory should get included, we include all -->
            <classpath refid="build-classpath" />
            <fileset dir="${src.dir}">
                <include name="**" />
            </fileset>
        </javadoc>
    </target>
    <!--Creates the deployable jar file  -->
    <target name="jar" depends="compile">
        <jar destfile="${dist.dir}\test.jar" basedir="${build.dir}">
            <zipgroupfileset dir="${libs.dir}" includes="**/*.jar" />
            <manifest>
                <attribute name="Main-Class" value="vml.coding.test.Test" />
                <attribute name="Build-Path" value="${build-classpath}" />
            </manifest>
        </jar>
    </target>
    <target name="main" depends="compile, jar, docs">
        <description>Main target</description>
    </target>
</project>