【问题标题】:how to import a resource file to static code filed in a maven project如何将资源文件导入到 Maven 项目中的静态代码中
【发布时间】:2017-10-28 13:59:20
【问题描述】:

在我的 maven 项目中,我有一个这样的静态代码:

static {
    int address = xxx;
    int port = xxx;
}

我想用一个属性文件来定义变量,属性文件在我项目的resources目录下,但是我尝试了很多方法将这个属性文件导入到静态代码文件中,比如如:

Properties prop = new Properties();
prop.load(RedisPool.class.getClassLoader().getResourceAsStream("config.properties"));

但它不起作用。

【问题讨论】:

  • 为什么不在你的代码中使用属性...写一个类当然你应该使用getResourceAsStream("/config.properties")...如果你的config.properties文件位于src/main/resources...

标签: java maven properties static


【解决方案1】:

我认为您可以使用两种选择。

  1. templating-maven-plugin
  2. maven-replacer-plugin

网站上提供了如何使用的示例。

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2013-10-04
    • 2016-05-29
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多