WAR包與配置文件分離(二)
如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033
環境:
SpringMVC
Maven:3.3.9
相關閱讀:
WAR包與配置文件分離
前言
Java開發中經常打WAR的人會遇到一個問題,每次都得根據部署電腦的IP、路徑等信息修改工程中的配置文件。但是每次都這樣還是非常煩人的。
下面介紹一下如何將配置文件分離出來,這樣每次打WAR包的時候就可以直接將WAR部署就好了,一般不需要關心配置文件的問題。
1.配置pom.xml基礎路徑
由於我們使用maven,所以這裏以pom.xml爲根目錄進行配置,其他配置採用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/xsd/maven-4.0.0.xsd">
...
<!-- 自定義屬性 -->
<properties>
<!-- 配置文件輸出路徑 -->
<customConfigDir>c:\\webConfig</customConfigDir>
</properties>
...
</project>
2.設置pom.xml參數其他配置文件可用
filtering標籤可以設置pom.xml參數其他配置文件可用
這裏以resources爲例,設置*.xml和*.properties可以使用pom.xml參數
<build>
<!-- 項目資源清單(可以配置多個項目資源) -->
<resources>
<resource>
<!-- 資源目錄(編譯時會將指定資源目錄中的內容複製到輸出目錄) -->
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!-- 是否開啓資源過濾(需要引入maven-resources-plugin插件)
| true:將用過濾資源(filters標籤)中的內容 替換 資源中相應的佔位符(${Xxxx})內容
| false:不做過濾替換操作 -->
<filtering>true</filtering>
</resource>
...
</build>
3.其他配置文件使用pom.xml的變量
這裏以src\main\resources\spring-config.xml文件爲例
<!--屬性配置文件引入 -->
<context:property-placeholder location="file:/${customConfigDir}/${project.artifactId}/config/jdbc.properties"/>
<!--引入其他文件 -->
<import resource="file:/${customConfigDir}/${project.artifactId}/config/spring-mybatis.xml"/>
4.指定配置文件的輸出
上面我們在spring-config.xml中配置了相應的路徑,但是工程並沒有生成對應路徑的文件
targetPath標籤可以指定輸出目錄
<build>
<!-- 項目資源清單(可以配置多個項目資源) -->
<resources>
<resource>
<!-- 資源目錄(編譯時會將指定資源目錄中的內容複製到輸出目錄) -->
<directory>src/main/resources</directory>
<includes>
<include>**/jdbc.properties</include>
<include>**/spring-mybatis.xml</include>
<include>**/spring-quartz.xml</include>
<include>**/spring-shiro.xml</include>
<include>**/*.lic</include>
</includes>
<!-- 輸出目錄(默認爲${build.outputDirectory},即target/classes) -->
<targetPath>${customConfigDir}\${project.artifactId}\config\</targetPath>
<!-- 是否開啓資源過濾(需要引入maven-resources-plugin插件)
| true:將用過濾資源(filters標籤)中的內容 替換 資源中相應的佔位符(${Xxxx})內容
| false:不做過濾替換操作 -->
<filtering>true</filtering>
</resource>
</resources>
...
</build>
Reference:
NULL
覺得文章對你有幫助,可以掃描二維碼捐贈給博主,謝謝!
如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033