Maven - 从外部属性文件中读取属性

我有一个包含以下内容的属性文件

junit.version=3.8.1
dbcp.version=5.5.27
oracle.jdbc.version=10.2.0.2.0

我尝试从我的 pom 文件中读取这些属性,如下所示

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>${junit.version}</version>
  <scope>test</scope>
</dependency>


<dependency>
    <groupId>dbcp</groupId>
    <artifactId>dbcp</artifactId>
    <version>${dbcp.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc14</artifactId>
  <version>${oracle.jdbc.version}</version>
  <scope>provided</scope>
</dependency>

以及插件配置

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <executions>
           <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>../live.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>

我发现当我运行 mvn clean install 时它没有找到属性,而是出现以下错误:

'dependencies.dependency.version' for junit:junit:jar must be a valid version but is '${junit.version}'. @ line 23, column 16
'dependencies.dependency.version' for dbcp:dbcp:jar must be a valid version but is '${dbcp.version}'. @ line 31, column 12
'dependencies.dependency.version' for com.oracle:ojdbc14:jar must be a valid version but is '${oracle.jdbc.version}'. @ line 37, column 13

上述失败似乎是在我声明依赖项时引用属性的情况。我发现在其他一些情况下,属性是从文件中读取的。 例如,如果我在项目版本标签(不是依赖版本)上使用属性,它就可以工作

如果从依赖声明中引用该属性,则似乎不会从文件中读取该属性,但如果从其他任何地方引用该属性,则似乎会读取该属性。有什么想法吗?

最佳答案

initialize 阶段不是 clean lifecycle 的一部分.您还需要将属性插件绑定(bind)到 pre-clean 阶段。

但是,依赖解析在解析和执行其他插件之前运行,因此您的方法不起作用。

解决这个问题的正确方法是将依赖版本移动到父 pom.xml 中,并在两个项目中使用相同的父 pom。

https://stackoverflow.com/questions/9912632/

相关文章:

c++ - 使用 C++ 构建错误 - ‘find_if’ 不是 ‘std' 的成员

c++ - 在头文件与 .cpp 文件中编码 C++ (moSTLy)

scala - 如何为 SBT 设置默认项目

build - Gitlab-CI:gitlab ci trigger build 仅用于合并请求

scala - SBT - 姓名和身份证有什么区别?

build - 在构建服务器上使用 ramdisk 是否明智?

build - 链接大型库时是否可以加快 Rust 编译?

c++ - 用于创建合并/将库的所有源文件合并为 C/C++ 的工具?

xcode - 构建静态库

ios - 带有 "Release: Fastest, Smallest [-Os]"的 Xcode