2012-08-17 70 views
3

我想用ant建立一個「uberjar」,並且該項目有一個配置目錄和幾個屬性文件。如何將包含屬性文件的目錄添加到Ant build.xml中?

如何將配置目錄添加到build.xml?

下面是它是如何引用的例子:

static private String CONFIG_DIR = "config/"; 
static public String CONFIG_FILE = "proj.properties"; 

File configFile = new File(CONFIG_DIR, CONFIG_FILE); 

有config目錄多個屬性文件。通常我只會把它添加到類路徑:

java -classpath bin:lib/*:config some.project.Example 

這裏是我的build.xml的目標,這是不完全正確:

<target name="uberjar" depends="compile"> 
    <jar jarfile="${babelnet.jar}"> 
    <fileset dir="${bin.dir}"> 
     <include name="**/*.class"/> 
    </fileset> 
    <zipgroupfileset dir="lib" includes="*.jar"/> 
    </jar> 
    <copy todir="config"> 
    <fileset dir="config"> 
     <include name="**/*.properties"/> 
    </fileset> 
     </copy> 
</target> 
+0

你應該考慮加載文件,從classpath ressources:http://stackoverflow.com/questions/3294196/load-resource-from -where-class-class – oers 2012-08-17 14:47:01

+1

如果可能,我寧願不修改代碼 - 這是我正在評估的外部項目。 – espeed 2012-08-17 14:51:37

+0

@espeed:你是否試圖將屬性文件添加到jar中的config目錄中? – 2012-08-17 15:21:55

回答

4

要包括的屬性的新的JAR文件中的文件,你可以添加另一個嵌套<fileset><jar>任務。

<target name="uberjar" depends="compile"> 
    <jar jarfile="${babelnet.jar}"> 
    <fileset dir="${bin.dir}"> 
     <include name="**/*.class"/> 
    </fileset> 
    <zipgroupfileset dir="lib" includes="*.jar"/> 
    <fileset dir="${basedir}"> 
     <include name="config/*.properties"/> 
    </fileset> 
    </jar> 
</target> 

而且,看到計算器問題:How to read a file from a jar file?

+0

我試過了,但'new File (CONFIG_DIR,CONFIG_FILE);'沒有找到該文件。 – espeed 2012-08-17 16:30:59

+0

@espeed:請參閱*** [如何從jar文件讀取文件?](http://stackoverflow.com/q/2271926/1164465)*** – 2012-08-17 16:31:48

+0

謝謝。我試圖在不修改項目代碼的情況下進行這項工作 - 這是一個外部項目。 – espeed 2012-08-17 16:36:18

相關問題