如何在ANT脚本中包含MongoDB jar?我应该使用什么样的标签来包含罐子?

时间:2015-01-15 09:09:41

标签: java mongodb ant classpath pathelement

每当我尝试通过 ANT脚本运行我的代码时,我都会收到此错误" 错误: 包com.mongodb不存在" 。当我在 Eclipse 中运行我的代码时,它成功执行,当我通过 ANT 脚本执行我的代码时,我收到此错误。我在项目中包含了mongoDB jar。

错误 - "包com.mongodb不存在"

build.xml -

<!-- 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="dist.dir" location="dist" />
<property name="docs.dir" location="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>

<path id="master-classpath">
    <pathelement path="./lib/mongo-2.10.1.jar" >
        </pathelement>
</path>


<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false">
        <classpath refid="master-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 -->
        <fileset dir="${src.dir}">
            <include name="**" />
        </fileset>
    </javadoc>
</target>

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

<target name="main" depends="compile, jar, docs">
    <echo>Hello Ankur - Welcome to Apache Ant!</echo>
    <description>Main target</description>
</target>

1 个答案:

答案 0 :(得分:1)

只需在ant脚本中添加类路径引用。您可以看到名为

的标记
  <classpath>

在此标记内,您可以找到另一个标记

<pathelement path="./lib/rt.jar" />

添加另一个pathelement标记并放入mongodb jar名称。 如: - <pathelement path="./lib/com.mongodb.jar" />

并确保相应的jar也应存在于lib文件夹中。 我认为这样可以正常工作。试试看,让我知道。