MavenProfile小知识

例子1.mysql

 <properties>sql

     <port>9105</port>数据库

  </properties>apache

 

  <profiles>tomcat

       <profile>maven

         <id>dev</id>ui

         <properties>url

           <port>9105</port>spa

         </properties>xml

       </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配置文件内容,已经替换为生产环境的值。

相关文章
相关标签/搜索