zoukankan      html  css  js  c++  java
  • Maven中使用描述文件切换环境配置

    首先在项目中定义一个属性文件,如这里的数据库配置文件 database.properties

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://xxx:3306/xxx?useUnicode=true&characterEncoding=utf-8
    jdbc.username=xxx
    jdbc.password=xxx

    在其他配置文件中引用,如Spring的配置文件 applicationContext.xml

        <!-- 引入properties文件 -->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>classpath:database.properties</value>
            </property>
        </bean>
        <!-- 配置DataSource -->
        <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>

    接下来创建各个环境的属性配置文件,这里创建的配置文件是用来覆盖前面的database.properties文件的,所以文件中的变量名需要保持一致,只需要变更各个参数值即可达到变更环境。

    如这里在srcmain esourcesenv下创建了几个环境的配置文件

    • dev.properties
    • prd.properties
    • pre.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/xsd/maven-4.0.0.xsd">
      <build>
        <filters>
            <filter>src/main/resources/env/${env}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>database.properties</include>
                </includes>
            </resource>
        </resources>
      </build>
      <profiles>
          <profile>
              <id>dev</id>
              <activation>
                  <activeByDefault>true</activeByDefault>
              </activation>
              <properties>
                  <env>dev</env>
              </properties>
          </profile>
          <profile>
              <id>pre</id>
              <properties>
                  <env>pre</env>
              </properties>
          </profile>
          <profile>
              <id>prd</id>
              <properties>
                  <env>prd</env>
              </properties>
          </profile>
      </profiles>
    </project>

    至此配置完毕,刷新Maven的面板后会发现多出一个Profiles文件夹,在里面勾选不同的环境后编译出的database.properties文件会变成对应环境的配置

  • 相关阅读:
    JSON.parse()与JSON.stringify()的区别
    响应式布局
    document.selection
    jQuery $.proxy() 方法
    <转> 键值表
    jquery-jqzoom 插件 用例
    jquery 笔记
    前端表单验证常用的15个JS正则表达式<转>
    css 问题解决
    <转>break与continue
  • 原文地址:https://www.cnblogs.com/masahiro/p/10370874.html
Copyright © 2011-2022 走看看