gpt4 book ai didi

java - Apache Ant : adding and using a . txt 文件

转载 作者:行者123 更新时间:2023-12-01 18:42:29 25 4
gpt4 key购买 nike

我正在使用 Apache Ant 来编译、jar 并运行学校的 java 项目。我遇到了一个问题,我不知道如何将单个 .txt 文件包含到我的 .jar 中,然后在我的 java 代码中引用它。

这是我的 build.xml 中的编译和 jar 命令。

    <target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
</jar>
</target>

我使用什么命令将 .txt 文件包含到我的 jar 中。我已经看到了一些关于它的 Stack Overflow 问题,特别是

adding non-code resources to jar file using Ant

How can I include data text files in a jar using Ant?

但是他们都没有真正解释如何包含文件。他们只是发出了对我来说并没有真正意义的命令。我试过了,

        <jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
<fileset dir="src/resources" />
</jar>

但这似乎没有帮助,因为我只是不理解 Ant,我找不到它的文档来真正解释为什么你在 ant 中做不同的事情。

我的 .txt 文件位于 src/resources 中。

最佳答案

<jar>任务任务多个文件集:

<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
<fileset dir="src/resources" />
</jar>

<manifest>任务是一个子实体。它非常类似于 <manifest> 任务。你的 jar 中有一个名为 META-INF/MANIFEST.MF 的文件这是向该 jar 添加另一个属性。这个特定的属性告诉 Jar 什么是主类,在执行 jar 时应该启动该类。

<fileset dir="src/resources"/> is adding all the files that are found in the资源目录。如果您只想添加特定文件怎么办?您可以使用selectors 。例如:

<jar destfile="build/jar/BinaryTree.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="cs3345.MainLauncher"/>
</manifest>
<fileset dir="src/resources">
<include name="foo.txt"/>
</fileset>
</jar>

这次我不会添加整个 resources目录树。我只是添加一个名为 foo.txt 的文件进入我的 jar 文件。

关于java - Apache Ant : adding and using a . txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19368614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com