七夕之夜,送别佳人,独自骑着小黄龟速在车水马龙的大街上,嘴里莫名的哼着老萧的“回忆过去,痛苦的相思忘不了,为何你还来拨动我心跳......”,虽是一首悲情歌,但是我却感觉到甜蜜的味道,前方路途艰辛,还需要不断的武装自己,파이팅!!!
这位同学,收!!!想什么呢,今天的工作完成了么,今天的笔记做了么!!!瞎比比什么,
get out
!!!
好吧,收心,今天我们开始来讲讲WEB
项目自动部署的故事,这章主要介绍两个自动部署的插件,方便我们日常工作的开发
WEB项目自动部署
关于测试的话
对于项目测试而言,通常WEB
页面上的测试应该仅限于页面层次(比如jsp
,css
,javascript
的修改),其他代码修改(比如数据访问),应该通过编写单元测试的方式进行测试,(说到这我今天一口气完成的接口还没写测试用例呢,明天又得累狗了......)
jetty-maven-plugin自动部署
使用jetty
可以不借助其他web
服务器对项目进行部署,因为在jetty
内部内置了jetty web
容器,jetty-maven-plugin
默认就很好的支持了maven
项目的目录结构
下面是配置方式:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.6.v20170531</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<contextPath>/account-web</contextPath>
</webAppConfig>
</configuration>
</plugin>
运行jetty
容器需要执行
mvn clean install jetty:run
即可将项目部署到jetty
容器中,jetty
默认监听的是本地8080
端口,可以通过下面的命令修改监听端口
mvn clean install jetty:run -Djetty.port=8081
完成部署后就可以在浏览器通过http://localhost:8081/account-web/
访问项目地址了
在前面插件章节也提到过,要在命令行简化的执行maven
插件命令,需要插件groupId
是官方提供的org.apache.maven.plugins
或者org.codehaus.mojo
两者之一,其他插件groupId
需要在settings.xml
中的<pluginGroups>
中添加才可以调用简化命令
C:\Users\Administrator\.m2\settings.xml
配置文件中的配置如下:
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
<!--jetty-maven-plugin-->
<pluginGroup>org.eclipse.jetty</pluginGroup>
<!--cargo-->
<pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>
cargo-maven2-plugin自动部署
cargo
是一组帮助用户操作web
容器的工具,它能够帮用户实现自动化部署,而且它支持几乎所有的容器
cargo
与jetty
看起来相似,但目标不同,jetty
主要是便于日常的开发与测试,而cargo
主要是用于项目自动化部署,cargo
有三种部署方式:
standalone方式
这种方式将会把服务器配置拷贝到用户指定的目录(也就是copy
一个本地服务器)并在其中部署运行项目,下面是standalone
的配置方式
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<home>G:\workspace\apache-tomcat-7.0.56</home>
</container>
<configuration>
<type>standalone</type>
<home>${project.build.directory}/tomcat7x</home>
<properties>
<cargo.servlet.port>8888</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
通过运行mvn clean install cargo:run
部署并启动容器,这时会在项目构建目录target/
目录下产生一个tomcat7x
的服务器,并在其中部署项目war
最后启动,如此可以通过http://localhost:8888/account-web/login
访问项目了,默认情况下,cargo
监听本地8080
端口
这里需要注意的一点是,不要使用cargo:start
命令启动容器,start
命令在maven
构建完成后会自动关闭,run
命令则会一直监听,这样服务器就可以被访问,通过ctrl+c
停止服务器
existing模式部署项目
existing
模式是指cargo
直接通过用户配置的本地web
服务器部署和启动而不会再拷贝一份本地的web
服务器,下面是existing
的配置方式
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<home>G:\workspace\apache-tomcat-7.0.56</home>
</container>
<configuration>
<type>existing</type>
<home>G:\workspace\apache-tomcat-7.0.56</home>
</configuration>
</configuration>
</plugin>
部署至远程web容器
前两种方式都是直接管理本地web
容器,而这种方式允许用户部署到远程已经启动的web
服务器中,下面以tomcat
容器为例,前提是需要拥有tomcat
管理权限,通过tomcat管理页面的热部署方式记性项目的部署
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.remote.username>tomcat</cargo.remote.username>
<cargo.remote.password>123456</cargo.remote.password>
<cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>
</properties>
</configuration>
</configuration>
</plugin>
其中cargo.remote.username/password,cargo.tomcat.manager.url
配置了tomcat
服务器在线管理用户与页面,这也就意味着远程tomcat
服务是启动着的
%TOMCAT_HOME%/conf/tomcat-users.xml
中需要添加上面的用户tomcat/123456
并给定权限:
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="123456" roles="manager,manager-script,manager-gui, admin-gui"/>
我们可以在日常开发中使用这两个插件快速的部署我们的项目,通过几条简短的maven命令就可以让我们庞大的项目运行起来,有没有很神奇,有没有很方便,刺激么?惊喜么?那还在等什么,赶紧飞舞你的手指实操起来吧!!!