利用maven的profile功能直接打包不同环境的配置文件(附例子下载)

最近刚接手一个历史遗留的应用,打包的时候采用了很古老的方式,用maven 打包成war包,然后到生产环境解压之后,逐个替换配置文件中的值,但这个应用是分布式的,是有几个小应用组成的,所以一旦修改配置文件,得分别到几个应用里面修改。而且几个系统有依赖关系的。毕竟是分布式部署的。姑且不管如何部署,就修改配置文件这点来说,我就不满意了。因为产品生产环境也是我们自己可以控制的,所以完全可以在打包前就准备好。但由于生产环境与开发环境的配置是不一样的。所以完全可以提前准备好需要变化的值作为属性,在打包时去替换配置文件中的变量就好了,maven 的profile 就具备这样的功能,所以做了一个例子,给team 成员,让他们去效仿完成.在profile下建立几个文件夹,分别代表dev,sit,uat等环境用到的参数,配置在config.properties 文件中. 用这些配置去替换 src/main/resources/ 中的配置文件src/main/resources/ 中的配置文件

# database connection configurationsdriverClassName=${driverClassName}validationQuery=${validationQuery}#DEVELOPjdbc_url=${jdbc_url}jdbc_username=${jdbc_username}jdbc_password=${jdbc_password}

看 maven 的配置 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/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.isprint</groupId>  <artifactId>mavenReplaceTest</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>mavenReplaceTest Maven Webapp</name>  <url>http://maven.apache.org</url>   <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.16</version>        </dependency>    </dependencies>    <!-- Profiles configuration -->    <profiles>        <profile>            <id>dev</id>            <!-- Dev profile is active by default -->            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <properties>                               <build.profile.id>dev</build.profile.id>            </properties>            <build>                <filters>                                        <filter>profiles/${build.profile.id}/config.properties</filter>                </filters>                <resources>                                        <resource>                        <filtering>true</filtering>                        <directory>src/main/resources</directory>                        <!--                            You can also include only specific files found from the configured directory or                            exclude files. This can be done by uncommenting following sections and adding                            the configuration under includes and excludes tags.                        -->                        <!--                        <includes>                            <include></include>                        </includes>                        <excludes>                            <exclude></exclude>                        </excludes>                        -->                    </resource>                </resources>            </build>        </profile>        <profile>            <id>test</id>            <properties>                              <build.profile.id>test</build.profile.id>            </properties>            <build>                <filters>                                       <filter>profiles/${build.profile.id}/config.properties</filter>                </filters>                <resources>                                       <resource>                        <filtering>true</filtering>                        <directory>src/main/resources</directory>                                           </resource>                </resources>            </build>        </profile>            </profiles>    <build>        <finalName>maven-properties-filtering</finalName>        <plugins>            <!-- The configuration of maven-assembly-plugin -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-assembly-plugin</artifactId>                <version>2.2.1</version>                <!-- The configuration of the plugin -->                <configuration>                    <!-- Specifies the configuration file of the assembly plugin -->                    <descriptors>                        <descriptor>src/main/assembly/assembly.xml</descriptor>                    </descriptors>                </configuration>            </plugin>            <!-- The configuration of maven-jar-plugin -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-jar-plugin</artifactId>                <version>2.3.1</version>                <!-- The configuration of the plugin -->                <configuration>                    <!-- Configuration of the archiver -->                    <archive>                        <!-- Manifest specific configuration -->                        <manifest>                            <!-- Classpath is added to the manifest of the created jar file. -->                            <addClasspath>true</addClasspath>                            <!--                                Configures the classpath prefix. This configuration option is                                used to specify that all needed libraries are found under lib/                                directory.                            -->                            <classpathPrefix>lib/</classpathPrefix>                            <!-- Specifies the main class of the application -->                            <mainClass>HelloWorldApp</mainClass>                        </manifest>                    </archive>                </configuration>            </plugin>        </plugins>    </build></project>

在里面配置了 profiles, 分别为击中环境做了配置,其中默认了是dev , 所以如果在没有制定采用哪种环境的情况下,是用dev 的配置文件去替换,所以要注意.采用这种方式,配置适当的 resources 以及filter ,include, exclude ,几乎可以对任何文件做替换,一般常用的是 .properties, xml 文件,甚至 java 源代码都可以替换.提供源代码下载: maven replace properties files. 去陌生的街角,去做一切我们曾经或现在也很想做的事情,

利用maven的profile功能直接打包不同环境的配置文件(附例子下载)

相关文章:

你感兴趣的文章:

标签云: