maven的filters目录详解

maven的filters目录详解maven的资源过滤maven的过滤资源需要结合maven的2个定义才能实现,分别是:profileresources下面分开来做介绍。profileprofile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。需要掌握profile的定义以及激活条件。后面结合

大家好,又见面了,我是你们的朋友全栈君。

maven的资源过滤

maven的过滤资源需要结合maven的2个定义才能实现,分别是:

  • profile
  • resources

下面分开来做介绍。

profile

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。需要掌握profile的定义以及激活条件。后面结合resources会介绍。

resources

resources是指定maven编译资源文件指定到何处的,例如maven的标准资源目录结构是src/main/resources(这个在超级pom中定义到了),maven进行编译时候就会将resources中的资源文件放到web的WEB-INF/classes下.具体如何和资源目录有关系,后面结合的时候后讲到。
超级pom中定义的resources:

<resources>
     <resource>
            <directory>${project.basedir}/src/main/resources</directory>
    </resource>
</resources>
<testResources>
      <testResource>
           <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
</testResources>

maven标准目录filter

很多互联网项目中,测试环境和线上环境都是分离的,那么为了方便开发测试,maven项目可以在编译时选取不同的配置文件,如何设置呢,看看以下例子?。

例子如下:
我在java/src/resources目录中定义了jdbc.properties文件内容如下:

#dataSource configure
#jdbc.connection.url=jdbc:mysql://localhost:3306/test
#jdbc.connection.username=root
#jdbc.connection.password=123456


jdbc.connection.url=${jdbc.url}
jdbc.connection.username=${jdbc.username}
jdbc.connection.password=${jdbc.password}

通过maven编译后再WEB-INF/classes中得到的jdbc.properties文件内容如下:

#dataSource configure
#jdbc.connection.url=jdbc:mysql://localhost:3306/test
#jdbc.connection.username=root
#jdbc.connection.password=123456


jdbc.connection.url=abcd
jdbc.connection.username=cccc
jdbc.connection.password=dddd

具体是怎么做到的呢?属性在使用${}的方式获取,属性值肯定得在pom中定义,这个在项目pom.xml中的定义方式如下:

<profiles>
        <!-- 开发/测试环境,默认激活 -->
        <profile>
            <id>test</id>
            <properties>
                <jdbc.url>abcd</jdbc.url>
                <jdbc.username>cccc</jdbc.username>
                <jdbc.password>dddd</jdbc.password>
            </properties>
            <activation>
                <!--默认启用的是dev环境配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!-- 生产环境 -->
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>

为了能让profiles中的内容能让resources中的文件使用到,还需要上面说到的resources插件,定义信息如下:

<build>
        <finalName>idea-maven-introduce</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

filtering设置为true很关键,不然引用不到profiles中的内容。但是这样做,就算设置好了吗,如何切换不同的属性的呢,还是没能体现到啊

profiles的激活方式:

  1. 默认激活方式
    根据上面的例子,定义了一个
<activation>
        <!--默认启用的是dev环境配置 -->
         <activeByDefault>true</activeByDefault>
</activation>

这个是默认的激活方式,意思就是你什么都不做,直接使用标准的package打包时候会将该test定义属性能让resources下面的文件获取到。

  1. 手动方式激活
    使用命令,mvn package –P profileId,例如:
 mvn package –P profileTest1 

它就会找profileTest1定义的属性了。

  1. 根据jdk环境来激活
<profiles>  
       <profile>  
              <id>profileTest1</id>  
              <jdk>1.5</jdk>  
       </profile>  
<profiles>  
  1. 根据操作系统环境来激活
<profiles>  
       <profile>  
            <id>profileTest1</id>  
            <activation>  
              <os>  
                   <name>Windows XP</name>  
                   <family>Windows</family>  
                   <arch>x86</arch>  
                   <version>5.1.2600</version>  
              </os>  
            </activation>  
       </profile>  
</profiles> 
  1. 根据文件是否存在激活
<profiles>  
       <profile>  
            <id>profileTest1</id>  
            <activation>  
              <file>  
                   <missing>target</missing>  
              </file>  
            </activation>  
       </profile>  
</profiles>

profiles的激活方式,查询:

我们可以同时定义多个profile,那么在建立项目的过程中,到底激活的是哪一个profile呢?Maven为我们提供了一个指令可以查看当前处于激活状态的profile都有哪些,这个指定就是:

mvn help:active-profiles

执行该命令后,你会看到核心如下输出:

Active Profiles for Project
‘com.lgy:idea-maven-introduce:war:1.0-SNAPSHOT’: The following
profiles are active:
– test (source: com.lgy:idea-maven-introduce:1.0-SNAPSHOT)

说明激活的profiles的id为test。

使用标准的maven目录file进行管理profiles

src/main/java/filters中的文件如下:
aaa.properties

jdbc.url=aaajdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=aaatestuser
jdbc.password=aaa123456

bbb.properties

jdbc.url=bbbjdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=bbbtestuser
jdbc.password=bbb123456
file管理配置文件例子1:

pom.xml文件的内容:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lgy</groupId>
    <artifactId>idea-maven-introduce</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>idea-maven-introduce Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>



    <build>
        <finalName>idea-maven-introduce</finalName>

        <filters> <!-- 指定使用的 filter -->
            <filter>src/main/filters/aaa.properties</filter>
        </filters>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

此时去掉了profiles,直接用filters指定要使用的filter,此时,resources中要用到的值都会从aaa.properties.

#dataSource configure
#jdbc.connection.url=jdbc:mysql://localhost:3306/test
#jdbc.connection.username=root
#jdbc.connection.password=123456


jdbc.connection.url=aaajdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.connection.username=aaatestuser
jdbc.connection.password=aaa123456
file管理配置文件例子2:

结合profiles的激活机制能更好的使用filers目录中的内容,pom.xml中的内容如下:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lgy</groupId>
    <artifactId>idea-maven-introduce</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>idea-maven-introduce Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <profiles>
        <!-- 开发/测试环境,默认激活 -->
        <profile>
            <id>test</id>
            <properties>
                <dev.name>aaa</dev.name>
            </properties>

            <activation>
                <!--默认启用的是dev环境配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!-- 生产环境 -->
        <profile>
            <id>product</id>
            <properties>
                <dev.name>bbb</dev.name>
            </properties>
        </profile>
    </profiles>

    <build>
        <finalName>idea-maven-introduce</finalName>

        <filters> <!-- 指定使用的 filter -->
            <filter>src/main/filters/${dev.name}.properties</filter>
        </filters>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

此时默认的激活方式就是profiles中id为test,filters就会去寻找aaa.peroperties中的对应的属性值给resources中的资源文件进行使用!

总结

有关知识点的内容讲解有如下:
– maven profiles标签的使用
– resources 资源标签的使用
– filters 标签的使用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143192.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 华为OD(外包)社招技术二面,总结复盘

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者:沉夢志昂丶 blog.csdn.net/GOLOJO/article/details/105689366 一、…

    2021年6月26日
    124
  • 用python来开发webgame服务端(1)[通俗易懂]

    用python来开发webgame服务端(1)[通俗易懂]http://ciniao.me/article.php?id=9 刺鸟原创文章,转载请注明出处    在开始之前,先简单描述一下项目的特点:我要实现的是一个mmorpg的webgame,地图上需要看到其他的玩家,战斗系统采用半回合制的模式,所谓的半回合制,即是:采用回合制的画面布局,友方和敌方分列左右,但是战斗的中途,其他的玩家可以及时的随时插入这场战斗。当然,作为一款rpgGa

    2022年5月30日
    53
  • Jmeter正则表达式提取器-一些常用技巧

    Jmeter正则表达式提取器-一些常用技巧以下为网上转载:1、提取全部:^(.*)$2、提取中间某一段:(.*?)或(.+?)3、从中间某处提取到末尾:([^”]+)4、转义:\5、换行:\n6、\d+是来匹配1个或更多连续的数字ps:(.+?)和(.*?)的区别:(.+?)提取1个字符串及以上,不要太贪婪,在找到第一个匹配项后停止;(.*?)提取0个字符串及以上,要取的值是空值的时候可以取得到。例如:“test”:””,如果要取test的值,使用(.+?)匹配不…

    2025年10月20日
    4
  • IplImage中的widthStep大小计算及原理[通俗易懂]

    IplImage中的widthStep大小计算及原理[通俗易懂]一直以为IplImage结构体中的widthStep元素大小等于width*nChannels,大错特错!查看OpenCV2.1的源码,在src/cxcore/cxarray.cpp文件中,找到cvInitImageHeader函数,函数中对widthStep大小赋值如下:image->widthStep=(((image->width*image->nChannels*

    2022年4月30日
    51
  • 模板消息php40008,企业微信发送模板消息 40008 Warning: wrong json format. ?

    模板消息php40008,企业微信发送模板消息 40008 Warning: wrong json format. ?同样的入参,在单元测试,本地启动服务调用均正常,在某个机器一直返回错误{“touser”:”abingnew”,”msgtype”:”miniprogram_notice”,”miniprogram_notice”:{“title”:”测试标签”,”page”:”/pages/index/index”,”description”:”阿炳new向您发来推广任务”,”appid…

    2022年6月10日
    145
  • nexus3下载地址

    nexus3下载地址由于nexus目前官网上已经很难下载了,除非翻墙,故整理了一下目前最新版本的分享一下,有需要的欢迎下载。win64:nexus-3.20.1-01-win64.zip链接:https://pan.baidu.com/s/19THgVb6LzLXJlxzomsvwhw提取码:9vvglinux:nexus-3.20.1-01-unix.tar.gz链接:https://pan.baid…

    2022年7月12日
    183

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号