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

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

问题描述

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 <支持/ code>使用 store()方法。只从该任务中获取一条注释,并将其传递到属性类以保存到文件中。

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 。 commons属性文件由支持属性布局允许设置属性文件中各个键的注释。使用 save()方法并修改新任务以通过< comment> 元素接受多个评论。

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天全站免登陆