MavenProfile小知识

例子1.

 <properties>

     <port>9105</port>

  </properties>

  <profiles>

       <profile>

         <id>dev</id>

         <properties>

           <port>9105</port>

         </properties>

       </profile>

       <profile>

         <id>pro</id>

         <properties>

           <port>9205</port>

         </properties>

       </profile>  

  </profiles>

  <build>  

  <plugins>      

      <plugin>

    <groupId>org.apache.tomcat.maven</groupId>

    <artifactId>tomcat7-maven-plugin</artifactId>

    <version>2.2</version>

    <configuration>

    <!-- 指定端口 -->

      <port>${port}</port>

     <!-- 请求路径 -->

      <path>/</path>

    </configuration>

     </plugin>

  </plugins>  

    </build>

  

执行命令 tomcat7:run -P pro  发现以9205端口启动

执行命令 tomcat7:run -P dev  发现以9105端口启动

-P 后边跟的是profileid

如果我们只执行命令tomcat7:run ,也是以9105启动,因为我们一开始定义的变量值就是9105,就是在不指定profileID时的默认值.


 例子2.编译不同环境的配置文件

filter文件夹下创建db_dev.properties ,用于配置开发环境用到的数据库

env.jdbc.driver=com.mysql.jdbc.Driver

env.jdbc.url=jdbc:mysql://localhost:3306/**_devdb?characterEncoding=utf-8

env.jdbc.username=root

env.jdbc.password=123456

filter文件夹下创建db_pro.properties   用于配置生产环境用到的数据库

env.jdbc.driver=com.mysql.jdbc.Driver

env.jdbc.url=jdbc:mysql://localhost:3306/**_prodb?characterEncoding=utf-8

env.jdbc.username=root

env.jdbc.password=123456

修改properties下的db.properties

jdbc.driver=${env.jdbc.driver}

jdbc.url=${env.jdbc.url}

jdbc.username=${env.jdbc.username}

jdbc.password=${env.jdbc.password}

修改pom.xml

  <properties>

   <env>dev</env>

  </properties>

  <profiles>

   <profile>

   <id>dev</id>

   <properties>

   <env>dev</env>

   </properties>

   </profile>    

   <profile>

   <id>pro</id>

   <properties>

   <env>pro</env>

   </properties>

   </profile>

  </profiles>

//资源过滤与变量替换

//这里我们利用filter实现对资源文件(resouces) 过滤 

   <filters>

   <filter>src/main/resources/filters/db_${env}.properties</filter>

   </filters>

   <resources>

   <resource>

   <directory>src/main/resources</directory>

   <filtering>true</filtering>

   </resource>  

   </resources>

执行命令:package -P pro ,  解压生成的jar包,观察db.properties配置文件内容,已经替换为生产环境的值。

猜你喜欢

转载自www.cnblogs.com/code-farmers/p/9317986.html