2013-04-10 243 views
28

通過build.xml文件中使用的代碼如下塊添加註釋文件

<propertyfile file="default.properties" comment="Default properties"> 
    <entry key="source.dir" value="1" /> 
    <entry key="dir.publish" value="1" /> 
    <entry key="dir.publish.html" value="1" /> 
</propertyfile> 

我能夠生成以下文件內容

source.dir=1 
dir.publish=1 
dir.publish.html=1 

default.properties文件我想知道我怎麼能在生成的文件中添加我的評論?例如。生成的屬性應具有以下內容:

# Default Configuration 
source.dir=1 
dir.publish=1 
# Source Configuration 
dir.publish.html=1 

我怎麼能做到這一點動態使用Ant的build.xml

+0

你需要在屬性的多個註釋文件?你能編輯你的問題,以反映你期望這些評論的位置。 – 2013-04-10 11:18:52

+0

是的,我編輯了這個問題,現在你可以看到我想要的... – 2013-04-10 11:34:15

+0

是的,我需要多個評論 – 2013-04-10 11:34:34

回答

8

不支持使用多個註釋編寫屬性文件。爲什麼?

PropertyFile.java

public class PropertyFile extends Task { 

    /* ======================================================================== 
    * 
    * Instance variables. 
    */ 

    // Use this to prepend a message to the properties file 
    private String    comment; 

    private Properties   properties; 

螞蟻屬性文件任務由java.util.Properties類存儲使用store()方法意見的支持。只從該任務中取出一條評論,並將其傳遞給Properties類以保存到文件中。

解決此問題的方法是編寫自己的任務,該任務由commons properties而不是java.util.Properties支持。公共屬性文件由property layout支持,該屬性文件允許settings comments for individual keys。使用save()方法保存屬性文件,並修改新任務以通過<comment>元素接受多個註釋。

0

根據PropertyFile任務的documentation,您可以將生成的屬性附加到現有文件。您可以僅使用註釋行來創建屬性文件,並讓Ant任務附加生成的屬性。

5

屬性文件任務用於編輯屬性文件。它包含各種不錯的功能,允許您修改條目。例如:

<propertyfile file="build.properties"> 
    <entry key="build_number" 
     type="int" 
     operation="+" 
     value="1"/> 
</propertyfile> 

我已將我的build_number加1。我不知道它的價值是什麼,但現在比以前更大了。

  • 使用<echo>任務來構建屬性文件而不是<propertyfile>。您可以輕鬆佈局內容,然後使用<propertyfile>稍後編輯該內容。

實施例:

<echo file="build.properties"> 
# Default Configuration 
source.dir=1 
dir.publish=1 
# Source Configuration 
dir.publish.html=1 
</echo> 
  • 創建每個部分單獨的屬性的文件。您可以爲每種類型提供評論標題。然後,使用批量它們一起放到一個單一的文件:

例子:

<propertyfile file="default.properties" 
    comment="Default Configuration"> 
    <entry key="source.dir" value="1"/> 
    <entry key="dir.publish" value="1"/> 
<propertyfile> 

<propertyfile file="source.properties" 
    comment="Source Configuration"> 
    <entry key="dir.publish.html" value="1"/> 
<propertyfile> 
<concat destfile="build.properties"> 
    <fileset dir="${basedir}"> 
     <include name="default.properties"/> 
     <include name="source.properties"/> 
    </fileset> 
</concat> 

<delete> 
    <fileset dir="${basedir}"> 
     <include name="default.properties"/> 
     <include name="source.properties"/> 
    </fileset> 
</delete>  
+0

我最喜歡這個答案。它的創意和使用簡單。感謝名單! – Mig82 2017-11-14 09:38:23