资讯专栏INFORMATION COLUMN

Maven profile整合Spring profile

用户84 / 388人阅读

摘要:此时可以尝试或命令打包,安装包内的文件中占位符已被替换。整合原理项目中一般都会加上可以查看的文件,里面包含定义的值是这样插件会将或文件中的替换为中对应的值。

在Maven和Spring中,都有profile这个概念。profile是用于区分各种环境的,例如开发环境、测试环境、正式环境等。Maven的profile用于在打包时根据指定环境替换不同环境的配置文件配置,如数据库配置。Spring的Profile可以用于在不同的环境下加载不同的bean,例如@Profile注解。两者一个是Maven编译和打包时生效,另一个是运行时生效,默认是没有关联的,本文会分别介绍非Spring Boot项目和Spring Boot项目整合Maven profile。

Maven profile配置

pom.xml中,可以配置testproduct两个profile,分别对应测试环境和正式环境。这里也可以根据具体情况自定义。


  
    test
    ...
  
  
    product
    ...
  

此时,运行mvn package -Ptest就会使用id为test的profile内的配置打包,mvn package -Pproduct就是用来打正式环境包的命令。

Spring Framework(非Spring Boot)整合Maven profile Spring Framework如何启用一个profile

Spring启用某个profile有多种方式(摘自官方文档:https://docs.spring.io/spring... ):

Activating a profile can be done in several ways, but the most straightforward is to do it programmatically against the Environment API which is available through an ApplicationContext.
In addition, you can also declaratively activate profiles through the spring.profiles.active property, which may be specified through system environment variables, JVM system properties, servlet context parameters in web.xml, or even as an entry in JNDI.

总结一下有以下几种方式:

通过代码设置:ApplicationContext.getEnvironment().setActiveProfiles("yourProfile")

通过系统环境变量spring.profiles.active值来设置

通过JVM系统属性spring.profiles.active值来设置

通过web.xml中的context-param来设置

为了便于跟Maven整合,我们使用web.xml来设置Spring profile,如下:


    spring.profiles.active
    product

以上配置会启用Spring的product profile,即正式环境。

Spring Framework profile整合Maven profile

如果想要整合Maven profile和Spring Framework profile,需要在Maven打包时对web.xml中的spring.profiles.active值进行替换,可以在web.xml中配置一个占位符${activeProfile}


    spring.profiles.active
    ${activeProfile}

pom.xml配置maven-war-plugin



  
    maven-war-plugin
    3.2.2
    
      true
    
  




  dev



  
    test
    
      test
    
  
  
    product
    
      product
    
  

true表示过滤Deployment Descriptor并将文件中的占位符替换为pom.xml中对应的值,Deployment Descriptor即部署描述符,指的就是web.xml (参考维基百科:https://zh.wikipedia.org/wiki... )。

以上配置完成后,再通过mvn package -Ptestmvn package -Pproduct打包后,再解压war包,可以看到web.xml中原有的


    spring.profiles.active
    ${activeProfile}

被替换为了Maven中对应的profile,例如mvn package -Pproduct打包后web.xml内容:


    spring.profiles.active
    product

以上就完成了Maven profile和Spring profile的整合。

兼容jetty-maven-plugin

如果恰好在项目中使用到jetty-maven-plugin用于开发环境调试,那么在web.xml配置占位符${activeProfile}后,通过mvn jetty:run启动应用时会Spring框架会报错:

Could not resolve placeholder "activeProfile" in string value "${activeProfile}"

这是因为运行mvn jetty:run命令时插件并没有打war包,而是直接使用源码中的web.xml,此时占位符${activeProfile}未被maven-war-plugin替换,所以Spring框架会报错。

参考文档:https://www.eclipse.org/jetty...

解决方法一

使用mvn jetty:run-warmvn jetty:run-exploded命令替代mvn jetty:run,这两个命令会先用maven-war-plugin打好war包后再运行,此时占位符${activeProfile}已被替换为Maven的profile。

但是这种方案会带来一个问题:由于这种方式需要先打war包再运行,开发时项目中资源(例如html、jsp)修改后就不会实时生效,而是需要重新打包启动,不便于调试。

解决方法二(推荐)

这种方案还是使用mvn jetty:run命令,只需要给jetty-maven-plugin插件添加一个名为activeProfile的系统属性,让Spring框架来解析web.xml中的${activeProfile}


  org.eclipse.jetty
  jetty-maven-plugin
  9.2.10.v20150310
  
    
      /
    
    
      
        activeProfile
        ${activeProfile}
      
    
  

参考文档:https://www.eclipse.org/jetty...

Spring Boot整合Maven profile

如果项目采用的框架是Spring Boot而不是直接使用Spring Framework,那么Spring Boot的profile可以在resources目录下的application.propertiesapplication.yml文件中指定,以application.properties为例:

spring.profiles.active=product

要想整合Maven profile只需要改为@activeProfile@占位符即可:

spring.profiles.active=@activeProfile@

仅需要这一行配置就完成了Spring Boot profile整合Maven profile,非常方便。此时可以尝试mvn package -Ptestmvn package -Pproduct命令打包,安装包内的文件中@activeProfile@占位符已被替换。

Spring Boot整合Maven profile原理

Spring Boot项目中一般都会加上spring-boot-starter-parent


    org.springframework.boot
    spring-boot-starter-parent
    ${spring.boot.version}

可以查看spring-boot-starter-parent的pom.xml文件,里面包含maven-resources-plugin


    maven-resources-plugin
    
        
            ${resource.delimiter}
        
        false
    

${resource.delimiter}定义的值是@

@

这样maven-resources-plugin插件会将application.propertiesapplication.yml文件中的@activeProfile@替换为pom.xml中对应profile的值。

至于为什么Spring Boot要使用@..@而不是Maven默认的${..}作为占位符的符号,官方文档也给出了解释,以下摘自:https://docs.spring.io/spring...

Note that, since the application.properties and application.yml files accept Spring style placeholders (${…​}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)

因为Spring Boot框架本身也用${..}作为占位符,Maven插件maven-resources-plugin如果还使用相同的占位符,那么可能会导致一些冲突,所以spring-boot-starter-parentmaven-resources-plugin的占位符改为@..@

参考文档

Spring Framework: Activating a Profile

Spring Boot: Maven

Jetty Maven Plugin

维基百科:部署描述符(Deployment Descriptor)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73558.html

相关文章

  • Maven详细教程

    摘要:清理上一次执行创建的文件处理资源文件编译代码执行单元测试文件创建拷贝到本地的仓库下面发布生成文档将工程所有文档生成网站,生成的网站界面默认和的项目站点类似,但是其文档用格式写的,目前不支持,需要用其他插件配合才能支持。 前言 本文可以帮助你加深对Maven的整体认识,不是一篇基础文章。如果你现在还没有用 Maven 跑过 HelloWorld,那么本文可能不适合你。 一、Maven简介...

    Keagan 评论0 收藏0
  • SpringCloud核心教程 | 第三篇:服务注册与发现 Eureka篇

    摘要:下一篇介绍基于的服务注册与调用。服务提供者工程配置这里服务提供者是使用之前进阶教程第三篇整合连接池以及监控改造而来,这里一样的部分就不再重复说明,下面将说明新增的部分。 Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分...

    scq000 评论0 收藏0
  • Maven管理SpringBoot Profile

    摘要:的配置文件默认为或,此外仅以配置为说明。的由的标签管理。管理由于构建是基于或,此处仅以说明。管理分五步,以下详细介绍。并且为表示,会将文件内容的替换为相应的变量如文件中的会替换为属性值。 1. Spring Profile Spring可使用Profile决定程序在不同环境下执行情况,包含配置、加载Bean、依赖等。 Spring的Profile一般项目包含:dev(开发), test...

    wenzi 评论0 收藏0

发表评论

0条评论

用户84

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<