Maven properties属性进阶--资源过滤

来源:互联网 发布:郑州淘宝美工培训机构 编辑:程序博客网 时间:2024/06/05 20:17

maven的properties filter功能可以帮你自动替换配置文件中以${}包裹的变量。为了方便构建不同的环境,我们通常将不同的配置以properties形式配置在pom 中。默认情况下,Maven属性只有在POM中才会被解析。

资源过滤就是指让Maven属性在资源文件(src/main/resources、src/test/resources)中也能被解析。
在POM中添加下面的配置便可以开启资源过滤

<build>      <resources>          <resource>              <directory>${project.basedir}/src/main/resources</directory>              <filtering>true</filtering>          </resource>      </resources>      <testResources>          <testResource>              <directory>${project.basedir}/src/test/resources</directory>              <filtering>true</filtering>          </testResource>      </testResources>  </build>  

同时,通过不同的打包命令,也能实现在开发环境和发布环境中不同配置的设置

<properties>        <!-- 开发阶段配置文件位置, 发布时打包加参数:-P product -->        <scheduler.jdbc.config.path>classpath:config/jdbc.properties</scheduler.jdbc.config.path>        <sys.jdbc.config.path>classpath:config/jdbc.properties</sys.jdbc.config.path>        <system.config.path>classpath:config/system.properties</system.config.path></properties>

在profiles节点配置生产配置

<profiles>        <!-- 发布时打包命令 clean package -P build tomcat7:run-war-only -->        <profile>            <id>product</id>            <properties>                <scheduler.jdbc.config.path>file:/home/config/base/jdbc-scheduler.properties</scheduler.jdbc.config.path>                <sys.jdbc.config.path>file:/home/config/base/jdbc-sys.properties</sys.jdbc.config.path>                <system.config.path>file:/home/config/base/system.properties</system.config.path>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/java</directory>                        <includes>                            <include>**/*.properties</include>                            <include>**/*.xml</include>                        </includes>                        <filtering>true</filtering>                    </resource>                    <resource>                        <directory>src/main/resources</directory>                    </resource>                </resources>            </build>        </profile>        <profile>            <id>build</id>            <build>                <finalName>${project.name}</finalName>                <resources>                    <resource>                        <directory>src/main/java</directory>                        <includes>                            <include>**/*.properties</include>                            <include>**/*.xml</include>                        </includes>                        <filtering>true</filtering>                    </resource>                    <resource>                        <directory>src/main/resources</directory>                    </resource>                </resources>            </build>        </profile></profiles>

通过不同的打包命令,可打包成不同的配置:
mvn clean package -P product
mvn clean package -P build

配置好之后,在maven项目中都可以使用${sys.jdbc.config.path..等变量,支持过滤的xml和properties文件:

<bean class="org.xx.xx.PropertiesUtil">        <property name="locations">            <list>                <value>${sys.jdbc.config.path}</value>                <value>${system.config.path}</value>            </list>        </property></bean>

Maven除了可以对主资源目录、测试资源目录过滤外,还能对Web项目的资源目录(如css、js目录)进行过滤。这时需要对maven-war-plugin插件进行配置

<plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-war-plugin</artifactId>      <version>2.1-beta-1</version>      <configuration>          <webResources>              <resource>                  <filtering>true</filtering>                  <directory>src/main/webapp</directory>                  <includes>                      <include>**/*.css</include>                      <include>**/*.js</include>                  </includes>              </resource>          </webResources>      </configuration>  </plugin> 
原创粉丝点击