• 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文件会变成对应环境的配置

  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/masahiro/p/10370874.html
Copyright © 2020-2023  润新知