Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

java - adding comment in .properties files

By using following block of code in build.xml file

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

I am able to generate default.properties file with following file contents

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

I want to know how can I add my comments in the generated file? E.g. the generated properties should have the following content:

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

How can I do it dynamically using Ant's build.xml?

question from:https://stackoverflow.com/questions/15924220/adding-comment-in-properties-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Writing the properties file with multiple comments is not supported. Why ?

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;

The ant property file task is backed by a java.util.Properties class which stores comments using the store() method. Only one comment is taken from the task and that is passed on to the Properties class to save into the file.

The way to get around this is to write your own task that is backed by commons properties instead of java.util.Properties. The commons properties file is backed by a property layout which allows settings comments for individual keys in the properties file. Save the properties file with the save() method and modify the new task to accept multiple comments through <comment> elements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...