Maven利用jetty插件发布Web

来源:互联网 发布:淘宝第三方运营商 编辑:程序博客网 时间:2024/06/04 18:08

转自:http://www.blogjava.net/fancydeepin/archive/2012/06/23/maven-jetty-plugin.html

本机环境

Java:1.8Maven:3.3.9Jetty:9.2Eclipse:Mars

pom.xml

<build>  <plugins>    <plugin>      <groupId>org.eclipse.jetty</groupId>      <artifactId>jetty-maven-plugin</artifactId>      <version>9.2.8.v20150217</version>    </plugin>  </plugins></build>

启动 & 停止

启动:mvn jetty:run
停止:Ctrl + C

jetty 9 部署的项目的 Context path 默认是 /,
也就是说,项目的访问入口地址是:
http://localhost:8080(不带项目名)

若想使用mvn jetty:stop 停止服务器,
则需在pom.xml中添加:

<!-- plugin标签下 --><configuration>  [...]  <stopKey>shutdown</stopKey>  <stopPort>9966</stopPort>  [...]</configuration>

静态文件缓存

jetty 默认开启了 useFileMappedBuffer,在 jetty 运行期间,
页面所使用的静态文件(如 css 文件等)不允许修改。
如果你尝试去修改它们,保存的时候就会出现 Save could not be completed.

静态文件缓存

解决方法

找到 %repo%/org/eclipse/jetty/jetty-webapp/9.2.8.v20150217/jetty-webapp-9.2.8.v20150217.jar
(%repo% 表示你本地的 maven 仓库的目录,另外,将 9.2.8.v20150217 换成你所使用的版本)。
用压缩工具打开它, 找到 jetty-webapp-9.2.8.v20150217.jar/org/eclipse/jetty/webapp/webdefault.xml,
将 webdefault.xml 文件解压缩一份出来,用文本编辑器打开它,
搜索找到useFileMappedBuffer 配置的行,将 true 改成 false 以禁掉缓存。

<init-param>  <param-name>useFileMappedBuffer</param-name>  <param-value>false</param-value></init-param>

先确认 jetty 服务已经停止,将原文件
jetty-webapp-9.2.8.v20150217.jar/org/eclipse/jetty/webapp/webdefault.xml
删除,将刚才那份修改好的 webdefault.xml 文件重新压缩进去即可。

端口配置

jetty 默认使用的端口是 8080,命令行的方式修改端口的命令是:
mvn -Djetty.port=8081 jetty:run 。pom.xml 配置方式如下:

<!-- plugin标签下 --><configuration>  [...]  <httpConnector>    <port>8081</port>  </httpConnector>  [...]</configuration>

自动热部署

在你的 pom.xml 中添加如下配置:

<!-- plugin标签下 --><configuration>  [...]  <scanIntervalSeconds>2</scanIntervalSeconds>  [...]</configuration>

默认值是 0。大于 0 的数值表示开启,0 表示关闭,单位为秒。
以配置数值为一个周期,自动的扫描文件检查其内容是否有变化,
如果发现文件的内容被改变,则自动重新部署运用。
命令行的方式:mvn -Djetty.scanIntervalSeconds=2 jetty:run

手动重新加载

在你的 pom.xml 文件中添加如下配置,reload 的可选值 :
[automatic|manual]

<!-- plugin标签下 --><configuration>  [...]  <reload>manual</reload>  [...]</configuration>

默认值为 automatic,它与大于 0 的 scanIntervalSeconds 节点一起作用,
实现自动热部署的工作。设为 manual 的好处是,
当你改变文件内容并保存时,不会马上触发自动扫描和重部署的动作,
你还可以继续的修改,直至你在 Console 或命令行中敲
回车键(Enter)的时候才触发重新加载的动作。这样可以更加的方便调试修改。
命令行的方式是:mvn -Djetty.reload=manual jetty:run

访问日志

在你的 pom.xml 文件添加如下配置:

<configuration>  [...]  <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">    <filename>target/access-yyyy_mm_dd.log</filename>    <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>    <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>    <logTimeZone>GMT+8:00</logTimeZone>    <append>true</append>    <logServer>true</logServer>    <retainDays>120</retainDays>    <logCookies>true</logCookies>  </requestLog>  [...]</configuration>

org.eclipse.jetty.server.NCSARequestLog 是
org.eclipse.jetty.server.RequestLog 的一个实现类。
org.eclipse.jetty.server.NCSARequestLog 是一种伪标准的 NCSA 日志格式。
下面是一些节点参数的解释:
filename:日志文件的名称
filenameDateFormat:日志文件的名称的日期格式,它要求日志文件名必须含有 yyyy_mm_dd 串
logDateFormat:日志内容的时间格式
logTimeZone:时区
append:追加到日志
logServer:记录访问的主机名
retainDays:日志文件保存的天数, 超过删除
logCookies:记录 cookies
启动 jetty 服务,在项目的 target 目录下会生成一个 access-2015_06_23.log 文件,
该文件中的其中一条记录如下:

localhost 0:0:0:0:0:0:0:1 - - [2015-06-23 01:17:05] "GET /css/main.css HTTP/1.1" 304 - "http://localhost:8081/"  "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0" "JSESSIONID=2gyikovul2iz168116l2afo4f"

转储快照

在你的 pom.xml 文件添加如下配置:

<configuration>  [...]  <dumpOnStart>true</dumpOnStart>  [...]</configuration>

dumpOnStart 默认值为 false,如果设为 true,jetty 在启动时会把当前服务进程的内存信息输出到控制台中,但这并不会保存到文件中。

WEB上下文

最常用的是 contextPath,它的配置如下:

<configuration>  [...]  <webApp>    <contextPath>/${project.artifactId}</contextPath>  </webApp>  [...]</configuration>

contextPath 的默认值的 /,${project.artifactId} 引用了 节点的值,即项目的名称。
项目的静态资源文件目录默认是 src/main/webapp,如果静态资源目录有多个,或者不在默认的 src/main/webapp 目录下,可做如下配置:

<configuration>  [...]  <webApp>    <contextPath>/${project.artifactId}</contextPath>    <resourceBases>      <resourceBase>${project.basedir}/src/main/webapp</resourceBase>      <resourceBase>${project.basedir}/commons</resourceBase>    </resourceBases>  </webApp>  [...]</configuration>

引用静态资源文件时,路径不包含资源目录的名称,如 commons/main.css,引用方式为:<link href="main.css" rel="stylesheet" />
更多参数信息可参考 jetty-maven-plugin.html#configuring-your-webapp

完整的配置

附 pom.xml 文件中 jetty 插件的完整配置片段:

<build>  [...]  <plugins>    <plugin>      <groupId>org.eclipse.jetty</groupId>      <artifactId>jetty-maven-plugin</artifactId>      <version>9.2.8.v20150217</version>      <configuration>        <httpConnector>          <port>8081</port>        </httpConnector>        <stopKey>shutdown</stopKey>        <stopPort>9966</stopPort>        <!--        <scanIntervalSeconds>2</scanIntervalSeconds>        -->        <reload>manual</reload>        <dumpOnStart>true</dumpOnStart>        <webApp>          <contextPath>/${project.artifactId}</contextPath>          <!--          <resourceBases>            <resourceBase>${project.basedir}/src/main/webapp</resourceBase>            <resourceBase>${project.basedir}/commons</resourceBase>          </resourceBases>          -->        </webApp>        <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">          <filename>target/access-yyyy_mm_dd.log</filename>          <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>          <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>          <logTimeZone>GMT+8:00</logTimeZone>          <append>true</append>          <logServer>true</logServer>          <retainDays>120</retainDays>          <logCookies>true</logCookies>        </requestLog>      </configuration>    </plugin>  </plugins>  [...]</build>

更多有关 jetty 的配置信息可参考
http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html

0 0
原创粉丝点击