gpt4 book ai didi

java - 外部化 DEFAULT 配置属性

转载 作者:行者123 更新时间:2023-11-30 11:36:06 25 4
gpt4 key购买 nike

很多项目都在使用配置文件和我找到了很多关于如何读取此配置文件、如何将它们外部化的帖子,但我找不到有关如何生成示例配置文件的任何信息。

例如,如果您有一个充满各种配置属性的 company.config 文件,我希望自动生成一个 company.sample.config 文件每次构建项目时都从我的源代码中获取。此示例文件应包含项目使用的所有配置属性,以及默认值和注释。

我使用 Java 和 Gradle,所以我在考虑 Gradle 插件。

有这样的东西吗?其他人是如何解决这个问题的?


Later Edit: Some example:


public class CompanyConfig {
private static final String PROPERTY1_DEFAULT = "Company.property1_default";
private static final String PROPERTY2_DEFAULT = "Company.property2_default";

/**
* description 1
*/
private static final String PROPERTY1 = "Company.property1";
/**
* description 2
*/
private static final String PROPERTY2 = "Company.property2";

private Config config;

public CompanyConfig(Config config) {
this.config = config;
}

public String getProperty1() {
return config.getProperty(PROPERTY1, PROPERTY1_DEFAULT);
}

public String getProperty2() {
return config.getProperty(PROPERTY2, PROPERTY2_DEFAULT);
}
}

根据上面的代码,sample.conf 文件应该是这样的:

**description 1
*Company.property1=Company.property1_default

**description 2
*Company.property2=Company.property2_default

这样我就可以把sample.conf文件给要配置工程的人了。这个人不知道我项目中的配置属性,所以他/她需要一个属性列表和每个属性的默认值。由于引入了新的特性并删除了一些其他特性,因此必须在每次构建时生成示例文件。

希望这能澄清我在这里要问的问题。

最佳答案

您可以借助 ant 构建脚本。借助以下代码。

<project>
<target name="default">



<propertyfile file="company.sample.config" comment="sample properties here.">

<entry key="program.AUTHOR" default="RAIS" />
<entry key="program.COMPANY" default="IGT" />
<entry key="program.COPYRIGHT" default="now" type="date" pattern="yyyy" />
<entry key="program.DESCRIPTION" default="" />
<entry key="program.VERSION" default="1.0.0" />
<entry key="program.BUILDNUM" value="1" />
<entry key="program.BUILDDATE" type="date" value="now" pattern="yyyyMMDDHHmmss" />
</propertyfile>

</target>
</project>

关于java - 外部化 DEFAULT 配置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14725537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com