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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19642388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:03:40  来源:igfitidea点击:

How to include an external jar lib in my Ant build

javaantbuild

提问by pipsik

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 srccontains .javafiles.

文件夹src包含.java文件。

Path to MagicFoldershould be an input parameter.

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

libhas external .jar library which should be included in my build.

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

buildfolder which will have compiled .jar and.class` files

build将编译.jar and.class` 文件的文件夹

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

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

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

采纳答案by gvlasov

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:

如果您需要在类路径中添加一个 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.jarto 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>

回答by Mark O'Connor

There are two ways to run a java program. 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:

有两种方法可以运行 Java 程序。使用“jar”选项是最方便的,称为可执行 jar,但是为了使其工作,您需要在清单文件中指定 Main 类和类路径,如下所示:

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

回答by RaZieRSarE

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>