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配置文件內容,已經替換爲生產環境的值。

相關文章
相關標籤/搜索