您的位置:首页 > 编程语言 > Java开发

Maven项目之Spring.profile.开发.测试.生产环境的配置和切换之一键部署

2017-12-22 16:34 1026 查看
背景:

软件开发过程一般涉及“开发 -> 测试 -> 部署上线”多个阶段,每个阶段的环境的配置参数会有不同,如数据源,文件路径等。为避免每次切换环境时都要进行参数配置等繁琐的操作,可以通过spring的profile功能来进行配置参数的切换。

部署阶段最原始的方式是连接服务器,停掉tomcat,备份之前的war,替换war,启动tomcat,这个过程不繁复,但是一天来两次也是够烦的了。


环境配置:

jdk8,Maven3.3.9,IntelliJ IDEA,tomcat7。


1:springprofile

省略:具体步骤和http://blog.csdn.net/fengyong7723131/article/details/78543822中的第一步一样


2:Maven配置

1:maven本身支持profile,在pom文件里配置profile

<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profiles.activation>dev</profiles.activation>
</properties>
</profile>
<profile>
<id>tes</id>
<properties>
<profiles.activation>tes</profiles.activation>
<tomcat.deploy.serverUrl>http://47.92.137.130:8081/manager/text</tomcat.deploy.serverUrl>
<tomcat.deploy.path>/</tomcat.deploy.path>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<profiles.activation>pro</profiles.activation>
<tomcat.deploy.serverUrl>http://47.92.158.134:8081/manager/text</tomcat.deploy.serverUrl>
<tomcat.deploy.path>/</tomcat.deploy.path>
</properties>
</profile>
</profiles>

2:maven的profile部署,用到了tomcat的manager功能,这里需要修改tomcat的tomcat-users.xml配置,修改如下
<role rolename="manager-script"/>

<user username="username" password="password" roles="manager-script"/>

3:添加maven的tomcat插件和打war包插件

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.version}</version>
<configuration>
<server>Tomcat</server>
<url>${tomcat.deploy.serverUrl}</url>
<username>username</username>
<password>password</password>
<update>true</update>
<path>${tomcat.deploy.path}</path>
<warFile>target/ce.war</warFile>
<uriEncoding>${project.build.sourceEncoding}</uriEncoding>
</configuration>
</plugin>
<!-- war 打包插件, 设定war包名称不带版本号 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<packagingExcludes>
</packagingExcludes>
<warSourceExcludes>
</warSourceExcludes>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
<warName>ce</warName>
</configuration>
</plugin>


4:web.xml文件

<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${profiles.activation}</param-value>
</context-param>

5:使用命令将项目部署到远程服务器,前提是远程的tomcat正常运行
mvn clean install tomcat7:redeploy -Ptes -Dmaven.test.skip=true

mvn clean install tomcat7:redeploy -Ppro -Dmaven.test.skip=true

-P后面的参数是pom文件里profile里的id,war插件需要多说的是,可以将-P参数映射到web.xml里的${profiles.activation}
每次部署完记得将本地的编译文件清理一次,不然直接启动的话,连接的是你上一次部署的环境
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐