sringboot maven属性文件过滤
官网:Maven – Maven Getting Started Guide
*********************
属性文件过滤
Sometimes a resource file will need to contain a value that can only be
supplied at build time.
# 属性文件有时候会使用项目构建期间生成的属性
To accomplish this in Maven, put a reference to the property that will
contain the value into your resource file using the syntax ${}.
# 属性文件可以用${}引用该变量
# 如:application.properties
# application.name=${project.name}
# application.version=${project.version}
The property can be one of the values defined in your pom.xml, a value defined
in the user's settings.xml, a property defined in an external properties file,
or a system property
# property name来源:pom.xml、settings.xml、外部属性文件、系统变量
开启文件过滤:pom.xml ==> build
src/main/resources true
属性名称
# pom.xml 属性
${project.name}:项目名称
${project.version}:项目版本
${project.build.finalName}:jar包名称
${project.basedir}:项目路径
${project.build.directory}:jar包目录,默认为:${project.basedir}/target
# settings.xml 属性
${settings.localRepository}:用户本地仓库路径
*********************
示例
***************
配置文件
application.properties
# pom.xml
application.name=${project.name}
application.version=${project.version}
application.properties.value=${my.filter.value}
# settings.xml
setting.localRepository=${settings.localRepository}
setting.usePluginRegistry=${settings.usePluginRegistry}
# file properties
filters.hello=${hello}
filters.hello2=${hello2}
# command line
command.value=${commandLine.value}
***************
项目构建文件
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.5 com.example demo0.0.1-SNAPSHOT demo Demo project for Spring Boot 11 ${} hello org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-configuration-processortrue org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest hello org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok${basedir}/filters/filters.properties src/main/resources/filters/filters.properties src/main/resources true
***************
项目打包
执行命令:mvn package -DcommandLine.value=hello
target/classes ==> application.properties
# pom.xml application.name=demo application.version=0.0.1-SNAPSHOT application.properties.value=hello # settings.xml setting.localRepository=E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository setting.usePluginRegistry=false # file properties filters.hello=hello filters.hello2=hello2 # command line command.value=hello
属性文件替换成功



