Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

build - ANT read existing MANIFEST version and append to it

I need to script an ant build which reads in the existing version from a META-INF/manifest.mf file and appends to it.

The updating should be possible using the ANT manifest task, however I've had trouble reading the existing version.

As the manifest entries are use key: value rather than key= value I cant read them in using ANT's loadproperties task.

Has anyone done this/ have any ideas?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to be careful using <loadproperties> on a manifest: though it appears to work with short values, it fails when the line-length exceeds 70 characters because of the odd way manifest entries wrap. The resulting values are truncated.

I wrote a <scriptdef> that does what you ask, though it's not fully tested yet.

<!--
    Loads entries from a manifest file.

    @jar     The jar from where to read
    @file    A manifest file to read
    @prefix  A prefix to prepend
    @section The name of the manifest section to load
-->
<scriptdef name="loadmf" language="javascript" loaderRef="sharedbuild-loaderRef">
    <attribute name="jar" />
    <attribute name="file" />
    <attribute name="prefix" />
    <attribute name="section" />
    <![CDATA[
        var jarname = attributes.get("jar");
        var filename = attributes.get("file");
        if (jarname != null && filename != null) {
            self.fail("Only one of jar or file is required");
        }
        var prefix = attributes.get("prefix");
        if (prefix == null) {
            prefix = "";
        }
        var section = attributes.get("section");

        var manifest;
        if (jarname != null) {
            var jarfile = new java.util.jar.JarFile(new java.io.File(jarname));
            manifest = jarfile.getManifest();
        } else if (filename != null) {
            manifest = new java.util.jar.Manifest(new java.io.FileInputStream(new java.io.File(filename)));
        } else {
            self.fail("One of jar or file is required");
        }

        if (manifest == null) {
            self.log("No manifest in " + jar);
        } else {
            var attributes = (section == null) ? manifest.getMainAttributes() : manifest.getAttributes(section);
            if (attributes != null) {
                var iter = attributes.entrySet().iterator();
                while (iter.hasNext()) {
                    var entry = iter.next();
                    project.setProperty(prefix + entry.getKey(), entry.getValue());
                }
            }
        }
    ]]>
</scriptdef>

I'm sure the JavaScript can be improved--I'm no expert--but it seems to work well-enough for me (running AntUnit tests to make sure my OSGi manifests are created correctly.) As an added bonus it loads from either a jar (or ear or war) file or a stand-alone manifest file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...