springboot maven属性文件过滤


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 ${<property name>}. 
# 属性文件可以用${<property name>}引用该变量
# 如: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

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>  <!-- 属性文件位置 -->
        <filtering>true</filtering>                <!-- 该值默认为false -->
                                                   <!-- 修改为true开启过滤 -->
      </resource>
    </resources>
  </build>

                          

属性名称

# 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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <resource.delimiter>${}</resource.delimiter>
        <my.filter.value>hello</my.filter.value>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>hello</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>

        <!-- 引入外部配置文件 -->
        <filters>
            <filter>${basedir}/filters/filters.properties</filter>
            <filter>src/main/resources/filters/filters.properties</filter>
        </filters>

        <!-- 开启资源过滤 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>

                                     

***************

项目打包

                   

执行命令: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

属性文件替换成功

                                                                                                                                      

                                                      

Guess you like

Origin blog.csdn.net/weixin_43931625/article/details/120572468