zoukankan      html  css  js  c++  java
  • MAVEN实现多环境搭建

    在实际的开发中,会遇到开发环境的不同(开发环境,测试环境,线上环境),会来回根据环境的不同修改配置文件,一不小心修改错误导致无法正常运行,故障排除导致开发效率低。使用maven可以根据环境的不同,自动生成需要的配置文件,减少因为配置文件配置错误导致的人为故障

    实现环境:j2ee环境

    dev.properties配置文件的内容

    db.username=name_dev
    db.password=password_dev

    local.properties配置文件的内容

    db.username=name_local
    db.password=password_local

    config.properties配置文件的内容

    db.username=${db.username}
    db.password=${db.password}

    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">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.major</groupId>
      <artifactId>mavenTest</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>mavenTest Maven Webapp</name>
      
      <!-- 项目依赖包 -->
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      
      <!-- 项目配置文件 -->
        <profiles>
            <profile>
            <!-- 定义local配置文件 -->
                <id>local</id>
                <properties>
                    <!-- 定义变量package.env为local,为后面调用 -->
                    <package.dev>local</package.dev>
                </properties>
            </profile>
            <profile>
                <id>dev</id>
                <properties>
                    <package.dev>dev</package.dev>
                </properties>
            </profile>
        </profiles>
    
        <!-- 打包配置 -->
        <build>
            <plugins>
                <!-- 配置打包插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <!-- 配置jdk版本 -->
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                
                <!-- 配置war插件 -->
                 <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-war-plugin</artifactId>  
                    <version>2.1.1</version>  
                    <configuration>  
                        <archive>  
                            <addMavenDescriptor>false</addMavenDescriptor>  
                        </archive>  
                        <warName>${project.artifactId}</warName>  
                        <!-- 排除不需要的配置文件,如果不排除env目录下的配置文件,会导致env目录下的配置文件也会打包到war包中 -->
                        <excludes>src/env</excludes>
                    </configuration>  
                </plugin>  
            </plugins>
            
            <!-- 配置资源文件 -->
            <resources>
                <resource>
                    <!-- 该资源目录,其中${basedir}表示maven项目根目录 -->
                    <directory>${basedir}/src/main/resources</directory>
                    <!-- 包含文件,如果不过滤文件,可以使用*代替所有 -->
                    <includes>
                        <include>config.properties</include>
                    </includes>
                    <!-- 是否对包括的文件进行过滤(就是使用配置文件中的内容替换改文件中的变量) -->
                    <filtering>true</filtering>
                </resource>
            </resources>
            
            <!-- 配置文件过滤 -->
            <filters>
                <!-- 使用该配置文件内容替换资源文件,其中 ${package.dev}为上面定义的变量,如果选择dev则为dev.properties中的内容来替换-->
                <filter>
                    ${basedir}/src/env/${package.dev}.properties
                </filter>
            </filters>
        </build>
    
    </project>

    开始maven编译:

    右击项目-->maven-->select maven profiles选择配置文件

    右击项目-->run as --> maven install

    控制台输出信息

    这个时候可以在该项目的目录下的target目录中找到编译好的war包

    打开可以看到配置文件已经自动完成替换

    到此完成maven多环境配置搭建,如果是开发环境,可以选择local,如果时线上环境,可以选择dev,当然有可以添加多个适应多个开发环境的使用

  • 相关阅读:
    Python中的BeautifulSoup模块
    requests模块
    requests模块
    python中让输出不换行
    python中让输出不换行
    我为Dexposed续一秒——论ART上运行时 Method AOP实现
    Python2中的urllib、urllib2和 Python3中的urllib、requests
    Python2中的urllib、urllib2和 Python3中的urllib、requests
    Fidder抓包软件的使用
    Fidder抓包软件的使用
  • 原文地址:https://www.cnblogs.com/djoker/p/7360583.html
Copyright © 2011-2022 走看看