<!-- 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