問題由來:
最近公司的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中定義的變量名}
如果有錯誤,還望大家指正。
