码迷,mamicode.com
首页 > 编程语言 > 详细

Maven 结合 Spring profile对不同的部署环境打包部署

时间:2014-06-15 20:55:44      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   code   java   http   

这是一个草鸡鸡冻人心的时刻,搞了2天终于搞定了,麻麻再也不用担心我部署出错了!!!!!!!

所有profile,spring和maven的,定义均要一致,否则,自己运行看看。

首先,先来讲下spring的profile功能,这个是方便项目的各种环境分离(开发、测试、生产),简单介绍下如何使用。

  1. 在beans中定义环境代码,项目中,我在beans.xml里定义

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

           

           

       

  2. 在数据库配置文件中,加入三种profile的各自定义连接,我这里的配置是把test,develop配置设为一样,当然也可以区别开来,看实际环境,这里每个profile里面的配置都要对称,否则会出错哦~

    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啦,添加以下代码即可

           

    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的具体步骤

 

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

id:spring中配置的profile名称

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,我这里用的是酱紫的

 bubuko.com,布布扣 

酱紫,就可以完成打包部署到不同环境啦,定义不同的run configurations即可,run的时候选择对应的就ok了。

 

但是,这样会出现一个很大的问题,我们平时都是本机测试的。。。

这样直接部署到tomcat去会出现报错滴,需要做以下修改

首先,把web.xml复制到config(source folder),你也可以放入其他路径下,这里值得注意的是,webinfo下面的web.xml其他配置修改时你要注意同步修改到这个web.xml下去。。否则。。你白改了

然后恢复web.xml里面的占位符为你要的开发环境,如develop。。

bubuko.com,布布扣 

bubuko.com,布布扣 

然后修改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了。。。。。

Maven 结合 Spring profile对不同的部署环境打包部署,布布扣,bubuko.com

Maven 结合 Spring profile对不同的部署环境打包部署

标签:des   style   class   code   java   http   

原文地址:http://www.cnblogs.com/intime-dev/p/3785645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!