[英]Include Jar in ant build.xml
我正在尝试使用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
那些罐子在库里面。
我试过没有运气将其添加到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>
我该如何解决?
问题发生在您的编译目标而不是jar目标中。
正确的是,您需要使用path
将罐子添加到类路径,但是随后您需要在编译目标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再次位于类路径中。
好的,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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.