【问题标题】:Read property file in maven在maven中读取属性文件
【发布时间】:2013-01-20 13:30:21
【问题描述】:

我将以下代码添加到我的 pom.xml 以使用以下配置从外部属性文件中读取属性:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
      <execution>
        <phase>initialize</phase>
        <goals>
          <goal>read-project-properties</goal>
        </goals>
        <configuration>
          <files>
            <file>..\maven.properties</file>
          </files>
        </configuration>
      </execution>
    </executions>
</plugin>

在我的 maven.properties 文件中,我有以下内容:

aspectj-maven-plugin.version=1.4

versions-maven-plugin.version=2.0

我在 pom.xml 中使用${versions-maven-plugin.version}

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>versions-maven-plugin</artifactId>
  <version>${versions-maven-plugin.version}</version>
  <configuration>
    <rulesUri>url</rulesUri>
    <includesList>com.package:*</includesList>
  </configuration>
</plugin>

但是当我在包上运行mvn clean install 时抛出异常,说插件版本无效。为什么会出现这种情况?

注意:pom.xml 和mave.properties 都在同一个文件夹中

【问题讨论】:

  • 可能是典型的 `\` windows 路径分隔符问题?
  • 您确定要这样做吗?某人的桌面文件夹的绝对路径并不是真正的Maven 方式...
  • 我是这样测试的。我将属性文件放到父项目中,但它仍然没有拾取它。
  • 您是否介意将 ` \ ` 更改为 ` / `,至少要确保问题不是来自于此
  • 是的,我已经尝试过这两种组合仍然是同一个问题

标签: maven plugins pom.xml


【解决方案1】:

原因很简单:为了能够读取properties-maven-plugin 的配置,Maven 还必须解析所有其他插件——它在实际启动插件之前无法知道插件的作用。

当它这样做时,它注意到它没有所有必要的版本。这个验证步骤在第一个插件被实例化之前很久就发生了。

结论:你的方法行不通。您可以做的是将属性放入 POM,然后将它们从那里提取到属性文件中(例如使用 maven-resources-plugin)。

【讨论】:

  • 你能详细说明一下吗?
  • 我不知道你不知道/需要知道什么。请阅读资源插件 (specifically about filtering) 的文档或提出具体问题。
  • 似乎不可能使用 mvn3 将 properties 文件中的值输入到插件配置中,因为 properties-maven-plugin 根本无法“交付”它在 所有 插件之前读取的内容配置。没有机会逃脱——恶性循环。 -- 但是,可以使用maven-resources-plugin 将值馈入由 maven 处理的资源中:它需要复制资源(= 文本文件)作为 maven 应该为您完成的工作的一部分。
猜你喜欢
  • 1970-01-01
  • 2020-05-10
  • 2011-10-31
  • 1970-01-01
  • 2013-01-21
  • 2010-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多