maven环境隔离搭建

maven环境隔离在企业级开发中是必要的,通过maven命令的不同打包方式打包,打包发布不同的环境,省去了,本地环境与线上环境不同要逐个改配置文件的麻烦。比如:本地数据库和线上数据库,用户名密码的不同。(不仅仅是这些哦)如果忘记修改这些文件,就发布上线,会造成一些不必要的麻烦。

一,创建环境隔离所需的包 如下图:

resources包是以前没隔离之前的包


二,把需要隔离的文件放到各个package中,如图:



三,在pom.xml配置中配置隔离环境

注意:在</dependencies>之外加 <build>节点

[html]  view plain  copy
  1. .....其他的坐标  
[html]  view plain  copy
  1. </dependencies>  
  2.   
  3.   
  4.   <build>  
  5.     <finalName>mmall</finalName>  
  6.     <plugins>  
  7.       <plugin>  
  8.         <groupId>org.mybatis.generator</groupId>  
  9.         <artifactId>mybatis-generator-maven-plugin</artifactId>  
  10.         <version>1.3.2</version>  
  11.         <configuration>  
  12.           <verbose>true</verbose>  
  13.           <overwrite>true</overwrite>  
  14.         </configuration>  
  15.       </plugin>  
  16.   
  17.       <!-- geelynote maven的核心插件之-complier插件默认只支持编译Java 1.4,因此需要加上支持高版本jre的配置,在pom.xml里面加上 增加编译插件 -->  
  18.       <plugin>  
  19.         <groupId>org.apache.maven.plugins</groupId>  
  20.         <artifactId>maven-compiler-plugin</artifactId>  
  21.         <version>2.3.2</version>  
  22.         <configuration>  
  23.           <source>1.8</source>  
  24.           <target>1.8</target>  
  25.           <encoding>UTF-8</encoding>  
  26.           <compilerArguments>  
  27.             <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>  
  28.           </compilerArguments>  
  29.         </configuration>  
  30.       </plugin>  
  31.     </plugins>  
  32.   
  33.     <!-<strong>-开始环境隔离!!!此节点在 <build>节点里面</strong>-->  
  34.     <span style="color:#ff0000;"><resources>  
  35.       <resource>  
  36.         <directory>src/main/resources.${deploy.type}</directory>  
  37.         <excludes>  
  38.           <!--排除jsp  jsp 不用环境隔离-->  
  39.           <exclude>*.jsp</exclude>  
  40.         </excludes>  
  41.       </resource>  
  42.       <resource>  
  43.         <directory>src/main/resources</directory>  
  44.       </resource>  
  45.   
  46.     </resources>  
  47.   
  48.   </build>  
  49.    <!--与build同级-->  
  50.   <profiles>  
  51.     <profile>  
  52.       <id>dev</id>  
  53.       <activation>  
  54.         <activeByDefault>true</activeByDefault> <!--如果没写打包方式 这个为默认的-->  
  55.       </activation>  
  56.       <properties>  
  57.         <deploy.type>dev</deploy.type>  
  58.       </properties>  
  59.     </profile>  
  60.   
  61.     <profile>  
  62.       <id>beta</id>  
  63.       <properties>  
  64.         <deploy.type>beta</deploy.type>  
  65.       </properties>  
  66.     </profile>  
  67.     <profile>  
  68.       <id>prod</id>  
  69.       <properties>  
  70.         <deploy.type>prod</deploy.type>  
  71.       </properties>  
  72.     </profile>  
  73.   </profiles></span>  

然后会出现这个:



四,运行命令

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




打包完成!!如下图




猜你喜欢

转载自blog.csdn.net/anshengsuiyeu/article/details/80194304