这是一个草鸡鸡冻人心的时刻,搞了2天终于搞定了,麻麻不再用担忧我部署出错了!!!!!!!java
全部profile,spring和maven的,定义均要一致,不然,本身运行看看。web
首先,先来说下spring的profile功能,这个是方便项目的各类环境分离(开发、测试、生产),简单介绍下如何使用。spring
在beans中定义环境代码,项目中,我在beans.xml里定义数据库
1
|
<beans profile=
"develop,test,product"
></beans>
|
在数据库配置文件中,加入三种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>
|
而后就是修改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,我这里用的是酱紫的
酱紫,就能够完成打包部署到不一样环境啦,定义不一样的run configurations便可,run的时候选择对应的就ok了。
可是,这样会出现一个很大的问题,咱们平时都是本机测试的。。。
这样直接部署到tomcat去会出现报错滴,须要作如下修改
首先,把web.xml复制到config(source folder),你也能够放入其余路径下,这里值得注意的是,webinfo下面的web.xml其余配置修改时你要注意同步修改到这个web.xml下去。。不然。。你白改了
而后恢复web.xml里面的占位符为你要的开发环境,如develop。。
而后修改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了。。。。。