Maven 結合 Spring profile對不一樣的部署環境打包部署

這是一個草雞雞凍人心的時刻,搞了2天終於搞定了,麻麻不再用擔憂我部署出錯了!!!!!!!java

全部profile,spring和maven的,定義均要一致,不然,本身運行看看。web

首先,先來說下spring的profile功能,這個是方便項目的各類環境分離(開發、測試、生產),簡單介紹下如何使用。spring

  1. 在beans中定義環境代碼,項目中,我在beans.xml裏定義數據庫

    1
    <beans profile= "develop,test,product" ></beans>

           

           

       

  2. 在數據庫配置文件中,加入三種profile的各自定義鏈接,我這裏的配置是把test,develop配置設爲同樣,固然也能夠區別開來,看實際環境,這裏每一個profile裏面的配置都要對稱,不然會出錯哦~apache

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <beans profile= "test,develop" >
             <bean id= "dataSource"   class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
                 <property name= "driverClassName"   value= "oracle.jdbc.driver.OracleDriver" ></property>
                 <property name= "url"   value= "jdbc:oracle:thin:@localhost:1521:ora10g" ></property>
                 <property name= "username"   value= "username" ></property>
                 <property name= "password"   value= "password" ></property>
             </bean>
             <bean id= "dataSourceHR"
                 class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
                 <property name= "driverClassName"   value= "oracle.jdbc.driver.OracleDriver" ></property>
                 <property name= "url"   value= "jdbc:oracle:thin:@localhost:1521:ora10g" ></property>
                 <property name= "username"   value= "username" ></property>
                 <property name= "password"   value= "password" ></property>
             </bean>
         </beans>
         <beans profile= "product" >
             <bean id= "dataSource"   class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
                 <property name= "driverClassName"   value= "oracle.jdbc.driver.OracleDriver" ></property>
                 <property name= "url"   value= "jdbc:oracle:thin:@localhost:1521:ora11g" ></property>
                 <property name= "username"   value= "username" ></property>
                 <property name= "password"   value= "password" ></property>
             </bean>
             <bean id= "dataSourceHR"
                 class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
                 <property name= "driverClassName"   value= "oracle.jdbc.driver.OracleDriver" ></property>
                 <property name= "url"   value= "jdbc:oracle:thin:@localhost:1521:ora10g" ></property>
                 <property name= "username"   value= "username" ></property>
                 <property name= "password"   value= "passwd" ></property>
             </bean>
         </beans>

           

           

      

  3. 而後就是修改web.xml啦,添加如下代碼便可tomcat

           

    1
    2
    3
    4
    <context-param>
         <param-name>spring.profiles.active</param-name>
         <param-value>develop</param-value>
    </context-param>

           

       

這樣修改後,切換web.xml裏的param-value值就能夠實現環境切換。服務器

可是呢,這樣子就出現一種狀況,每次用maven打包都不得不修改web.xml,萬一忘記修改web.xml就發佈出去,會引發不可想象的狀況,因此就想到了用maven集成spring profile,網上搜羅了不少,總結了下而且實際操做演練後,發現是可行滴,如下就是整合maven profile的具體步驟oracle

 

在pom.xml最下面 build下添加profileapp

id:spring中配置的profile名稱webapp

profiles.activation中配置的要和id一致

build中配置的是項目部署路徑,用戶名密碼等信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<profiles>
        <profile>
            <id>test</id>
            <properties>
                <profiles.activation>test</profiles.activation>
            </properties>
            <build> 
                 <plugins> 
                     <plugin> 
                         <groupId>org.codehaus.mojo</groupId> 
                         <artifactId>tomcat-maven-plugin</artifactId> 
                         <version> 1.1 </version> 
                         <configuration> 
                             <!-- 配置項目自動發佈服務器 --> 
                            <url>http: //intimeit222:8080/manager</url> 
                             <server>admin</server> 
                         </configuration> 
                     </plugin> 
                 </plugins> 
             </build> 
        </profile>
        <profile>
            <id>develop</id>
            <properties>
                <profiles.activation>develop</profiles.activation>
            </properties>
            <activation>
                 <activeByDefault> true </activeByDefault>
             </activation>
            <build> 
                 <plugins> 
                     <plugin> 
                         <groupId>org.codehaus.mojo</groupId> 
                         <artifactId>tomcat-maven-plugin</artifactId> 
                         <version> 1.1 </version> 
                         <configuration> 
                             <!-- 配置項目自動發佈服務器 --> 
                             <url>http: //intimeit111:8080/manager</url> 
                             <server>admin</server> 
                         </configuration> 
                     </plugin> 
                 </plugins> 
             </build> 
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <profiles.activation>product</profiles.activation>
            </properties>
            <build> 
                 <plugins> 
                     <plugin> 
                         <groupId>org.codehaus.mojo</groupId> 
                         <artifactId>tomcat-maven-plugin</artifactId> 
                         <version> 1.1 </version> 
                         <configuration> 
                             <!-- 配置項目自動發佈服務器 --> 
                             <url>http: //intimeit000:8080/manager</url>
                             <server>admin</server> 
                         </configuration> 
                     </plugin> 
                 </plugins> 
             </build> 
        </profile>
    </profiles>

修改build

finalName能夠不定義,可是若是定義了,要和下面要修改的warName一致,不然打包部署會出現沒法找到文件錯誤

添加resources,將須要編譯的都放入對應的路徑下

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<finalName>custom-${profiles.activation}</finalName>
     <sourceDirectory>${basedir}/src</sourceDirectory>
     <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
     <resources>
       <resource>
         <directory>${basedir}/src</directory>
         <excludes>
           <exclude>** /*.java</exclude>
         </excludes>
       </resource>
       <resource>
         <directory>${basedir}/config</directory>
         <targetPath>${basedir}/WebRoot/WEB-INF/classes</targetPath>
         <includes>
           <include>*</include>
           <include>*/ *</ include >
         </includes>
         <excludes>
           <exclude>web.xml</exclude>
         </excludes>
       </resource>
     </resources>

 

 

 

而後修改pom.xml的build 的plugins裏面的插件maven-war-plugin,修改成以下

warname要和上面的finalname一致。

下面指定web.xml路徑,打包時會根據佔位符自動修改裏面的內容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <configuration>
           <warName>custom-${profiles.activation}</warName>
           <webResources>
               <resource>
                   <filtering> true </filtering>
                   <directory>${basedir}/src</directory>
 
                     <targetPath>WEB-INF/classes</targetPath> 
               </resource>
           </webResources>
           <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
           <webXml>${basedir}/WebRoot/WEB-INF/web.xml</webXml>
         </configuration>
       </plugin>

注意一個參數<filtering>true</filtering>,必定要設置成true這樣纔會用對應environment目錄下的配置文件覆蓋原來的。

 

修改web.xml

 

1
2
3
4
<context-param> 
         <param-name>spring.profiles.active</param-name> 
         <param-value>${profiles.activation}</param-value> 
</context-param>

這樣,基本上就ok啦,打包的時候命令爲 clean package -P develop ,profiles選擇對應的好比develop,我這裏用的是醬紫的

 麥庫截圖20141313131119269.jpg 

醬紫,就能夠完成打包部署到不一樣環境啦,定義不一樣的run configurations便可,run的時候選擇對應的就ok了。

 

可是,這樣會出現一個很大的問題,咱們平時都是本機測試的。。。

這樣直接部署到tomcat去會出現報錯滴,須要作如下修改

首先,把web.xml複製到config(source folder),你也能夠放入其餘路徑下,這裏值得注意的是,webinfo下面的web.xml其餘配置修改時你要注意同步修改到這個web.xml下去。。不然。。你白改了

而後恢復web.xml裏面的佔位符爲你要的開發環境,如develop。。

麥庫截圖20141313131549032.jpg 

麥庫截圖20141313131558442.jpg 

而後修改plugin爲如下代碼,這樣,打包部署時,會自動將config下的web.xml替換到web-inf下去~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <configuration>
           <warName>custom-${profiles.activation}</warName>
           <!--<webappDirectory>${basedir}/WebRoot</webappDirectory> -->
           <webResources>
               <resource>
                   <filtering> true </filtering>
                   <directory>${basedir}/config</directory>
                   <targetPath>WEB-INF</targetPath>
                   <includes>
                       < include >web.xml</ include >
                   </includes>
               </resource>
           </webResources>
           <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
           <webXml>${basedir}/WebRoot/WEB-INF/web.xml</webXml>
         </configuration>
       </plugin>

而後,就ok了。。。。。

相關文章
相關標籤/搜索