maven的资源过滤filters[通俗易懂]

maven的资源过滤filters[通俗易懂]maven的资源过滤maven的过滤资源需要结合maven的2个定义才能实现,分别是:profile resources下面分开来做介绍。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>  

 

 

使用标准的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

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 标签的使用

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

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/143331.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

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