12

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

这是我的目录结构:

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

文件夹src包含.java文件。

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

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

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

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

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

3 回答 3

39

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

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

或者,如果您需要将内容添加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>

要为<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>
于 2013-10-29T12:27:08.327 回答
4

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:

<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

于 2013-10-29T17:37:37.590 回答
2

试试这个:

<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>
于 2017-03-29T14:59:39.760 回答