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

[使用心得]maven2之m2eclipse使用手册之六使用Maven2插件创建一个简单的SSH2项目之jetty篇(三)

2010-06-22 22:45 1101 查看
终于到了使用jetty插件的时候,使用jetty非常之简单,对于使用maven-jetty-plugin来说,它有几个命令,这里只是简单介绍了jetty:start与jetty:stop,其使用方法如下:

mvn jetty:run :这个命令故名意思就是启动jetty服务器,以8080的服务端口进行启动,如果你需要关闭其服务器请运行mvn jetty:stop命令,

对于其pom.xml配置如下:

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.24</version>
</plugin>

如果需要使用自动执行的话,请参考官方的pom.xml配置,如下:

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>


定义<daemon>true</daemon>标签防止永不停止的运行,其运行状态只在maven启动的时候才会运行,这里只是针对maven2的cmd版本,对于m2eclipse来说是不同的

而<scanIntervalSeconds>0</scanIntervalSeconds>简单来说让当前应用是否使用热插拔功能,默认0是不扫描项目,输入秒数则在多少秒中进行扫描web项目中那些变化了

如果想定义一组插件组的时候可以在settings.xml中定义以下内容:

<profile>
...
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
</profile>


注意:使用这个配置运行的时候,stopPort标签必须是可以在本机自由配置的,如果不这样的话很容易会得到一个已经使用过的maven plugin地址,结果当你在执行maven命令的时候将会出现org.mortbay.jetty.plugin.util.Monitor是会引起堆栈异常,

其英文原话:

When running with this configuration, the "stopPort" must be free on the machine you are running on. If this is not the case, you will be getting an "address already in use" from the maven plugin, which appears *after* the "Started SelectedChannelConnector ..." message. If you run with the mvn -X option, you'll see org.mortbay.jetty.plugin.util.Monitor is part of the causing stacktrace.

mvn jetty:stop:停止当前jetty服务器的命令,对于它,也有它的配置项,pom.xml文件如下:


<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
</configuration>
</plugin>


一般来说运行使用了jetty:run,jetty:run-war,jetty:run-exploded 这些命令可以通过窗口的按钮停止服务器启动,如果需要mvn jetty:stop命令的话则需要配置特殊的端口去进行停止,

英文原话:

The run, run-war and run-exploded goals leave the plugin running indefinitely. You can terminate it with a <cntrl-c> in the controlling terminal window, or by executing the stop goal in another terminal window. If you wish to be able to use mvn jetty:stop then you need to configure the plugin with a special port number and key that you also supply on the stop command.
对于maven-jetty-plugin,还有其他命令分别是:

jetty:run:在webapp没进行部署的时候可以通过一些配置参数进行运行,
jetty:run-war:通过<configuration>标签指定webapp位置进行运行该以war形式的wepapp
jetty:run-exploded:讲目标项目组装成一个文件,然后检查<scanInterval>标签是否为0,如果不为0,jetty会检查你的pom.xml文件,必要的时候将会重新部署web.xml
jetty:deploy-war:使用maven中的 package命令打成war包,然后部署到jetty中
更加详细的详细,可以参考一下网址:

Maven Jetty Plugin Configuration Guide

介绍了那么多了,都是基于maven下的,到了m2eclipse我们怎么运行其命令呢?
答案是很简单的,在maven project中run as中选择build.. 选项(如图1:选择),出现一个命令框,在Goals输入jetty:start(如图2:输入命令),单击run,然后查看Console中的内容(实际中每个系统都不同的信息),你将会看到maven已经进行了build和部署了,默认使用了jetty:run的命令(如图:3运行信息),最终运行成功后会提示[INFO] Started Jetty Server这个信息(如图4:正常运行),红字那些则是配置框架时候那些信息.运行http://localhost:8080/SSH2Maven/index.jsp(如图5:页面信息),会出现正常的页面了,而后台也会出现查询数据的sql语句(如图6:后台打印信息).

67623FB7.png?download&psid=1]

图1:选择

5CDD5798.png?download&psid=1]

图2:输入命令

6C4C2F9B.png?download&psid=1]

图:3运行信息


.png?download&psid=1]

图4:正常运行

1C4E591F.png?download&psid=1]

图5:页面信息

.png?download&psid=1]

图6:后台打印信息

注意:使用strut2中的<s:debug/>标签的时候如果你删除了后再刷新,再添加<s:debug/>标签的话会出现异常,这个异常估计是跟jetty的热部署有关系,所以如果使用了<s:debug/>这个标签的话千万就不要随意删除了,否则很容易引起不必要的错误.

补充:默认jetty默认是jsp2.1,如果需要使用jsp2.0的话则可以使用以下配置

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.14</version>
<dependencies>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-api-2.0</artifactId>
<version>6.1.14</version>

</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>jasper-compiler-jdt</artifactId>
<version>5.5.15</version>
</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>jasper-compiler</artifactId>
<version>5.5.15</version>
</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>jasper-runtime</artifactId>
<version>5.5.15</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1</artifactId>
<version>6.1.14</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-api-2.1</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>start</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
....
</plugin>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐