Maven - 从外部属性文件中读取属性 [英] Maven - Reading a property from an external properties file

查看:21
本文介绍了Maven - 从外部属性文件中读取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a property file with the following

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

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

I try to read those properties from my pom file as shown below

<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 时它没有找到属性,而是出现以下错误:

I find that when i run mvn clean install it does not find the properties, instead it comes up with the following errors:

'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

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

The above failures appear to be in situations where i refer to the property when i declare the dependency. I found that in some other situations the property is read from the file. For example it works if i use a property on the project version tag (not dependency version)

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

It seems that the property is not read from the file if it is referred to from the dependency declaration but is read if referred to from anywhere else. Any ideas?

推荐答案

initialize 阶段不属于 干净的生命周期.您还需要将您的属性插件绑定到 pre-clean 阶段.

The initialize phase is not part of the clean lifecycle. You need to also bind your properties plugin to pre-clean phase.

然而,依赖解析在解析和执行其他插件之前运行,所以你的方法不起作用.

However, the dependency resolution runs before resolving and executing other plugins, so your approach won't work.

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

The proper way to deal with that would be to move dependency versions into a parent pom.xml and use the same parent pom in both of your projects.

这篇关于Maven - 从外部属性文件中读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆