zoukankan      html  css  js  c++  java
  • maven中<resources>标签详解

    <!-- profiles.active默认激活dev -->
      <profiles>
        <profile>
            <!-- 声明这个profile的id身份 -->
            <id>dev</id>
            <!-- 默认激活:比如当知心mvn package命令是,没有传入参数,默认使用这个 当使用mvn package -P dev 传入参数时,表示使用这个id的profile -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <!-- 该标签下配置对应的key  value -->
            <properties>
                <!-- 这里的标签名任意,在 项目的 properties、xml等配置文件中可以使用${profiles.active}取出dev这个值-->
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
      </profiles>
    
    
      <build>
        <finalName>com_dubbo_config</finalName>
        <resources>
            <resource>
                <!-- 指定resources插件处理哪个目录下的资源文件 -->
                <directory>src/main/resources</directory>
                <!-- 打包后放在什么位置 -->
                <targetPath>${project.build.directory}/classes</targetPath>
                <!-- 不包含directory指定目录下的以下文件 -->
                <excludes>
                    <exclude>pro/*</exclude>
                    <exclude>dev/*</exclude>
                    <exclude>test/*</exclude>
                </excludes>
                <!-- 只(这个字很重要)包含directory指定目录下的以下文件 
                     <include>和<exclude>都存在的话,那就发生冲突了,这时会以<exclude>为准 -->
                <includes>
                    <include></include>
                </includes>
                <!-- filtering为true的时候,这时只会把过滤的文件(<excludes>)打到classpath下,
                     filtering为false的时候,会把不需要过滤的文件(<includes>)打到classpath下 -->
                <filtering>true</filtering>
            </resource>
    
            <resource>
                <directory>src/main/resources/${profiles.active}</directory>
                <targetPath>${project.build.directory}/classes</targetPath>
            </resource>
        </resources>
      </build>
    •  预过滤处理(配置文件是否需要提前进行预处理,过滤时候进行赋值)

    maven的resources和profiles的使用

    在开发maven项目时,一般都会把配置文件放到src/main/resources目录下,针对这个目录,maven的resources对其进行单独的配置。

    resources配置一般如下:

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>context.xml</include>
                </includes>
            </resource>
     
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>context.xml</exclude>
                </excludes>
            </resource>
        </resources>
    </build>

    配置中一共有两个resource,第一个resource配置是过滤src/main/resources目录下文件context.xml,若文件中有类似${key}这样的配置,就会根据maven的配置进行覆盖,让其使用真实值来填写,至于真实值如何来,后面会具体讲。

    第二个resource配置是不过滤src/main/resources目录下除了context.xml的其他文件,也就不会用真实值来填写${key}这样的配置。

    若是<include>和<exclude>都存在的话,那就发生冲突了,这时会以<exclude>为准。

    也许有人会有疑问,若只需要过滤context.xml的话,那就只需要配置第一个resource就可以了吧。其实不然,若是只配置第一个resource,第二个不配置,那么当你运行maven打包操作后,你就会发现,在工程的classpath下只有context.xml文件了,其他配置文件都没有打过来。所以第二个resource是必不可少的,指明其他配置文件是不需要过滤的,但是同样需要打包到classpath下。

    其实filtering为true的时候,这时只会把过滤的文件打到classpath下,filtering为false的时候,会把不需要过滤的文件打到classpath下。

    还有一点需要说明,若<filtering>、<include>和<exclude>都不配置,就是把directory下的所有配置文件都放到classpath下,若这时如下配置

    <resources>
        <resource>
            <directory>src/main/resources-dev</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>

    会以resources-dev下的相同文件为准,不一样的文件取并集。其实这样配合下面讲的profiles也可以实现各种不同环境的自动切换。

    前面讲到被过滤的文件会被真实值填写文件中的${key}位置,那这些真实值来自哪里呢?

    这些真实值其实都来自于profiles的配置里面,如下

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <config>pathConfig</config>
            </properties>
        </profile>
    </profiles>

    这段配置结合文章开头的配置,就会把context.xml文件中的${config}在打包过程中替换成pathConfig,而其他配置文件不受任何影响,利用这种特性也可以实现各种不同环境的自动切换,主要是在打包时指定使用哪个profile即可,命令如下:

    # 利用id=dev的profile配置打包
    man clean package -Pdev
    故乡明
  • 相关阅读:
    javascript类的封装『转』
    闭包概念
    继承
    理解面向对象
    cookie的使用
    开园啦。。。
    Myslq 之常用命令
    Myslq 之修改提示符
    Myslq 之登陆、退出
    Javascript 之 Number
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14282017.html
Copyright © 2011-2022 走看看