简体   繁体   English

如何在我的Ant构建中包含外部jar库

[英]How to include an external jar lib in my Ant build

I have the following build.xml : 我有以下build.xml

<project>

<target name="clean">
    <delete dir="./build"/>
</target>

<target name="compile">
    <mkdir dir="./build/classes"/>          
    <javac srcdir="./src" destdir="./build/classes"/>                   
</target>

<target name="jar">
    <mkdir dir="./build/jar"/>
    <jar destfile="./build/jar/DependencyFinder.jar" basedir="./build/classes">
        <manifest>
            <attribute name="DependencyFinder" value="main"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <java jar="./build/jar/DependencyFinder.jar" classname="${main-class}" fork="true"/>                    
</target>

</project>

Here is my directory structure: 这是我的目录结构:

/build /lib /MagicFolder /Src /build.xml / build / lib / MagicFolder / Src /build.xml

Folder src contains .java files. Folder src包含.java文件。

Path to MagicFolder should be an input parameter. MagicFolder路径应该是输入参数。

lib has external .jar library which should be included in my build. lib有外部.jar库,应该包含在我的构建中。

build folder which will have compiled .jar and .class` files build将编译.jar and .class`文件的文件夹

QUESTION: How should I change my build.xml ? 问题:我应该如何更改build.xml My build.xml should: 我的build.xml应该:

  1. Add external lib ./lib/jbl.jar 添加外部lib ./lib/jbl.jar
  2. When I run my application put 2 input parametrs for my application 当我运行我的应用程序时,为我的应用程序输入2个输入参数

If you need to add a jar to classpath to compile the code (sorry, it isn't quite clear what you're asking for), then you need to change <javac> task to look like this: 如果你需要在classpath中添加一个jar来编译代码(抱歉,你要求的内容还不是很清楚),那么你需要将<javac>任务更改为如下所示:

<javac srcdir="./src" destdir="./build/classes">   
    <classpath>
        <pathelement path="lib/jbl.jar"/>
    </classpath>
</javac>

Or if you need to add contents of jbl.jar to the jar you are creating, then you need to change your <jar> task to look like this: 或者,如果您需要将jbl.jar内容添加到您正在创建的jar中,那么您需要将<jar>任务更改为如下所示:

<jar destfile="./build/jar/DependencyFinder.jar" basedir="./build/classes>
    <zipgroupfileset dir="lib" includes="jbl.jar" />
    <manifest>
        <attribute name="DependencyFinder" value="main"/>
        <attribute name="Main-Class" value="org.ivanovpavel.YourMainClass"/>
    </manifest>
</jar>

To add arguments to <java> call, use nested <arg> elements: 要向<java>调用添加参数,请使用嵌套的<arg>元素:

<target name="run">
    <java jar="./build/jar/DependencyFinder.jar:lib/jbl.jar" classname="dependencyfinder.DependencyFinder">  
        <arg value="Alexander Rosenbaum"/>
        <arg value="Dmitry Malikov"/>
    </java>                  
</target>

There are two ways to run a java program. 有两种方法可以运行java程序。 Using the "jar" option is the most convenient and is called an executable jar, however in order to make it work you need to specify both the Main class and classpath in the manifest file as follows: 使用“jar”选项是最方便的并且称为可执行jar,但是为了使其工作,您需要在清单文件中指定Main类和classpath,如下所示:

<jar destfile="${jar.file}" basedir="${classes.dir}">
    <manifest>
        <attribute name="Main-Class" value="${jar.main.class}" />
        <attribute name="Class-Path" value="${jar.classpath}" />
    </manifest>
</jar>

For a more detailed answer on how to do this see: 有关如何执行此操作的更详细的答案,请参阅:

Execute Java programs in a consistent environment 在一致的环境中执行Java程序

try with this: 试试这个:

<target name="jar" depends="clean,compile" >
   <jar destfile="./build/jar/DependencyFinder.jar">
    <fileset dir="./lib" includes="jbl.jar,mysql*.jar" />
    <fileset dir="./build/classes" excludes="**/form/*.class,**/orm/*.class,**/org/w3/xmldsig/*.class"/>
   </jar>
</target>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM