如何在我的 Ant 构建中包含外部 jar 库 [英] How to include an external jar lib in my Ant build

查看:43
本文介绍了如何在我的 Ant 构建中包含外部 jar 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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>

这是我的目录结构:

/构建/lib/魔法文件夹/源/build.xml

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

文件夹 src 包含 .java 文件.

Folder src contains .java files.

MagicFolder 的路径应该是一个输入参数.

Path to MagicFolder should be an input parameter.

lib 有外部 .jar 库,应该包含在我的构建中.

lib has external .jar library which should be included in my build.

build 文件夹将编译.jar 和.class` 文件

build folder which will have compiled .jar and.class` files

问题:我应该如何更改我的 build.xml?我的 build.xml 应该:

QUESTION: How should I change my build.xml? My build.xml should:

  1. 添加外部库 ./lib/jbl.jar
  2. 当我运行我的应用程序时,为我的应用程序输入 2 个输入参数

推荐答案

如果你需要在 classpath 中添加一个 jar 来编译代码(抱歉,你的要求不是很清楚),那么你需要将 任务更改为如下所示:

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:

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

或者如果您需要将 jbl.jar 的内容添加到您正在创建的 jar 中,那么您需要将您的 任务更改为如下所示:

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:

<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:

<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>

这篇关于如何在我的 Ant 构建中包含外部 jar 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆