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

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

问题描述

我有以下属性文件

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运行干净的安装没有找到属性,而不是它与下面的错误出现:

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?

推荐答案

初始化相不中的一部分<一个href=\"http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference\">clean生命周期。您还需要绑定你的属性插件 pre-清洁阶段。

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天全站免登陆