Search code examples
ant

How to read the Class-Path from a JAR in Ant?


The Class-Path of a JAR is written in the Manifest file in the JAR. The following Bash code reads the Class-Path from a JAR, if it does not exceed the 72 char limit:

unzip -c "$1" META-INF/MANIFEST.MF |
sed -n 's/^Class-Path: \(.\+\)$/\1/p' |
tr -d '\r'

Right now I am calling the code with exec in Ant but I would like to remove all execs.

How to do the same in Ant without using unzip, sed and tr?


Solution

  • You may want to try adding the following (in the beginning of your build.xml file where properties are defined) which puts the short Class-Path in property classpath:

    <loadresource property="classpath">
        <zipentry zipfile="demo.jar" name="META-INF/MANIFEST.MF"/>
        <filterchain>
            <tokenfilter>
                <containsregex pattern="^Class-Path: (.+)$" flags="i"/>
                <replaceregex pattern="^Class-Path: (.+)$" replace="\1" flags="i"/>
            </tokenfilter>
            <tokenfilter><!-- get rid of trailing line separator -->
                <filetokenizer/>
                <replaceregex pattern="(\r?\n)+" replace="" flags="m"/>
            </tokenfilter>
        </filterchain>
    </loadresource>
    

    Edit: If you put the following before the tokenfilter above, then it should also work for longer values of Class-Path (by first joining split lines):

    <tokenfilter>
        <filetokenizer/>
        <replaceregex pattern="\r?\n (.+)$" replace="\1" flags="m"/>
    </tokenfilter>