您当前的位置: 首页 >  apache

Bulut0907

暂无认证

  • 0浏览

    0关注

    346博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Apache Maven profile详解和资源属性过滤

Bulut0907 发布时间:2022-07-14 08:45:36 ,浏览量:0

目录
  • 1. 概述
  • 2. 默认profile
  • 3. profile配置
  • 4. 激活profile
    • 4.1 基于环境信息激活
    • 4.2 命令行激活
  • 5. 资源属性过滤
  • 6. 资源文件过滤

1. 概述

profile有侧面的含义,项目有很多个运行环境,比如开发环境、测试环境、生产环境,每一个运行环境相当于是项目整体的一个侧面

所以profile可以给不同的运行环境定义不同的pom配置属性,然后根据需要激活一个运行环境

2. 默认profile

pom.xml中,project标签下除了modelVersion和module的坐标标签之外,其它标签都是在设定默认的profile。所以其它标签都可以配置到我们自己定义的profile中

3. profile配置

profile可以在下面两种配置文件中配置:

  • settings.xml:全局生效。比如全局配置JDK 1.11
  • pom.xml:当前POM生效

语法要求:

  • profiles/profile标签

由于profile标签覆盖了pom.xml中的默认配置,所以profiles标签通常是pom.xml中的最后一个标签

  • id标签

每个profile都必须有一个id标签,指定该profile的唯一标识。这个id标签的值会在命令行调用profile时被用到。这个命令格式是:-Pprofile id

  • 其它允许出现的标签

一个profile可以覆盖项目的最终名称、项目依赖、插件配置等各个方面以影响构建行为

build
    defaultGoal
    finalName
    resources
    testResources
    plugins
reporting
modules
dependencies
dependencyManagement
repositories
pluginRepositories
properties
4. 激活profile 4.1 基于环境信息激活

环境信息包含:JDK 版本、操作系统参数、文件、属性等各个方面。一个profile 一旦被激活,那么它定义的所有配置都会覆盖原来POM中对应层次的元素。可以参考下面的标签结构

  
    
      
      dev

      
      
        
        false
        11
        
          windows 10
          windows
          amd64
          10.0
        
        
          mavenVersion
          3.8.6
        
        
          test1.properties
          test2.properties
        
      

      
      
        
          
            
              maven-resources-plugin
              3.2.0
            
          
        
      
    
  

说明:

  • 如果activeByDefault为false,需要activation里面的所有条件都符合,才能激活该profile
  • Maven会自动检测当前环境安装的JDK版本,只要JDK版本是以11开头都算符合条件。比如这几个都符合:11.0.1、11.0.15
4.2 命令行激活

列出活动的profile,以及它们在哪里定义

C:\Users\dell\Desktop\maven-learn>mvn help:active-profiles
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.hh:maven-learn >-------------------------
[INFO] Building maven-learn 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:active-profiles (default-cli) @ maven-learn ---
[INFO] 
Active Profiles for Project 'com.hh:maven-learn:jar:0.1':

The following profiles are active:

 - jdk-11 (source: external)



[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.922 s
[INFO] Finished at: 2022-06-30T21:25:24+08:00
[INFO] ------------------------------------------------------------------------

C:\Users\dell\Desktop\maven-learn>

这是我们在settings.xml中定义了一个id为jdk-11的profile,且默认激活

指定某个具体profile进行激活。这里我们以上面定义的profile为例

C:\Users\dell\Desktop\maven-learn>mvn compile -Pdev
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.hh:maven-learn >-------------------------
[INFO] Building maven-learn 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ maven-learn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Using 'null' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\dell\Desktop\maven-learn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-learn ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.973 s
[INFO] Finished at: 2022-06-30T21:32:21+08:00
[INFO] ------------------------------------------------------------------------

C:\Users\dell\Desktop\maven-learn>

如果不激活dev这个profile,默认使用的maven-resources-plugin插件是2.6版本的。激活后使用的maven-resources-plugin插件版本是我们在profile中定义的3.2.0

5. 资源属性过滤

Maven可以通过profile实现各种不同运行环境切换,提供了一种资源属性过滤的机制。通过属性替换实现不同环境使用不同的参数

定义profile如下:

  
    
      devJDBCProfile
      
        com.mysql.cj.jdbc.Driver
        http://localhost:3306/information_schema
        root
        Root_123
      
      
        
          
            
            true
            
            
            ${project.basedir}/src/main/resources
          
        
      
    
  

src/main/resources/jdbcTest.properties文件内容如下:

jdbc.driver=${dev.jdbc.driver}
jdbc.url=${dev.jdbc.url}
jdbc.user=${dev.jdbc.user}
jdbc.password=${dev.jdbc.password}

然后执行资源处理命令

C:\Users\dell\Desktop\maven-learn>mvn clean resources:resources -PdevJDBCProfile
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.hh:maven-learn >-------------------------
[INFO] Building maven-learn 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-learn ---
[INFO] Deleting C:\Users\dell\Desktop\maven-learn\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-cli) @ maven-learn ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.633 s
[INFO] Finished at: 2022-06-30T21:50:51+08:00
[INFO] ------------------------------------------------------------------------

C:\Users\dell\Desktop\maven-learn>

生成的target/classes/jdbcTest.properties文件的内容如下:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=http://localhost:3306/information_schema
jdbc.user=root
jdbc.password=Root_123
6. 资源文件过滤

可以在resource标签下,使用includes和excludes标签,在resource阶段包含或排除一些资源文件

定义profile如下:

  
    
      devJDBCProfile
      
        
          
            
            true
            
            
            ${project.basedir}/src/main/resources
            
            
              *.txt
            
            
              test2.properties
            
          
        
      
    
  

src/main/resources目录下新建文件test1.txt和test2.properties

然后执行资源处理命令

C:\Users\dell\Desktop\maven-learn>mvn clean resources:resources -PdevJDBCProfile
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.hh:maven-learn >-------------------------
[INFO] Building maven-learn 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-learn ---
[INFO] Deleting C:\Users\dell\Desktop\maven-learn\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-cli) @ maven-learn ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.563 s
[INFO] Finished at: 2022-06-30T21:59:47+08:00
[INFO] ------------------------------------------------------------------------

C:\Users\dell\Desktop\maven-learn>

在target/classes目录下,只有test1.txt

关注
打赏
1664501120
查看更多评论
立即登录/注册

微信扫码登录

0.0411s