How to include an external jar lib in my Ant build

后端 未结 3 1697
时光取名叫无心
时光取名叫无心 2021-01-02 02:56

I have the following build.xml:




    




        
相关标签:
3条回答
  • 2021-01-02 03:05

    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

    0 讨论(0)
  • 2021-01-02 03:12

    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>
    
    0 讨论(0)
  • 2021-01-02 03:18

    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>
    

    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>
    
    0 讨论(0)
提交回复
热议问题