【问题标题】:Unable to retrieve values from property files in Maven无法从 Maven 中的属性文件中检索值
【发布时间】:2014-01-27 13:23:30
【问题描述】:

我正在使用maven pom.xml(刚开始学习)

我有一些 .properties 文件(例如:log4j.properties),我应该能够在 pom.xml 或 web.xml 文件中从它们中检索值,我的意思是如果我使用类似 ${somename. version} 在 pom.xml 或 web.xml 中,此值应从 .properties 文件中检索。

我的属性文件如下:

src/main/resources/log4j.properties
src/main/env/dev/config.properties

我正在尝试如下,但无法从属性文件中检索值..我做错了什么。

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/env/dev</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

请给我建议。

【问题讨论】:

标签: maven maven-2 maven-3 pom.xml


【解决方案1】:

您的配置错误,用于从文件中读取属性:

<filters>
    <filter>src/main/resources/log4j.properties</filter>
</filters>

您的实际配置是关于应该过滤哪些文件。

有关更多信息和示例,请访问:Maven Resource Plugin - Filtering


你也可以使用Properties Maven Plugin

【讨论】:

【解决方案2】:

资源过滤是指在 pom.xml 中设置一个属性,然后在属性文件中使用它。

使用 log4j.properties 中的 ${somename.version} 和 pom.xml 中的相应代码,您的 ${somename.version} 将替换为您在 pom.xml 中键入的内容

例如在 pom.xml 中

<property> <name>somename.version</name> <value>123</value> </property>

在您的 log4j.property 中 ${somename.version} 将替换为 123

启动 mvn 包后,您将在目标目录中找到具有替换值的文件

使用配置文件打包时会使用资源过滤。每个配置文件都可以更改 pom.xml 中的属性

您的属性可以保存随环境变化的值,例如 windows 配置或 linux 配置

【讨论】:

猜你喜欢
  • 2018-01-24
  • 2013-05-07
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-12
  • 2018-10-06
相关资源
最近更新 更多