在 ant build.xml 中包含 Jar
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
那些 jar 在库中。
我曾尝试将此添加到 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
将 jars 添加到类路径,但是你需要在你的编译目标 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>
我正在尝试使用 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 在库中。
我曾尝试将此添加到 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
将 jars 添加到类路径,但是你需要在你的编译目标 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>