使用maven profile 实现不同环境打包

来源:互联网 发布:仿ios日历 js插件 编辑:程序博客网 时间:2024/05/21 09:17

maven 的build 配置可以在两处出现:

<span style="font-size:14px;"><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">   <!-- 项目级别的构建,基础配置 -->  <build>...</build>    <profiles>    <profile>      <!-- 特殊的构建 -->      <build>...</build>    </profile>  </profiles></project></span>
工程运行环境有研发,测试和生产,不同的运行环境配置不同。 maven可根据不同的环境打包不同的配置文件。
有两种方式:
1)、打包时指定文件目录,使用指定的文件夹下的配置文件。
2)、使用占位符,点位符使用指定的profile 中的properties 的key对应值替换,这样不同环境就可以加载不同的配置文件。
下面分别说明这两种情况。
1、指定打包过虑的文件目录
项目分为多个环境:研发(dev),测试(test),生产(product),演示(demo)。配置文件目录如下:


src/main/resources 中的配置文件是在开发时使用的。
src/main/filters   文件夹是不同环境的配置文件。
说明:不同环境的配置文件只是各环境特定的配置,公共可用的配置都是在src/main/resources 文件夹中,如:common.properties

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">    <modelVersion>4.0.0</modelVersion>    <groupId>com.masz.test</groupId>    <artifactId>package-test</artifactId>    <version>1.0.0</version>    <packaging>jar</packaging>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <profiles>        <profile>            <id>qa</id>            <properties>                <package.environment>qa</package.environment>            </properties>            <!-- 配置默认激活(打包时不指定 -P 参数使用此打包) -->            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <build>                <!-- 表示发布时要打包的资源文件,需要指明,这样才会打到包中去 -->                <resources>                    <resource>                        <directory>src/main/resources</directory>                        <includes>                            <include>**/*</include>                        </includes>                        <excludes> <!-- 不包含的文件(${package.environment})下面的文件 -->                            <exclude>${package.environment}/*</exclude>                        </excludes>                        <filtering>true</filtering>    <!-- 是否使用过滤器 -->                    </resource>                    <resource>                        <directory>src/main/filters/${package.environment}</directory>                        <includes>                            <include>**.*</include>                        </includes>                        <filtering>true</filtering>                    </resource>                </resources>                <finalName>${project.artifactId}</finalName><!-- 打包后的文件名 -->            </build>        </profile>    </profiles>    <!-- 全局build 配置 -->    <build>        <sourceDirectory>src/main/java</sourceDirectory>        <outputDirectory>target/classes</outputDirectory>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-surefire-plugin</artifactId>                <version>2.3</version>                <configuration>                    <includes>                        <include>**/*Test*.java</include>                    </includes>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.6</source>                    <target>1.6</target>                </configuration>            </plugin>        </plugins>    </build></project>
打包命令:
mvn clean package -Pqa -Dmaven.test.skip=true
-P 参数指定使用 profile 配置id 为qa 的build 参数打包。()

项目代码:http://download.csdn.net/detail/convict_eva/9638939


2、使用占位符
项目如下:


applicationContext.xml 配置如下:

    <bean id="configurationProperties"        class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="locations">            <list>                <value>classpath*:${env.props.file}</value><!-- 使用点位符 -->                <value>classpath*:common.properties</value>            </list>        </property>    </bean>
pom.xml 配置:

<!-- 指定profile 对应的属性 -->    <profiles>        <profile>            <id>demo</id>            <properties>                <env.props.file>filters/demo/application.properties</env.props.file>            </properties>        </profile>        <profile>            <id>dev</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <properties>                <env.props.file>filters/dev/application.properties</env.props.file>            </properties>        </profile>        <profile>            <id>product</id>            <properties>                <env.props.file>filters/product/application.properties</env.props.file>            </properties>        </profile>        <profile>            <id>qa</id>            <properties>                <env.props.file>filters/qa/application.properties</env.props.file>            </properties>        </profile>    </profiles>    <!-- 全局build 配置 -->    <build>        <resources>            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.xml</include>                </includes>            </resource>            <resource>                <directory>src/main/resources</directory>                <includes>                    <include>**/*.xml</include>                    <include>**/*.properties</include>                </includes>            </resource>            <resource>                <directory>src/main/resources</directory>                <filtering>true</filtering>            </resource>        </resources>    </build>

打包命令:
mvn clean package -Pqa -Dmaven.test.skip=true
打包后解压,占位符已经被替换。
注:配置文件可以单独为一个工程,项目依赖这个工程即可。这样也方便管理

项目代码:http://download.csdn.net/detail/convict_eva/9638941

0 0