java Netbeans - 使用 Ant 将资源文件添加到 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3955299/
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-10-30 04:07:32  来源:igfitidea点击:

Netbeans - adding resource files to jar file with Ant

javanetbeansantresourcesjar

提问by Alistair Collins

I want add some resource files (non-code text-based files) to the jar file in a Java project using Netbeans 6.9, I'd expect using Ant. I had thought that this would be reasonably simple...but after quite a bit of searching I can't find how to do it..! Any pointers in the right direction?

我想在使用 Netbeans 6.9 的 Java 项目中将一些资源文件(基于非代码文本的文件)添加到 jar 文件中,我希望使用 Ant。我原以为这会相当简单......但经过相当多的搜索后,我找不到如何去做......!任何指向正确方向的指针?

回答by Alistair Collins

The answer I think I was looking for is as follows:

我想我正在寻找的答案如下:

In the build.xml file (as per trashgod's answer) you can use the hooks already in the ant build script to add the following:

在 build.xml 文件中(根据垃圾神的回答),您可以使用 ant 构建脚本中已有的钩子来添加以下内容:

<target name="-post-jar">
    <echo>Adding files to jar...</echo>
    <jar destfile="dist/jarFileName.jar" update="true">
        <fileset dir="${basedir}">
            <include name="files/*"/>
        </fileset>
    </jar>
</target>

This adds the files directory and any files under it directly to the jar file.

这会将 files 目录及其下的任何文件直接添加到 jar 文件中。

回答by trashgod

If you choose File > Project Properties > Build > Packaging, you'll see a dialog that lets you exclude artifacts from the build; everything else is the source tree is included. The source of TreeIconDemois a concrete example that inlcudes htmlfiles.

如果选择File > Project Properties > Build > Packaging,您将看到一个对话框,可让您从构建中排除工件;其他一切都包括源树。的来源TreeIconDemo是一个包含html文件的具体示例。

For more advanced tasks, examine the default build.xmlgenerated for a freshly created project; it identifies various hooks into the predefined tasks. For example,

对于更高级的任务,检查build.xml为新创建的项目生成的默认值;它将各种挂钩识别到预定义的任务中。例如,

There exist several targets which are by default empty and which can be 
used for execution of your tasks. These targets are usually executed 
before and after some main targets. They are: 

  -pre-init:                 called before initialization of project properties
  -post-init:                called after initialization of project properties
  -pre-compile:              called before javac compilation
  -post-compile:             called after javac compilation
  -pre-compile-single:       called before javac compilation of single file
  -post-compile-single:      called after javac compilation of single file
  -pre-compile-test:         called before javac compilation of JUnit tests
  -post-compile-test:        called after javac compilation of JUnit tests
  -pre-compile-test-single:  called before javac compilation of single JUnit test
  -post-compile-test-single: called after javac compilation of single JUunit test
  -pre-jar:                  called before JAR building
  -post-jar:                 called after JAR building
  -post-clean:               called after cleaning build products

Addendum: As an example, this target overrides -post-compileto print some statistics.

附录:例如,此目标覆盖-post-compile以打印一些统计信息。

<project name="TreeIconDemo" default="default" basedir=".">
    <import file="nbproject/build-impl.xml"/>
    <target name="-post-compile">
        <echo>build.dir: ${build.dir}</echo>
        <length mode="all" property="build.size">
            <fileset dir="${build.dir}">
              <include name="**/*"/>
            </fileset>
        </length>
        <echo>build.size: ${build.size}</echo>
    </target>
</project>

Output:

输出:

$ ant compile
Buildfile: build.xml
...
-post-compile:
     [echo] build.dir: build
     [echo] build.size: 11992

compile:

BUILD SUCCESSFUL