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

How to include MongoDB jar in ANT script ? What kind of tags should I use to include the jar?

每当我尝试通过 ANT Script 运行 我的代码时,我都会收到此错误“ error: package com.mongodb does not exist" 。当我 运行 我的代码在 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>

只需在ant中添加class路径引用script.You即可看到一个名为

的标签
  <classpath>

在此标签中您可以找到另一个标签,

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

添加另一个 pathelement 标签并输入 mongodb jar 名称。 喜欢:-<pathelement path="./lib/com.mongodb.jar" />

并确保相应的 jar 也应该存在于您的 lib 文件夹中。 我认为这会起作用 fine.Try 并告诉我。