Java 使用 Ant 将非代码资源添加到 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1216744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:00:39  来源:igfitidea点击:

adding non-code resources to jar file using Ant

javaantjar

提问by denchr

I am in the process of packaging my java application into a jar file. I am using ant and eclipse. I need to actually include in the jar a couple of separate, non-code files (xml and txt files) directly under the root folder, not in the same place as the code.

我正在将我的 java 应用程序打包成一个 jar 文件。我正在使用蚂蚁和日食。我实际上需要在 jar 中直接在根文件夹下包含几个单独的非代码文件(xml 和 txt 文件),而不是与代码位于同一位置。

I am trying to use includesfile, but that doesn't seem to work, here is my ant target:

我正在尝试使用包含文件,但这似乎不起作用,这是我的蚂蚁目标:

<target name="distribute" depends="compile" includesfile="readthis.txt">
    <jar destfile="${distributionDir}/myjar.jar" >
        <fileset dir="${outputDir}"/>
    <fileset dir="${sourceDir}"/>           
    </jar>
</target>

The above works fine when I omit the includesfile argument. Plus, I need to add multiple non-code files. These files are located directly under the root project folder in Eclipse, and not within any java packages.

当我省略 includesfile 参数时,上述工作正常。另外,我需要添加多个非代码文件。这些文件直接位于 Eclipse 中的根项目文件夹下,而不是在任何 java 包中。

Also, as you can see above, I am basically including to the jar my source code as well. Is there a way to tell the jar task to put the source code in a separate folder from the compiled code?

此外,正如您在上面看到的,我基本上也将我的源代码包含在 jar 中。有没有办法告诉 jar 任务将源代码与编译代码放在一个单独的文件夹中?

As a compile or build task, Ant easily can separate source code from the compiled classes. But is there a way to do it within a jar file as well?

作为编译或构建任务,Ant 可以轻松地将源代码与已编译的类分开。但是有没有办法在 jar 文件中做到这一点?

Many thanks for your time!

非常感谢您的时间!

采纳答案by Jon Skeet

You shouldn't have an includesfileattribute in a targetto start with, and I suspect you've misunderstood the point of it anyway - the idea is that the file you specify with includesfilecontains patterns for which files to include, if you don't want to put them into your Ant file.

你不应该includesfile目标中有一个属性作为开始,我怀疑你无论如何都误解了它的意义 - 这个想法是你指定的文件includesfile包含要包含的文件的模式,如果你不想要将它们放入您的 Ant 文件中。

It strikes me that all you need is an extra fileset containing the appropriate files you need. For example, for readthis.txt:

令我震惊的是,您所需要的只是一个包含您需要的适当文件的额外文件集。例如,对于readthis.txt

<target name="distribute" depends="compile">
  <jar destfile="${distributionDir}/myjar.jar" >
    <fileset dir="${outputDir}"/>
    <fileset dir="${sourceDir}"/>
    <fileset file="readthis.txt" />
  </jar>
</target>

I would also suggest that you create a new directory for the resources, so you can do:

我还建议您为资源创建一个新目录,以便您可以:

<target name="distribute" depends="compile">
  <jar destfile="${distributionDir}/myjar.jar" >
    <fileset dir="${outputDir}"/>
    <fileset dir="${sourceDir}"/>
    <fileset dir="${resourcesDir}" />
  </jar>
</target>

with no hassle. This will also keep your directory structure cleaner, making it easier to find things from the top downwards.

没有麻烦。这也将使您的目录结构更清晰,更容易从上到下查找内容。