zoukankan      html  css  js  c++  java
  • POM中profile配置maven根据不同的运行环境,打包不同的配置文件

    1.首先在pom添加如下profile配置

    <profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>
    

    2.工程目录src/main/resources中建立对应配置

    如开发环境、测试环境、生产环境的配置文件分别放到src/main/resources目录下的dev、test、pro三个子目录中,剩余公共的配置文件放于resources目录下,如dev的config.properties:

    hibernate.dialect=org.hibernate.dialect.OracleDialect
    hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
    hibernate.connection.username=platform_portal_service
    hibernate.connection.password=kingstar
    #hibernate.connection.url=jdbc:oracle:thin:@127.0.01:1521:orcl
    hibernate.connection.url=jdbc:oracle:thin:@127.0.01:1521/dev
    

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

    <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <!-- 资源根目录排除各环境的配置,防止在生成目录中多余其它目录 -->
                    <excludes>
                        <exclude>test/*</exclude>
                        <exclude>pro/*</exclude>
                        <exclude>dev/*</exclude>
                    </excludes>
                </resource>
                <resource>
                    <directory>src/main/resources/${profiles.active}</directory>
                </resource>
            </resources>
        </build>
    

    首先第一个资源文件位置src/main/resources需要排队提各个环境的配置文件,各个环境的配置我们在第二个节点中通过前面在profile中配置的profiles.active属性来指定。即src/main/resources/${profiles.active}。这样在激活指定的profile时,会加载指定目录下的配置文件,如当前激活的是pro profile,那么这个资源目录就是src/main/resources/pro。这样就达到了不同环境加载不同配置的目的

    4.项目编译生成

    mvn clean package -Pdev即构建出生产环境需要的包

  • 相关阅读:
    替换TStrings
    WordPress数据备份方案
    图像反色
    通过网络复制文件
    SQL Server的patindex和charindex的用法
    C冒泡排序 @100到200素数
    正则。IP验证
    C以二进制读、写、文本
    HTML下拉框、文本框、复选框!
    HTM页面获得本机时间
  • 原文地址:https://www.cnblogs.com/bbbing/p/11011411.html
Copyright © 2011-2022 走看看