2014-10-08 61 views
1

我想把一些類放在一個jar中,用作庫。用Ant創建java庫

對於這個測試,我想要有一個類,apiInterface,它在包project.api

我這裏想我用的是庫錯誤的,因爲我得到的「包project.api不存在」在導入線,但This answer暗示的jar tf project.jar輸出應該是

project/api/apiInterface.class 

而我得到

apiInterface.class 
apiInterface.java 

被包含的java文件的目的是,但沒有必要。

我build.xm

<target name="buildAPI" depends="compile">     
    <mkdir dir="${jar}" />         
    <jar destfile="${jar}/${ant.project.name}-api.jar"> 
     <fileset dir="${bin}/${ant.project.name}/api"/> 
     <fileset dir="${src}/${ant.project.name}/api"/> 
    </jar>             
</target> 

成功編譯的相關部分編譯Java並把.class文件在斌/項目/ API。

回答

1

Jar文件將包含fileset元素中指定的目錄下的文件列表。由於您將該目錄指定爲${bin}/${ant.project.name}/api,因此該任務將僅在該目錄下進行搜索並將該類文件包括在Jar中,即${bin}/${ant.project.name}/api/apiInterface.class

要包含與包相對應的目錄,只需將dir屬性更改爲指向根文件夾(在本例中爲${bin})。相同的源文件。

<jar destfile="${jar}/${ant.project.name}-api.jar"> 
    <fileset dir="${bin}"/> 
    <fileset dir="${src}"/> 
</jar> 

如果只有這個包應該包含(即其他包不應該在罐),使用includes屬性:

<jar destfile="${jar}/${ant.project.name}-api.jar"> 
    <fileset dir="${bin}" includes="${ant.project.name}/api/*.class" /> 
    <fileset dir="${src}" includes="${ant.project.name}/api/*.java" /> 
</jar> 

您可以檢查從Ant手冊this鏈接如何正確使用這個任務。

+0

如何排除bin和src中除api目錄外的所有內容? – mtfurlan 2014-10-08 18:46:52

+0

@Scuzzball更新了一個例子。 – manouti 2014-10-08 18:50:53