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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…