在 .properties 文件中添加注释 [英] adding comment in .properties files

查看:75
本文介绍了在 .properties 文件中添加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在 build.xml 文件中使用以下代码块

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>

我能够生成具有以下文件内容的 default.properties 文件

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

如何使用 Ant 的 build.xml 动态执行此操作?

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

推荐答案

不支持写入带有多个注释的属性文件.为什么?

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;

ant 属性文件任务由 java.util.Properties 类支持,该类使用 store() 方法.仅从任务中提取一条注释,并将其传递给 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.

解决此问题的方法是编写自己的任务,该任务由 公共属性 而不是 java.util.Properties.公共属性文件由 支持属性布局允许 属性文件中各个键的设置注释.使用 save() 方法并修改新任务以通过 元素接受多个评论.

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.

这篇关于在 .properties 文件中添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆