如何读取现有 jar 的清单,并使用 Ant 附加到其类路径

发布于 2024-12-10 03:22:48 字数 141 浏览 0 评论 0原文

我想向 Ant 脚本添加一个目标,该目标读取 jar 的清单并在末尾附加一个新的 jar。我查看了 loadproperties 任务,它看起来很接近,但是当类路径分成多行时无法处理。那么有人知道开箱即用的 Ant 任务是否可以实现这一点吗?

I want to add a target to my Ant script that reads in a jar's manifest and appends a new jar at the end. I've looked at loadproperties task, and it seemed close, but unable to handle when the classpath is split up among multiple lines. So does anyone know if this is possible with the out-of-the-box Ant tasks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦旅人picnic 2024-12-17 03:22:48

update 模式下的 清单任务 似乎是显而易见的回答。

The manifest task in update mode would seem the obvious answer.

我早已燃尽 2024-12-17 03:22:48

基于此处的代码,我将其修改为在现有的类路径中读取,追加最后创建一个新的 jar 文件,然后将其保存到属性中。此时,使用清单任务很容易写回。

public void execute() throws BuildException {
    validateAttributes();

    checkFileExists();

    JarFile jarFile = null;     
    Manifest manifest = null;

    try {
        jarFile = new JarFile(directory + "/" + jar);
        manifest = jarFile.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String currClasspath = attributes.getValue("Class-Path");

        String newClasspath = currClasspath.concat(" ").concat(append);

        getProject().setProperty(propertyName, newClasspath);           
    } catch (IOException e) {
        throw new BuildException();
    } finally {
        if (manifest != null) {
            manifest = null;
        }
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {}
            jarFile = null;
        }

    }
}

由于空间原因,省略了 getters/setters/utility 方法。那么 Ant 代码如下所示:

<target name="addToClasspath" depends="build">
    <property name="testPath" value="C:/"/>     

    <taskdef name="manifestAppender" classname="ClasspathAppender" />

    <manifestAppender dir="${testPath}" jar="wbclasspath.jar" append="test.jar" property="newClasspath" />

    <echo>Manifest class-path: ${newClasspath}</echo>

    <jar destfile="${testPath}/wbclasspath.jar">
        <manifest>
            <attribute name="Class-Path" value="${newClasspath}" />
        </manifest>
    </jar>      
</target>

Based on the code here, I modified it to read in the existing classpath, append a new jar file at the end and then save it to a property. At that point, it's easy to write back out using the manifest task.

public void execute() throws BuildException {
    validateAttributes();

    checkFileExists();

    JarFile jarFile = null;     
    Manifest manifest = null;

    try {
        jarFile = new JarFile(directory + "/" + jar);
        manifest = jarFile.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String currClasspath = attributes.getValue("Class-Path");

        String newClasspath = currClasspath.concat(" ").concat(append);

        getProject().setProperty(propertyName, newClasspath);           
    } catch (IOException e) {
        throw new BuildException();
    } finally {
        if (manifest != null) {
            manifest = null;
        }
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {}
            jarFile = null;
        }

    }
}

The getters/setters/utility methods omitted for space. Then the Ant code looks like this:

<target name="addToClasspath" depends="build">
    <property name="testPath" value="C:/"/>     

    <taskdef name="manifestAppender" classname="ClasspathAppender" />

    <manifestAppender dir="${testPath}" jar="wbclasspath.jar" append="test.jar" property="newClasspath" />

    <echo>Manifest class-path: ${newClasspath}</echo>

    <jar destfile="${testPath}/wbclasspath.jar">
        <manifest>
            <attribute name="Class-Path" value="${newClasspath}" />
        </manifest>
    </jar>      
</target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文