使用Maven實現環境隔離


一、環境隔離的目的:

解決不同環境中配置存在的差異;

二、實際項目環境有以下幾種:

本地開發環境:Local

開發環境:Dev

測試環境:Beta

線上環境:Prod

三、隔離環境之間配置的差異:

如Ftp配置、數據庫配置等

四、Maven環境隔離解決的實際問題

1.避免人工修改的弊端,也就是說手動修改 系統配置時容易出現錯誤

2.根據不同環境進行 編譯、打包、部署

五、Maven環境配置及原理

在 pom.xml中build節點增加如下代碼:

<resources>
	<resource>
		<directory>
			src/main/resources.${deploy.type}				</directory>
		<excludes>
	        <exclude> *.jsp</exclude>
        </excludes>
     </resource>
     <resource>
     	<directory> src/main/resources</directory>
     </resource>
 </resources>
 
 *****************************************
 特別注意:<resources></resources>這一段要放在 <build></build>節點里面
 *********************************************

在pom.xml中增加profiles節點,指定環境,相當於SpringBoot中的spring.profile.active=dev 或prod

 *****************開發環境*******************
 <profiles>
 	<profile>
 		<id> dev </id>
 		<activation>
 		  <!--默認環境-->
 			<activeByDefault>true</activeByDefault>
 		</activation>
        <properties>
        	<deploy.type> dev </deploy.type>
        </properties>
    </profile>
        *******************測試環境******************

    <profile>
 		<id> dev </id>
        <properties>
        	<deploy.type> beta </deploy.type>
        </properties>
    </profile>  
    *******************生產環境******************
    <profile>
 		<id> dev </id>
        <properties>
        	<deploy.type> prod </deploy.type>
        </properties>
    </profile>    
 </profiles>
 

****************Maven環境隔離打包命令***************************
mvn clean package -Dmaven.test.skip=true  -Pdev或者是-Pprod  或者是 -Pbeta

六、Maven環境隔離目錄初始化

新建對應的文件夾,並且把要隔離的文件分開,公共的留下

公共的為 resources

創建不同環境下的 不同文件夾

七、Maven環境隔離IDEA中設置默認環境

八、Maven環境隔離 編譯、打包命令

-P${環境標識} Dev 、Prod、Beta

命令如下:

mvn clean package -Dmaven.test.skip=true -Pprod

執行打包命令有很多種方式:

方式1:在 idea控制台 Terminal 執行,如圖所示

方式2:在Windows命令窗口執行,如下圖,記得一定要進入到項目所在 目錄下

九、在 Idea中發布項目

在idea中發布項目時,需要勾選下面的環境,然后 導入依賴,import Changes ,然后運行Tomcat,直接就發布過去了,此時到target/classes下面找,就可以看到了

十、完整 Pom文件

<?xml version="1.0" encoding="UTF-8"?>

<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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mmall.study</groupId>
  <artifactId>mmall-study</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mmall-study Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
    <org.mybatis.version>3.4.1</org.mybatis.version>
    <org.mybatis.spring.version>1.3.0</org.mybatis.spring.version>
  </properties>

  <dependencies>
    <!--tomcat-servlet-api-->
    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-servlet-api</artifactId>
      <version>7.0.64</version>
    </dependency>
    <!--Spring框架-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <!--SpringMVC 框架-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.3</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${org.mybatis.spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${org.mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.11</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.12</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
      <!--<scope>runtime</scope>-->
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>20.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.5</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.1</version>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.3</version>
    </dependency>
    <!-- id加密解密 -->
    <dependency>
      <groupId>org.hashids</groupId>
      <artifactId>hashids</artifactId>
      <version>1.0.1</version>
    </dependency>
    <!-- ftpclient -->
    <dependency>
      <groupId>commons-net</groupId>
      <artifactId>commons-net</artifactId>
      <version>3.1</version>
    </dependency>
    <!-- file upload -->

    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.2.2</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.0.1</version>
    </dependency>
    <!-- mybatis pager -->

    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.1.0</version>
    </dependency>

    <dependency>
      <groupId>com.github.miemiedev</groupId>
      <artifactId>mybatis-paginator</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>com.github.jsqlparser</groupId>
      <artifactId>jsqlparser</artifactId>
      <version>0.9.4</version>
    </dependency>
    <!-- alipay 支付寶-->
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.10</version>
    </dependency>
    <dependency>
      <groupId>commons-configuration</groupId>
      <artifactId>commons-configuration</artifactId>
      <version>1.10</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>2.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
    </dependency>

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <!--引入支付寶Demo中帶的jar包依賴,其實也可以不用引,-->
    <!--為了防止版本不一致引起不必要的麻煩,還是引進來,版本必須保持一致-->
    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <!--該依賴是用於生成二維碼的-->
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.10</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration -->
    <!--配置相關的依賴-->
    <dependency>
      <groupId>commons-configuration</groupId>
      <artifactId>commons-configuration</artifactId>
      <version>1.10</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>2.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
      <scope>test</scope>
    </dependency>
<!--以上公共jar包都是出自谷歌-->


  </dependencies>

  <build>
    <finalName>mmall-study</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <encoding>UTF-8</encoding>
            <compilerArguments>
              <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
            </compilerArguments>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <!--逆向工程插件,把mybatis-generator單另提出來放到plugins中,plugins和pluginManager放到同一級,不然在Maven中看不到該插件-->
 <plugins>
   <plugin>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-maven-plugin</artifactId>
     <version>1.3.2</version>
     <configuration>
       <verbose>true</verbose>
       <overwrite>true</overwrite>
     </configuration>
   </plugin>
 </plugins>
    <!--添加環境隔離配置-->
    <!--第一步:在<build></build>節點中添加<resources></resources>節點-->
    <resources>
      <resource>
        <directory>
          src/main/resources.${deploy.type}
        </directory>
        <!--排除 jsp文件,因為它里面不包含配置型數據,可以存在於任意環境下,所以要排除-->
        <excludes>
          <exclude>*.jsp</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>
          <!--不同環境下可以公用的配置文件-->
          src/main/resources
        </directory>
      </resource>
    </resources>
  </build>
    <!--特別注意: <profiles></profiles>標簽和<build></build>是同級的-->
  <profiles>
    <!--開發環境-->
    <profile>
      <id>dev</id>
      <activation>
        <!--是否指定為默認 環境,如果該屬性為true,則為默認環境-->
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <!--自定義屬性 deploy.type 表示環境類型,如dev,prod,beta-->
        <deploy.type>dev</deploy.type>
      </properties>
    </profile>
    <!--測試環境-->
    <profile>
      <id>beta</id>
      <!--因為已經指定默認環境為 dev了,所以其他的就不需要標簽了<activation></activation>-->
      <properties>
        <!--自定義屬性 deploy.type 表示環境類型,如dev,prod,beta-->
        <deploy.type>beta</deploy.type>
      </properties>
    </profile>
    <!--生產環境-->
    <profile>
      <id>prod</id>
      <!--因為已經指定默認環境為 dev了,所以其他的就不需要標簽了<activation></activation>-->
      <properties>
        <!--自定義屬性 deploy.type 表示環境類型,如dev,prod,beta-->
        <deploy.type>prod</deploy.type>
      </properties>
    </profile>
  </profiles>
</project>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM