maven中 pom.xml与properties等配置文件之间互相读取变量

问题由来:

        最近公司的maven项目需要改进,希望把该项目依赖的一系列artifact放到properties文件中,这样做的目的是是为了很容易看到四月份release和七月份的release,它们所依赖的artifact的版本多少,以及版本之间的差别。当然能做到这个,有很多方法,比如定义很多个profile,把那些artifact的version都写在其中,这样做好处简单,不要对项目的pom改动太大,坏处是当release很多时,根pom会变得特别大。我采用的的方式是将每个release dependencies 放在properties文件中。这就涉及到pom.xml如何读取properties文件的变量。

问题一:

pom.xml读取properties文件的变量

解决办法:

我的properties的一些列文件在该项目下的properties文件夹中

在根pom添加插件:

<build>
    <plugins>
      <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>${user.dir}/properties/2017.11.release.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

然后就可以直接在pom(包括子module中的pom)里面引用2017.11.release.properties中定义的变量了。

 

问题二:

properties文件读取pom.xml中定义的变量

***我的properties的一些列文件在该项目下的properties文件夹中

在pom加上如下代码:

      <resources>
           <resource>
                <directory>${project.basedir}/properties</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                </includes>
            </resource>
        </resources>

properties中就可以直接应用pom定义的变量,引用的方法version=${pom中定义的变量名}

如果有错误,还望大家指正。

 

posted @ 2017-11-23 16:34  周博文  阅读(21649)  评论(2编辑  收藏  举报