簡體   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