maven根據profile讀取指定環境的配置檔案
<profiles>
<!--default開發分支-->
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<
</filters>
<resources>
<resource>
<directory>src/main/resources/common/</directory>
<filtering>true</filtering
</resource>
<resource>
<directory>src/main/resources/local/</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<filters>
<filter>src/main/resources/test/deploy.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources/common/</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/test/</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
- 先指定 src/main/resources下所有檔案及資料夾為資原始檔 在不同的profile下資原始檔不同<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<includes>
<include>deploy/*</include>
</includes>
<targetPath>${project.build.directory}</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
deploy資料夾下這些檔案中的${key}會被替換掉為真正的值
#!/bin/bash
base="${deploy.base}"
name="${project.name}"
java_home="${deploy.java.home}"
bin_home="${deploy.bin.home}"
conf_home="${deploy.conf.home}/resin"
deploy="${deploy.base}"
webapp="${deploy}/${project.name}"
servers=(${deploy.。。.server})
resin="${deploy.resin.home}"
而${deploy.base} ..這些是在deploy.properites中定義
詳解
我們分析下<resource>下面的屬性
<directory>: 配置那個目錄下的檔案通過${key}會被替換成屬性值,resource目錄下,我們一般放jdbc連線,或配置檔案
<includes>:指定那個目錄下那個檔案
<filtering>:這個配置的意思是過濾上面指定屬性檔案中的佔位符,佔位符是${變數名稱}這樣的形式,maven會自動讀取配置檔案,然後解析其中的佔位符,使用上面pom檔案中定義的屬性進行替換
<exclueds>:在resource目錄下,有很多檔案,但用些檔案不希望替換,則可以通過<excluede>指定
<filters>:這裡的filters與<profile>的filter意思一樣,都是指屬性檔案地址,這個如果上面定義<profile>的時候指定了,這裡就不需要了,但有些開發習慣是在<profile>不定義,然後在<build>裡指定。
在dispather-servlet下
<context:component-scanbase-package="com.。。。.webapp"/>
<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="locations">
<list>
<value>classpath:dubbo.properties</value>
</list>
</property>
</bean>
只有簡單配置這些,在使用maven命令的時候 mvn clean package -PprofileId ,就可以根據不同環境打不同的包了