tags:

views:

107

answers:

1

I'm using ant to generate the MANIFEST.MF for a .jar, and I need to add multiple manifest <section> blocks based on a list of files in a directory. However, I need to automate the process to do this at build-time since the list will change between development and deployment.

For example:

<manifest file="MANIFEST.MF">
  <foreach files="./*">
    <section name="section">
      <attribute name="Attribute-Name" value="$file"/>
    </section>
  </foreach>
</manifest>

I've looked at foreach from Ant-contrib but it doesn't look like it will work in this instance.

Is this possible?

+3  A: 

You can do this with the Manifest task

<manifest file="MANIFEST.MF">
    <section name="section">
        <attribute name="Attribute-Name" value="value"/>
    </section>
    <section name="section/class1.class">
        <attribute name="Second-Attribute-Name" value="otherValue"/>
    </section>
</manifest>

It will generate this manifest :

Manifest-Version: 1.0
Created-By: Apache Ant 1.7

Name: section
Attribute-Name: value

Name: section/class1.class
Second-Attribute-Name: otherValue

You can maintain two different custom tasks to handle the different cases, and call the right one at the right moment.


For an "automatic" management :

<target name="manifest-generation">
    <foreach param="file" target="manifest">
        <path>
            <fileset dir=".">
                <include name="**/*.class"/>
            </fileset>
        </path>
    </foreach>
</target>

<target name="manifest">
    <manifest file="MANIFEST.MF" mode="update">
        <section name="${file}">
            <attribute name="Attribute-Name" value="value"/>
        </section>
    </manifest>
</target>
Colin Hebert
if this is not flexible enough, then you most probably need to write your own custom task
Neeme Praks
Yes, I know I can do it manually, but that's not what I'm asking. I'm asking how to **automate** adding multiple sections based on files in the filesystem.
digitala
Then what do you mean by "automate"? What result do you want?
Colin Hebert
@Colin: updated question with an example.
digitala
@digitala, I updated with the solution.
Colin Hebert
Thanks colin! With some tweaks I've got that working how I need it.
digitala
@digitala, glad it worked. But be careful with the `mode="update"` you might want to remove the manifest file from time to time (or you'll keep old data)
Colin Hebert
@Colin: yep, in another section I'm always creating a new file, prepending some items, then using your example to update multiple sections. Cheers!
digitala