zoukankan      html  css  js  c++  java
  • Maven配置测试和开发环境连接不同数据库

    ##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">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.gkwind</groupId>
      <artifactId>testmaven</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>maven test project</name>
      
    <!-- 测试配置maven profile 来配置数据库 start-->
      <profiles>
      	<profile>
      		<id>testDev</id>
      		<properties>
      			<db.driver>com.mysql.jdbc.Driver</db.driver>
      			<db.url>jdbc:mysql://192.168.1.100:3306/test</db.url>
      			<db.username>testDev</db.username>
      			<db.password>thisispwd</db.password>
      		</properties>
      	</profile>
      	<profile>
      		<id>testDev2</id>
      		<properties>
      			<db.driver>com.mysql.jdbc.Driver</db.driver>
      			<db.url>jdbc:mysql://192.168.1.100:3306/test2</db.url>
      			<db.username>testDev2</db.username>
      			<db.password>thisispwd2</db.password>
      		</properties>
      		  	<!-- 默认启用这个profile -->
    		<activation>
    			<activeByDefault>true</activeByDefault>
    		</activation>
      	</profile>
      </profiles>
      
      <build>
        <!-- 测试配置maven profile 来配置数据库  定义resources过滤 start -->
    	<resources>
    		<resource>
    			<directory>${project.basedir}/src/main/resources</directory>
    			<filtering>true</filtering>
    		</resource>
    	</resources>
    	<testResources>
    		<testResource>
    			<directory>${project.basedir}/src/test/resources</directory>
    			<filtering>true</filtering>
    		</testResource>
    	</testResources>
        <!-- 测试配置maven profile 来配置数据库 定义resources过滤 end -->
      <!-- 测试配置maven profile 来配置数据库 end-->
        
        
      	<plugins>
    
    		<!-- 打包源码 -->
    		<plugin>
    			<groupId>org.apache.maven.plugins</groupId><!-- 官方插件可以省略该行 -->
    			<artifactId>maven-source-plugin</artifactId>
    			<version>2.1.1</version>
    		</plugin>
    	
    		<plugin>
    			<groupId>org.apache.maven.plugins</groupId>
    			<artifactId>maven-compiler-plugin</artifactId>
    			<version>3.1</version>
    			
    			<configuration>
    				<!-- 使用1.7编译 -->
    				<source>1.7</source>
    				<target>1.7</target>
    			</configuration>
    		</plugin>
    		
      	</plugins>
      </build>
    
      </project>
    

    对应的要替换的变量
    这里写图片描述

    最终根据命令来决定不同的值。
    命令行激活profile. (也可以通过配置setting.xml中 activityProfile来激活–maven实战p256)
    mvn clean package -PtestDev2
    这里写图片描述
    打开包里面的jdbc.properties,已经被替换了
    这里写图片描述

    也可以配置其他属性,不会被替换

    此外,还可以配置 当某个文件文件/系统环境变量为xx时启用 ,配置不同的webapp下的资源文件

    参考:《maven实战》
    参考:https://www.cnblogs.com/yanduanduan/p/5340039.html
    其他 :https://blog.csdn.net/zzq900503/article/details/77163899

  • 相关阅读:
    shell 知识点
    folder,source folder,package 区别
    meta viewport 理解
    一张图看懂JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
    java 报错及解决
    Charles :mac上的手机代理
    关联本地文件夹到github项目
    tomcat 安装
    Refused to display '[url]' in a frame because it set 'X-Frame-Options' to 'Deny'.
    linux 知识点
  • 原文地址:https://www.cnblogs.com/thewindkee/p/12873205.html
Copyright © 2011-2022 走看看