zoukankan      html  css  js  c++  java
  • Maven profile 打包分环境加载不同的资源文件

    在实际开发项目中,常常有几种环境,一般情况下最少有三种环境:开发测试正式。

    各个环境之间的参数各不相同,比如mysql、等不同环境的host不一样,若每个环境都手动替换环境很容易出错,这里我们利用maven的profile功能切换环境。

    项目结构如下

    “dev” ---------------> 开发环境

    “product”----------->生产环境          开发和生产加载不同的数据库配置文件  db.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/maven-v4_0_0.xsd">
    <profiles>
            <profile>
                <id>dev</id>
                <properties>
                    <env>dev</env>
                </properties>
                <activation>
                    <activeByDefault>true</activeByDefault>    <!-- 默认加载开发环境的配置文件-->
                </activation>
            </profile>
            <profile>
                <id>product</id>
                <properties>
                    <env>product</env>
                </properties>
            </profile>
            <profile>
                <id>sandbox</id>
                <properties>
                    <env>sandbox</env>
                </properties>
            </profile>
    </profiles>
    
    。。。。。。。。。。。。。。
    
    <build>
            <finalName>ROOT</finalName>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        <plugins>
          <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <resourceEncoding>${project.encoding}</resourceEncoding>
                    </configuration>
            </plugin>
    。。。。。。。。。。。。
        </plugins>
      </build>

    applicationContext.xml 配置如下

     ${env}  -----> 打包时传入对应的profiles id 就会加载对应的配置文件。

    <context:property-placeholder ignore-resource-not-found="false" location="classpath:${env}/db.properties"/> 

     通过eclipse 打包, Profiles 输入product ,就会加载 product下的配置文件,默认的是开发环境(在pom的profiles中配置)

    使用maven  命令

    使用  mvn install -P{profile} 命令打包war
    example:     
    1、mvn install               没有指定profile,默认为dev
    2、mvn install -Pdev -Dmaven.test.skip=true   指定profile为dev并跳过测试

    域名购买.com 后缀好域名 

     https://mi.aliyun.com/shop/38040

  • 相关阅读:
    PHP获取当前服务器版本,Ip等详细信息
    Alipay 支付类
    php对接app支付宝支付出错Cannot redeclare Decrypt()
    Alipay支付宝支付 报错 invalid [default store dir]: /tmp/
    C# .net 高清压缩图片 合并图片方法
    csharp C#数字字符串排序orderby的问题解决
    tesseract 4.0 ocr图像识别利器,可识别文字。图片越高清越准确
    mysql update ...select的使用 & update 遇到 disable safe 的解决方法
    在Visual Studio 2013中安装Mysql for EntityFramework
    Mysql 中如何创建触发器
  • 原文地址:https://www.cnblogs.com/c9999/p/7942981.html
Copyright © 2011-2022 走看看