javaantjarant-contribmeta-inf

Ant Contrib If for optional JAR META-INF


I have a scenario where I'm trying to reuse the same build.xml file across multiple projects. In each case, the finaly target is a dist that JARs everything up. The only difference is that some of the projects have a src/main/java/META-INF/* directory, and others don't (and just have a src/main/java/* directory).

I want to use the Ant-Contrib <if/> task to optionally define a META-INF/ directory if the build sees a src/main/java/META-INF/* directory available. So something like this:

<jar jarfile="myapp.jar">
    <if>
        <available file="src/main/java/META-INF" />
        <then>
            <!--
                Copy everything from src/main/java/META-INF into
                the JAR's META-INF directory.

                But how?!?
            -->
            <echo message="I'm trying to copy META-INF!" />
        </then>
    </if>
</jar>

But I am choking on two things here:

  1. When I run this target, I get a build exception stating that I can't nest an <if/> inside the <jar/> task; and
  2. I'm not sure how to configure the <jar/> task to create a META-INF directory at the root of the classpath, and copy all of the src/main/java/META-INF contents into it.

Any thoughts? Thanks in advance.


Solution

  • It sounds like you want to take advantage of a more modular build configuration. How about having two targets:

    <target name="jar">
        <!-- Normal JAR tasks like you currently have (sans the metainf stuff). -->
    </target>
    
    <target name="jar-with-metainf">
        <jar destfile="test.jar">
            <fileset dir="classes"/>
            <metainf dir="src/main/java/META-INF"/>
        </jar>
    </target>
    

    Then, when you want to build your META-INF/-containing projects, you just run jar-with-metainf.