栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot maven属性文件过滤

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 ${}. 
# 属性文件可以用${}引用该变量
# 如: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-parent
        2.5.5
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        11
        ${}
        hello
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        hello
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.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

属性文件替换成功

                                                                                                                                      

                                                      

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/285399.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号