使用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>
一路涉足、一路留恋、一路回望。依旧前行。