使用maven profile实现多环境可移植构建

简介: 配置profile这里定义了三个环境,分别是development(开发环境)、test(测试环境)、production(生产环境),其中开发环境是默认激活的(activeByDefault为true),这样如果在不指定profile时默认是开发环境。

配置profile

这里定义了三个环境,分别是development(开发环境)、test(测试环境)、production(生产环境),其中开发环境是默认激活的(activeByDefault为true),这样如果在不指定profile时默认是开发环境。
同时每个profile还定义了两个属性,其中profiles.active表示被激活的profile的名称,deploy.url表示发布服务器的地址。我们需要在下面使用到这两个属性。
另外host和port分别是发布服务器的主机地址和端口号。
首先是profile配置,在pom.xml中添加如下profile的配置:

    <profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>development</id>
            <properties>
                <profiles.active>development</profiles.active>
            </properties>

        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境136 -->
            <id>production136</id>
            <properties>
                <profiles.active>production136</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 生产环境207 -->
            <id>production207</id>
            <properties>
                <profiles.active>production207</profiles.active>
            </properties>

        </profile>
    </profiles>

配置文件目录

针对不同的环境,我们定义不同的配置文件,而这些配置文件都做为资源文件放到maven工程的resources目录下,即src/main/resources目录下,且各个环境的配置分别放到相应的目录下,而所有环境都公用的配置,直接放到src/main/resources目录下即可。如下图所示:

这里写图片描述

maven资源插件配置

在pom中的build节点下,配置资源文件的位置,如下所示:

        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
                <!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->  
                <excludes>  
                    <exclude>test/*</exclude>  
                    <exclude>production136/*</exclude>
                    <exclude>production207/*</exclude>  
                    <exclude>development/*</exclude>  
                </excludes>  
            </resource>  
            <resource>  
                <directory>src/main/resources/${profiles.active}</directory>  
            </resource>  
        </resources>  

构建与发布

下图中根据发布需要更改activation的位置,如下加入我需要发布到生产环境136的服务器,那么activation的配置放在如下位置即可。
这里写图片描述
修改好activation的位置后,依次执行run as Maven clean—> run as Maven install
生产的war包会打印在console末尾,把war包上传到相应服务器即可。

目录
相关文章
|
3月前
|
Java 测试技术 应用服务中间件
使用Maven构建一个项目时,通常按照以下六个步骤的顺序进行
使用Maven构建一个项目时,通常按照以下六个步骤的顺序进行
103 0
|
24天前
|
Java Maven
Maven【5】在IDEA环境中配置和使用Maven
Maven【5】在IDEA环境中配置和使用Maven
57 1
|
2月前
|
Java Maven
【Maven从入门到如土】如何在Maven工程中编写代码并执行构建
【Maven从入门到如土】如何在Maven工程中编写代码并执行构建
37 0
|
2月前
|
存储 JavaScript Java
【Maven从入门到如土】快速使用 Maven:命令行环境
【Maven从入门到如土】快速使用 Maven:命令行环境
34 0
|
3月前
|
存储 Java 关系型数据库
深入探索Maven:优雅构建Java项目的新方式(二)
深入探索Maven:优雅构建Java项目的新方式(二)
|
3月前
|
Java Maven
深入探索Maven:优雅构建Java项目的新方式(一)
深入探索Maven:优雅构建Java项目的新方式(一)
|
3月前
|
存储 Java 测试技术
深入 Maven:构建杰出的软件项目的完美工具
深入 Maven:构建杰出的软件项目的完美工具
|
3月前
|
前端开发 Java Maven
Spring-Spring MVC + Spring JDBC + Spring Transaction + Maven 构建web登录模块
Spring-Spring MVC + Spring JDBC + Spring Transaction + Maven 构建web登录模块
60 0
|
25天前
|
Java Maven
手把手教你搭建Maven项目
手把手教你搭建Maven项目
26 0
|
2月前
|
Java Maven
java修改当前项目的maven仓库地址为国内
修改当前项目的maven仓库地址为国内

推荐镜像

更多