繁体   English   中英

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

[英]Netbeans - adding resource files to jar file with Ant

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

我认为我正在寻找的答案如下:

在build.xml文件中(根据trashgod的回答),您可以使用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>

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

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

对于更高级的任务,请检查为新创建的项目生成的缺省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

附录:作为示例 ,此目标会覆盖-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>

输出:

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

compile:

BUILD SUCCESSFUL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM