Maven profile实现多环境配置

来源:互联网 发布:顽固软件卸载。 编辑:程序博客网 时间:2024/05/18 02:58
实际开发项目是需要配置多套环境配置的,如开发、测试、生产等。 在Maven中,有种多环境配置的方法,可以做到在开发、测试、运营的时候,使用各自对应的环境配置,可以大大的提高开发效率。

用个例子说明下。为了对比方便,这里创建了一个聚合项目,然后其中一个Project根据构建参数不同,加载不同配置。另一个Project不加载配置。目录如下:


这里有三个project,各自的pom如下:

mavenaggregator的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</groupId><artifactId>mavenaggregator</artifactId><!--  require pom if aggregator --><packaging>pom</packaging><version>1.0.0.1</version><name>my Maven Webapp</name><url>http://maven.apache.org</url><modules><!--  relative paths to the directories of pom -->    <module>my-project</module>    <module>another-project</module></modules></project>
my-project的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</groupId><artifactId>my-project</artifactId><packaging>jar</packaging><version>1.0.0.1</version><name>my-project</name><url>http://maven.apache.org</url><parent>        <groupId>com</groupId>        <artifactId>mavenaggregator</artifactId>        <version>1.0.0.1</version>    </parent></project>
another-project的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</groupId><artifactId>another-project</artifactId><packaging>jar</packaging><version>1.0.0.1</version><name>another-project</name><url>http://maven.apache.org</url><parent>        <groupId>com</groupId>        <artifactId>mavenaggregator</artifactId>        <version>1.0.0.1</version>    </parent></project>


为了方便,这里我将项目设置聚合加继承的结构,因此我们在最大Project的pom.xml 里面进行设置即可,这样子可以一次构建整个项目。

mavenaggregator的pom.xml中添加如下:

<!-- 其他代码 -->  <profiles><profile>          <!-- 本地开发打包 -->          <id>development</id>        <properties>        <!-- 为该环境下的build指定参数赋值 -->            <env>development</env>            <deploy.url>http://host:port/manager/text</deploy.url>        </properties>        <activation>        <!-- 默认激活的(activeByDefault为true) -->            <activeByDefault>true</activeByDefault>        </activation>    </profile>    <profile>          <id>production</id>        <properties>        <!-- 为该环境下的build指定参数赋值 -->            <env>production</env>            <deploy.url>http://host:port/manager/text</deploy.url>        </properties>        <activation>        <!-- 默认激活的(activeByDefault为true) -->            <activeByDefault>true</activeByDefault>        </activation>    </profile></profiles><!-- 其他代码 -->
通常为了开发方便,我们都是将本地开发激活 <activeByDefault>true</activeByDefault>,即当构建时没有参数时,默认使用本地开发配置。
下面看下环境的配置文件:

带有参数的 common的文件,这里是 config.properties

username=${db_username}password=${db_password}
而各个环境的参数配置,都在各自文件夹下,比如这里的 resource/development/build.properties 和 resource/production/build.properties 。
db_username=localdb_password=password1

db_username=productiondb_password=password1
因为我们这里只需要让这个子Project加载资源文件,因此只需要在my-project的pom.xml中添加如下配置:

<!-- 其他代码 -->        <build>    <plugins><plugin><!-- 处理资源文件。默认的主资源文件目录是src/main/resources,可以在此指定额外的资源文件目录 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.5</version><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins><resources>          <resource>        <!--                     资源文件位置src/main/resources/,这下面的资源文件的${}会全部被替换成filter中的标签内容。                    directory指定的value会作为classes的资源跟目录,                    比如指定:src/main/resources/,则classes下会出现jdbc等包,                    若指定:src/main/resources/jdbc/,则classes下直接出现jdbc包下的文件,不会额外出现jdbc等其他包结构。因为他把jdbc作为了根目录                -->            <directory>src/main/resources</directory>            <!-- 在某个resource中如果设置filtering为true,将会根据输入参数动态修改相关内容。 -->                <filtering>true</filtering>            <!-- 排除环境的配置资源根目录使用单独的资源目录来指定 -->            <excludes>                <exclude>production/*</exclude>                  <exclude>development/*</exclude>              </excludes>          </resource>        <!-- <resource> -->        <!-- 然后再加载使用单独的资源目录来指定 -->            <!-- <directory>src/main/resources/${env}</directory>   -->        <!-- </resource>   -->    </resources>    <filters>            <filter>src/main/resources/${env}/build.properties</filter>        </filters></build><!-- 其他代码 -->
最后,回到项目根目录下,执行maven命令即可:

mvn clean package -Pproduction

mvn clean package -Pdevelopment
此时再回到my-project的target 目录就会发现 config.properties 文件里面的参数都已经被指定环境的配置替换好了。

username=localpassword=password1
















阅读全文
0 0
原创粉丝点击