预加载资源文件需要先启用功能:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> 启用预加载,默认false </resource> </resources> </build>
在src/main/resources里,新增一个测试用的application.properties文件
# application.properties application.name=${env.PATH} application.name=${settings.offline} application.name=${java.home} application.version=${pom.name} application.version=${project.version}
先看普通效果,运行cmd
mvn process-resources
被放到target/classes里
# application.properties application.name=C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;F:\MySQL\MySQL Server 5.1\bin;D:\Program Files\TortoiseSVN\bin;D:\Program Files\TortoiseGit\bin;d:\Program Files (x86)\Git\cmd;F:\apache-maven-3.2.3\bin;F:\Java\jdk1.8.0_25\bin;F:\HashiCorp\Vagrant\bin;F:\Tomcat 7.0\bin application.name=false application.name=F:\Java\jdk1.8.0_25\jre application.version=my-app application.version=1.0-SNAPSHOT
现在新建个src/main/filters/filter.properties,用于预加载:
# filter.properties
my.filter.value=hello!
更改pom.xml:
<build> <filters>指明预加载资源 <filter>src/main/filters/filter.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
这时候就可以在application.properties中直接引用了:
message=${my.filter.value}
就会变成:
message=Hello!
这样就实现了资源文件间的属性引用,而无需声明二者的关联(Spring?)