这一章节将说明如何经过Maven管理Jetty和使用Jetty的Maven插件。html
Apache Maven是一个款软件项目管理工具。基于项目对象模型(POM)的概念,Maven能够管理一个项目的构建生成报告和文档。这是一个理想的工具用来构建一个web应用程序,这样的项目也可使用jetty-maven-plugin来轻松运行并能够节省开发时间,你也可使用Maven来构建、测试、运行一个嵌入式Jetty项目。java
首先咱们要看一个很是简单的Jetty嵌入式的HelloWorld程序,而后再看如何用jetty-maven-plugin来加速开发一个web应用。web
为了理解构建和运行Jetty的基本操做,首先请回顾如下内容(点击我):算法
Maven多使用约定而不是配置,因此最好的作法是使用Maven推荐的命令来构建项目。你可使用archetypes 模板来快速生成一个Maven项目,可是对于这个简单的入门例子,咱们将手动进行配置:apache
> mkdir JettyMavenHelloWorld > cd JettyMavenHelloWorld > mkdir -p src/main/java/org/example
可使用文本编辑器建立文件src/main/java/org/example/HelloWorld.java,有以下内容:api
package org.example; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.IOException; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; public class HelloWorld extends AbstractHandler { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println("<h1>Hello World</h1>"); } public static void main(String[] args) throws Exception { Server server = new Server(8080); server.setHandler(new HelloWorld()); server.start(); server.join(); } }
pom.xml用来描述项目名称和依赖关系。使用文本编辑器建立pom.xml,包含以下内容:数组
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>hello-world</artifactId> <version>0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Jetty HelloWorld</name> <properties> <!-- 具体版本信息可在此查看 http://central.maven.org/maven2/org/eclipse/jetty/jetty-maven-plugin/ --> <jettyVersion>9.0.2.v20130417</jettyVersion> </properties> <dependencies> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jettyVersion}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1</version> <executions> <execution><goals><goal>java</goal></goals></execution> </executions> <configuration> <mainClass>org.example.HelloWorld</mainClass> </configuration> </plugin> </plugins> </build> </project>
你可使用以下命令来编译并执行HelloWorld:浏览器
> mvn clean compile exec:java
你能够在你的浏览器中输入http://localhost:8080来查看欢迎页。你能够观察到使用mvn dependency:tree命令后Maven到底作了什么,主要是解决依赖和下载文件:
安全
> mvn dependency:tree [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'dependency'. [INFO] ------------------------------------------------------------------------ [INFO] Building Jetty HelloWorld [INFO] task-segment: [dependency:tree] [INFO] ------------------------------------------------------------------------ [INFO] [dependency:tree {execution: default-cli}] [INFO] org.example:hello-world:jar:0.1-SNAPSHOT [INFO] \- org.eclipse.jetty:jetty-server:jar:9.0.0:compile [INFO] +- org.eclipse.jetty:javax.servlet:jar:3.0.0.v201112011016:compile [INFO] +- org.eclipse.jetty:jetty-continuation:jar:9.0.0:compile [INFO] \- org.eclipse.jetty:jetty-http:jar:9.0.0:compile [INFO] \- org.eclipse.jetty:jetty-io:jar:9.0.0:compile [INFO] \- org.eclipse.jetty:jetty-util:jar:9.0.0:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4 seconds [INFO] Finished at: Thu Jan 24 16:19:08 EST 2013 [INFO] Final Memory: 11M/68M [INFO] ------------------------------------------------------------------------
上面一节演示了如何经过Maven来建立嵌入式Jetty应用。如今咱们要研究如何经过Maven和Jetty来开发一个标准的web应用。首先建立Maven项目结构(若是你喜欢你也可使用maven的webapp模板来构建)。session
> mkdir JettyMavenHelloWarApp > cd JettyMavenHelloWebApp > mkdir -p src/main/java/org/example > mkdir -p src/main/webapp/WEB-INF
使用编辑器建立一个src/main/java/org/example/HelloServlet.java文件,内容以下:
package org.example; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Hello Servlet</h1>"); response.getWriter().println("session=" + request.getSession(true).getId()); } }
你须要将这个servlet声明在部署描述中,因此建立src/main/webapp/WEB-INF/web.xml文件,并增长以下内容:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" metadata-complete="false" version="3.1"> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>org.example.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping> </web-app>
pom.xml用来描述项目名称和依赖关系。使用文本编辑器建立pom.xml,包含以下内容,特别注意 jetty-maven-plugin的声明:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>hello-world</artifactId> <version>0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Jetty HelloWorld WebApp</name> <properties> <jettyVersion>9.3.11.v20160721</jettyVersion> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jettyVersion}</version> </plugin> </plugins> </build> </project>
如今你能够同时构建和运行一个web应用,不须要把项目打成war包,而是使用jetty-maven-plugin插件命令:
> mvn jetty:run
你能够在http://localhost:8080/hello看到静态和动态的内容。
你可使用以下命令来说一个web项目打包成war包:
> mvn package
打包后的war包能够部署到标准的servlet容器里,包括Jetty。
Jetty Maven插件很是适合快速开发和测试。根据一般的Maven约定你能够把它添加到任何web项目中。这个插件能够周期性的扫描你的项目,如有改变会自动部署。这样经过消除构建的时间,可让开发更有效率,部署的步骤是:你使用你的开发工具对项目有所改变,那么运行中的web容器会自动更换它们,容许你能够当即测试代码。
重要提示:
为了使用这个插件应该使用Maven 3.3+ 和 Java1.8版本。
虽然Jetty Maven的插件对开发很是有用,可是咱们依然不建议用户在生产环境上使用。为了让插件能正常工做,时用了不少maven内部的API,可是maven自己并非一个生产部署工具。因此咱们建议使用传统方式部署应用或者使用嵌入式Jetty。
首先新增jetty-maven-plugin 到你的 pom.xml中:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> </plugin>
而后,在pom.xml的目录下执行以下命令:
mvn jetty:run
能够经过访问http://localhost:8080/来查看启动的Jetty服务。
Jetty会持续运行知道你中止它,当它运行时,它会按期扫描你项目下的文件,因此若是你对保存修改的文件并从新编译成class文件,那么Jetty会从新部署你的应用,你就能够当即对你刚才作的改变进行测试。
你能够在控制台使用Ctrl+C,来强行终止Jetty运行。
注意:
运行中的Jetty程序的classpath和它的部署webapp都是由Maven进行管理的,因此有可能不像你想的那么精确。例如:web应用依赖的jar包有可能引用的是本地文件,而不是WEB-INF/lib目录下。
Jetty Maven插件有不少不一样于Maven的功能,能够说最有用的命令run
能够用在一个原始的web应用上。这还有另外一个方法一样能够帮助你实现目标,例如:你有可能须要将你的应用运行在Jetty下,而不是maven下;或者你想经过Jetty来部署应用。有不一样的方法来实现你想要的。
使用以下命令能够列出Jetty Maven 插件全部的功能
mvn jetty:help
若是想要查看具体的参数配置,能够添加以下参数:
mvn jetty:help -Ddetail=true -Dgoal= goal-name
下面这些Jetty环境属性的设置在你的web应用中执行,最经常使用的配置以下:
httpConnector
可选择的配置,若是没有设置,Jetty将建立ServerConnector实例来监听8080端口。你能够在命令行上使用系统属性jetty.http.port来
修改默认的端口配置,例如mvn -Djetty.http.port=9999 jetty:run,
固然你能够经过配置下面的属性来配置ServerConnector。能够配置的属性以下:
port:链接监听的端口,默认8080;
host:监听的主机,默认监听全部主机,即全部主机均可以访问;
name:链接器的名称,在配置指定链接器来处理指定请求时有用;
idleTimeout:链接超时时间;
soLinger:socket链接时间;
你一样能够在一个标准的Jetty xml配置文件中配置链接,并把配置文件的路径赋值给jettyXml参数。jetty-9.0之后已经不须要把链接信息配置在pom.xml中了;你可使用Jetty的xml进行配置。
jettyXml
可选择的配置,一般,能够把以逗号分割的Jetty xml配置文件的地址字符串增长到任何插件的配置参数中。若是你有另外一个web应用、处理器、特别是链接器,你就可使用它,可是若你有另一个Jetty对象,则不能经过插件获得配置信息。
scanIntervalSeconds
自动扫描文件改变并进行热部署的时间间隔,单位为秒。默认值为0,这表明着禁用扫描并热部署,只有一个大于0的配置可使它生效。
reload
从新加载选项,默认值是"automatic"(自动),通常用来和配置不为0的scanIntervalSeconds一同使用。默认配置下当发现有文件改变会自动进行热部署。若是设置为"manual" (手动),这样设置的话,部署将会经过插件被手动触发,这在当你频繁改动文件时比较有用,这样会忽略你的改动,直到你作完全部改变。
dumpOnStart
可选择的配置,默认为false,若是设置为true。那么Jetty会在启动时打印出server的结构。
loginServices
可选择的配置。是一系列org.eclipse.jetty.security.LoginService的实现类。注意,没有指定默认的域,若是你须要在web.xml中配置域,那么就能够配置一个统一的域。固然也能够在Jetty的xml里面进行配置。并把配置文件的地址增长到
jettyXml中。
requestLog
可选择的配置。一个实现了org.eclipse.jetty.server.RequestLog接口的请求日志记录。有三种方式配置请求日志:
server
jetty-9.3.1之后是可选择配置。这能够配置org.eclipse.jetty.server.Server 实例用来支持插件的使用,然而一般是不须要配置的,由于插件会自动为你配置。特别是你在使用jettyXml的时候你一般不肯意使用这个元素。若是你同时定义了server元素和在xml文件中进行了包含“<Configure id="Server" class="org.eclipse.jetty.server.Server">”的配置,那么xml文件的配置将会覆盖pom中的配置。
stopPort
可选择配置。一个用来监听中止命令的端口。
stopKey
可选择的配置。和stopPort结合使用。
systemProperties
可选择的配置。容许你为了执行插件而配置系统参数。
systemPropertiesFile
可选择的配置。一个包含执行插件系统参数的文件。默认状况你在文件中设置的参数不会覆盖在命令行中写的参数,无论经过JVM仍是经过POM的systemProperties。
skip
默认为false。若是为true的话,插件的执行会退出。一样可使用命令-Djetty.skip
进行设置。这在一体化测试你能够经过配置取消执行时很是有用。
useProvidedScope
默认为false。若是为true的话, <scope>provided</scope所依赖的位置将会被添加到容器的classpath中。注意这并非webapp的classpath,只是提供给容器使用的。因此你应该会少使用这一功能,而是明确拷贝须要的依赖。
excludedGoals
可选择的配置。一系列Jetty插件的名称可使插件打印出有效的信息并退出。这在你想禁止用户使用指定功能的时候颇有用。
为了配置https链接器,你须要使用Jetty xml配置文件。下面的例子是直接拷贝 etc/文件夹下的文件,固然你也能够本身编写你本身xml文件。咱们将使用下面的文件:
jetty.xml
设置 org.eclipse.jetty.server.Server 实例的各类属性,为了让插件可使用。重点提示,下面设置了org.eclipse.jetty.server.HttpConfiguration元素,咱们也可使用一个字xml文件来配置它。这是一个相关的部分:
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration"> <Set name="secureScheme">https</Set> <Set name="securePort"><Property name="jetty.secure.port" default="8443" /></Set> <Set name="outputBufferSize">32768</Set> <Set name="requestHeaderSize">8192</Set> <Set name="responseHeaderSize">8192</Set> <Set name="sendServerVersion">true</Set> <Set name="sendDateHeader">false</Set> <Set name="headerCacheSize">512</Set> </New>
jetty-ssl.xml
为https链接配置ssl。下面一个jetty-ssl.xml
例子来自jetty-distribution:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"> <!-- ============================================================= --> <!-- SSL基础配置 --> <!-- 这个配置文件须要和至少一个或多个 --> <!-- etty-https.xml 或 jetty-http2.xml文件同时使用 --> <!-- ============================================================= --> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- =========================================================== --> <!-- 不使用协议工厂增长一个SSL链接 --> <!-- =========================================================== --> <Call name="addConnector"> <Arg> <New id="sslConnector" class="org.eclipse.jetty.server.ServerConnector"> <Arg name="server"><Ref refid="Server" /></Arg> <Arg name="acceptors" type="int"><Property name="jetty.ssl.acceptors" deprecated="ssl.acceptors" default="-1"/></Arg> <Arg name="selectors" type="int"><Property name="jetty.ssl.selectors" deprecated="ssl.selectors" default="-1"/></Arg> <Arg name="factories"> <Array type="org.eclipse.jetty.server.ConnectionFactory"> <!-- 注释掉用于支持代理 <Item> <New class="org.eclipse.jetty.server.ProxyConnectionFactory"/> </Item>--> </Array> </Arg> <Set name="host"><Property name="jetty.ssl.host" deprecated="jetty.host" /></Set> <Set name="port"><Property name="jetty.ssl.port" deprecated="ssl.port" default="8443" /></Set> <Set name="idleTimeout"><Property name="jetty.ssl.idleTimeout" deprecated="ssl.timeout" default="30000"/></Set> <Set name="soLingerTime"><Property name="jetty.ssl.soLingerTime" deprecated="ssl.soLingerTime" default="-1"/></Set> <Set name="acceptorPriorityDelta"><Property name="jetty.ssl.acceptorPriorityDelta" deprecated="ssl.acceptorPriorityDelta" default="0"/></Set> <Set name="acceptQueueSize"><Property name="jetty.ssl.acceptQueueSize" deprecated="ssl.acceptQueueSize" default="0"/></Set> </New> </Arg> </Call> <!-- =========================================================== --> <!-- 基于定义在jetty.xml配置文件里的HttpConfiguration --> <!-- 建立一个基于TLS的HttpConfiguration --> <!-- 增长一个SecureRequestCustomizer来管理证书和session信息 --> <!-- =========================================================== --> <New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration"> <Arg><Ref refid="httpConfig"/></Arg> <Call name="addCustomizer"> <Arg> <New class="org.eclipse.jetty.server.SecureRequestCustomizer"> <Arg name="sniHostCheck" type="boolean"><Property name="jetty.ssl.sniHostCheck" default="true"/></Arg> <Arg name="stsMaxAgeSeconds" type="int"><Property name="jetty.ssl.stsMaxAgeSeconds" default="-1"/></Arg> <Arg name="stsIncludeSubdomains" type="boolean"><Property name="jetty.ssl.stsIncludeSubdomains" default="false"/></Arg> </New> </Arg> </Call> </New> </Configure>
如今你须要让插件来应用上面的这个文件:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <jettyXml>jetty.xml,jetty-ssl.xml,jetty-https.xml</jettyXml> </configuration> </plugin>
!警告
对于Jetty的安装来讲,xml配置文件的顺序是很重要的。
你也可使用jetty xml文件来配置http链接供插件使用。在这咱们使用Jetty程序包中的jetty-http.xml文件:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"> <!-- ============================================================= --> <!-- 使用"Server"的ID来配置Server实例,并添加一个HTTP链接 --> <!-- 这个配置文件必须和jetty.xml文件结合使用 --> <!-- ============================================================= --> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- =========================================================== --> <!-- 增长一个HTTP链接 --> <!-- =========================================================== --> <Call name="addConnector"> <Arg> <New id="httpConnector" class="org.eclipse.jetty.server.ServerConnector"> <Arg name="server"><Ref refid="Server" /></Arg> <Arg name="acceptors" type="int"><Property name="jetty.http.acceptors" deprecated="http.acceptors" default="-1"/></Arg> <Arg name="selectors" type="int"><Property name="jetty.http.selectors" deprecated="http.selectors" default="-1"/></Arg> <Arg name="factories"> <Array type="org.eclipse.jetty.server.ConnectionFactory"> <Item> <New class="org.eclipse.jetty.server.HttpConnectionFactory"> <Arg name="config"><Ref refid="httpConfig" /></Arg> <Arg name="compliance"><Call class="org.eclipse.jetty.http.HttpCompliance" name="valueOf"><Arg><Property name="jetty.http.compliance" default="RFC7230"/></Arg></Call></Arg> </New> </Item> </Array> </Arg> <Set name="host"><Property name="jetty.http.host" deprecated="jetty.host" /></Set> <Set name="port"><Property name="jetty.http.port" deprecated="jetty.port" default="8080" /></Set> <Set name="idleTimeout"><Property name="jetty.http.idleTimeout" deprecated="http.timeout" default="30000"/></Set> <Set name="soLingerTime"><Property name="jetty.http.soLingerTime" deprecated="http.soLingerTime" default="-1"/></Set> <Set name="acceptorPriorityDelta"><Property name="jetty.http.acceptorPriorityDelta" deprecated="http.acceptorPriorityDelta" default="0"/></Set> <Set name="acceptQueueSize"><Property name="jetty.http.acceptQueueSize" deprecated="http.acceptQueueSize" default="0"/></Set> </New> </Arg> </Call> </Configure>
如今讲这个文件添加到文件列表中:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <jettyXml>jetty.xml,jetty-http.xml,jetty-ssl.xml,jetty-https.xml</jettyXml> </configuration> </plugin>
固然也可使用httpConnector元素来配置同上面的例子同样。
这些配置参数应用到你的webapp中。它们能够实现全部的要求。
webApp
这是一个继承了org.eclipse.jetty.webapp.WebAppContext的org.eclipse.jetty.maven.plugin.JettyWebAppContext实例。你可使用这个对象上全部的方法来配置你的webapp。下面几个是最经常使用的:
contextPath
你web应用的根路径。默认设置为“/”,若是你能够设置一个路径在“/”下面,例如/mycontext
descriptor
当前web应用的web.xml路径
defaultsDescriptor
webdefault.xml的路径,会在web.xml以前应用这个文件内容。若是你不指定一个,那么Jetty会使用一个在jetty-webapp.jar里面的webdefault.xml。
overrideDescriptor
当Jetty读取web.xml后要覆盖的配置文件。你能够用这个文件来覆盖或者增长配置。
tempDirectory
在web应用运行时,Jetty用来扩展或者拷贝jar文件和JSP编译后类的文件夹路径,默认路径是${project.build.outputDirectory}/tmp
baseResource
Jetty静态资源的根目录,默认为src/main/webapp
resourceBases
用来替代baseResource,若是你有多个静态目录。这是一个地址名字的数组。
baseAppFirst
默认为true。控制是否要在web应用的原始资源加载前或者加载后覆盖war包。
containerIncludeJarPattern
默认为"./javax.servlet-[^/]\.jar$|./servlet-api-[^/]\.jar$|.javax.servlet.jsp.jstl-[^/]\.jar|.taglibs-standard-impl-.\.jar"。这是一个匹配规则,用来匹配容器classpath(注意:是容器的classpath不是web应用的classpath)里jar包的名字中应该被扫描的fragments,tlds和annotations 。这和context中org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern 属性比较类似。用户也能够定义额外的须要被扫描的jar包。
contextXml
应用到web应用的context的xml的路径,在元素webApp
之后。
run 命令运行在一个web应用上,而且不须要将应用打成一个war包。相反,Jetty直接从源代码处进行部署。它将会在Maven默认的项目路径下寻找webapp的组成部分,你也能够经过插件配置来覆盖默认路径。例如,默认它将寻找以下内容:
${project.basedir}/src/main/webapp
下的资源${project.build.outputDirectory}下
的classes${project.basedir}/src/main/webapp/WEB-INF/
下的web.xml插件会在部署前确保classes被从新编译过并保证是最新的。若是你改变class的来源那么你的IDE工具将会自动在后台从新编译,插件也将得到修改后的class。
用户也不须要将webapp打成一个war包,在部署时能够节省时间。一旦被调用,用户能够配置插件来不断的运行并扫描项目的改变,而且在有须要的时候进行热部署。你一旦对项目作出了改变,那么将会当即在Jetty实例中展示出来,让你很快的进行代码的测试,这样比编码、编译、打包、测试这样的循环要好。
这里有一个简单的例子,用来每10秒钟进行一次扫描获取改变,而且设置web应用的根路径为“/”:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/test</contextPath> </webApp> </configuration> </plugin>
除了webApp这一个经常使用的元素外,jetty:run还支持如下:
classesDirectory
你webapp应用编译后classes文件的地址。你应该尽可能不要设置这个属性,而是应该在pom.xml设置build outputDirectory这个属性。
testClassesDirectory
你webapp应用测试代码编译后classes文件的地址。默认是${project.build.testOutputDirectory}
。
useTestScope
默认为false,若为true的话,testClassesDirectory 路径下的classes和“test”依赖的将会被放置在classpath下。
webAppSourceDirectory
默认的设置为${project.basedir}/src/main/webapp,若是逆境在资源的在不一样路径下,能够设置这个路径。
jettyEnvXml
可选择的,jetty-env.xml文件的路径,这个容许你对JNDI进行绑定,并将env-entry, resource-env-ref 和 resource-ref关联到web.xml的合适位置。这将只对当前web应用有效,不能共享到其它web应用,因此你须要在部署项目的同时部署此文件(例如,经过使用jettyConfig文件)。
scanTargets
可选择的,须要被插件自动扫描的文件或者文件夹的列表。
scanTargetPatterns
可选择的,若是你有大量的文件想要扫描,最方便的作法是使用规则来匹配文件这样要比把全部文件列出来要好。
scanClassesPattern
9.3.0之后可选择配置,在扫描的时候包含或者排除的规则。若是一个文件发生被修改了,但这个文件符合排除规则,那么将不会进行从新部署。
scanTestClassesPattern
同scanClassesPattern,用于测试类。
下面有一个例子:
<project> ... <plugins> ... <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <webAppSourceDirectory>${project.basedir}/src/staticfiles</webAppSourceDirectory> <webApp> <contextPath>/</contextPath> <descriptor>${project.basedir}/src/over/here/web.xml</descriptor> <jettyEnvXml>${project.basedir}/src/over/here/jetty-env.xml</jettyEnvXml> </webApp> <classesDirectory>${project.basedir}/somewhere/else</classesDirectory> <scanClassesPattern> <excludes> <exclude>**/Foo.class</exclude> </excludes> </scanClassesPattern> <scanTargets> <scanTarget>src/mydir</scanTarget> <scanTarget>src/myfile.txt</scanTarget> </scanTargets> <scanTargetPatterns> <scanTargetPattern> <directory>src/other-resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <excludes> <exclude>**/myspecial.xml</exclude> <exclude>**/myspecial.properties</exclude> </excludes> </scanTargetPattern> </scanTargetPatterns> </configuration> </plugin> </plugins> </project>
无论什么缘由,若是你不能在一个没有配置过的webapp上运行,那么使用可使用run-war 。
首先打包你的应用成一个war包,而后将它部署到Jetty。若是你把scanInterval设置为非0数值,那么Jetty将会监视pom.xml和war文件,若是文件被改变,那么Jetty将会从新部署这个war。
配置
war
war包的地址。默认为${project.build.directory}/${project.build.finalName}.war。若是这个不合适,那么将其设置到你经常使用的地点。
下面有配置的例子:
<project> ... <plugins> ... <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <war>${project.basedir}/target/mycustom.war</war> </configuration> </plugin> </plugins> </project
run-exploded命令的目的是首先将你的webapp与一个已经打开的war文件关联,而后把它部署到Jetty上。若是scanInterval参数设置了一个非0的数,那么Jetty将会监视你的pom.xml,WEB-INF/lib,WEB-INF/classes和WEB-INF/web.xml文件,若是检测到改变则从新部署。
配置
war
解压的war文件地址,默认的为${project.build.directory}/${project.build.finalName}
,可是你能够经过配置覆盖默认值。
下面有一个例子展现如何使用:
<project> ... <plugins> ... <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <war>${project.basedir}/target/myfunkywebapp</war> </configuration> </plugin> </plugins> </project>
这个命令和jetty:run-war基本同样,可是不会把当前模块打成war包,你能够指定任何路径下的war文件来运行。
配置
war
war文件的路径。默认为${project.build.directory}/${project.build.finalName},可是你能够经过设置参数来覆盖。
daemon
若是设置为true,那么插件会运行Jetty并让构建继续。这是很是有用的,若是你想启动jetty做为执行绑定在一个特定的阶段,而后阻止它在另外一个。固然,你也能够把这个参数设置为false,在这种状况下,Jetty将会堵塞运行,你须要使用Ctrl+C来终止运行它。
下面有一个配置文件:
<project> ... <plugins> ... <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <war>/opt/special/some-app.war</war> <stopKey>alpha</stopKey> <stopPort>9099</stopPort> </configuration> <executions> <execution> <id>start-jetty</id> <phase>test-compile</phase> <goals> <goal>deploy-war</goal> </goals> </execution> <execution> <id>stop-jetty</id> <phase>test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> </plugins> </project>
这个命令的做用是容许你将当前webapp运行在一个新的JVM上,能够传入参数到新的JVM上。命令支持同jetty:run同样的参数配置。
配置
可用的配置参数,比jetty:run多的以下:
jvmArgs
可选择配置。表示须要传入到JVM的任意参数的字符串。
waitForChild
默认为true。这个须要父进程等待全部进程结束后才能退出。在这种状况下你也可使用Ctrl+C来同时终止它们。把这个参数设置为false或许更有效,在这种状况下,父进程退出了,而子进程能够继续运行。你可使用 jetty:stop来终止子进程。
maxStarupLines
默认为50。子进程运行后,父进程能够读取子进程传入的最大行数。若是子进程产生大量的输出,那么你有可能须要把这个配置的数字增大。
这个命令不会支持容器的某些配置:
scanIntervalSeconds
不支持。不会监控并重部署应用。
reload
不支持。不会从新部署应用。
httpConnector
不支持。定义connectors使用jetty xml配置文件。
loginService
不支持。定义LoginServices使用jetty xml或者容器xml文件来替代。
requestLog
不支持。定义requestLog使用jetty xml或者容器xml文件来替代。
systemProperties
不支持。使用 jvmArgs 参数来说系统属性传入命令中。
部署你未打包的程序到新的JVM使用以下命令:
mvn jetty:run-forked
Jetty将持续运行直到你作了以下操做的一种:
waitForChild=true来启动时
)。这个命令的目的是执行时绑定你的pom.xml。它和jetty:run命令有些类似,然而它第一次运行不会编译除非输入“test-compile”命令来确保全部须要的classes和webapp下的全部文件都是新生成的。这个你想经过绑定的pom.xml控制启动和终止的状况下颇有用。
例如,你能够配置插件为在你测试前启动webapp并在你测试完成后中止webapp。为了这么作,你能够设置Jetty插件的一对execution 情景。你是用 pre-integration-test和post-integration-test的Maven插件来设置Jetty启动和终止的触发器。
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</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>start</goal> </goals> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin>
stop命令会终止一个运行中的Jetty实例。为了使用它,你须要为这个插件配置一个特别的端口和key。相同的端口和key将被另外一个运行的Jetty使用。
stopPort
一个Jetty的端口数字,又来监听接收一个终止的命令并将其终止。
stopKey
一个发送到stopPort端口的有效命令。
stopWait
等待Jetty中止确认的最大时间间隔,单位秒。若是为false或者为设置,那么插件将不会再发出命令后等待结果。
下面有一个配置的例子。
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <stopPort>9966</stopPort> <stopKey>foo</stopKey> <stopWait>10</stopWait> </configuration> </plugin>
而后当Jetty运行的时候,在另外一个窗口输入如下命令:
mvn jetty:stop
stopPort端口必须是有效的。若是不是这种状况,那么你将再输入命令后获得一个“端口已经被占用”的错误消息。
这个命令的做用是计算一个合成的web.xml(一个有效的web.xml),经过Servlet规范把应用组件中的全部描述文件(webdefault.xml、web.xml、web-fragment.xmls、web-override.xml),发现的注解(@WebServlet、 @WebFilter、 @WebListener)。经过有效的来源组成一个web.xml并输出日志。关于你应用的一些其余有用的信息,也会存储在effective-web.xml中。
下面的参数配置容许你保存文件:
deleteOnExit
默认为true。若是设置为false,那么原始的web.xml将会生成到输出路径下的一个名为effective-web.xml的文件中。
effectiveWebXml
实际web.xml文件生成的全路径名。
注意,没有被声明的servlets,filters和listeners将不会被使用。
若是你的应用须要依赖其余war文件,jetty:run和jetty:run-forked命令能够合并全部的资源文件。它能够基于maven-war-plugin的配置,若是你的项目不是用maven-war-plugin,那么它将经过简单的算法来得到全部资源文件。
maven-war-plugin插件有合并资源的丰富经验。 jetty:run和jetty:run-forked的命令有能力解释你运行中程序的大多数。能够经过如下两个例子来了解。
让你的webapp依赖如下war文件:
<dependency> <groupId>com.acme</groupId> <artifactId>X</artifactId> <type>war</type> </dependency> <dependency> <groupId>com.acme</groupId> <artifactId>Y</artifactId> <type>war</type> </dependency>
包含以下:
WebAppX: /foo.jsp /bar.jsp /WEB-INF/web.xml WebAppY: /bar.jsp /baz.jsp /WEB-INF/web.xml /WEB-INF/special.xml
它们能够被maven-war-plugin配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <overlays> <overlay> <groupId>com.acme</groupId> <artifactId>X</artifactId> <excludes> <exclude>bar.jsp</exclude> </excludes> </overlay> <overlay> <groupId>com.acme</groupId> <artifactId>Y</artifactId> <excludes> <exclude>baz.jsp</exclude> </excludes> </overlay> <overlay> </overlay> </overlays> </configuration> </plugin>
执行 jetty:run将得到如下资源:com.acme.X.war : com.acme.Y.war: ${project.basedir}/src/main/webapp。注意,当前项目的资源将会被放在最后一位<overlay/>元素中。你可使用它或者定义
<baseAppFirst>false</baseAppFirst>命令到jetty-maven-plugin.。
此外,因为上面配置的排除策略,请求资源 bar.jsp 将会从com.acme.Y.war中得到,而请求baz.jsp资源,将会返回404。
这个算法是很是简单的,经过依赖war包的声明顺序,而且不支持排除策略。<baseAppFirst>配置属性能够用来控制那个webapp的资源放在前面或者放在后面。
例如,让咱们当前webapp依赖以下两个war:
<dependency> <groupId>com.acme</groupId> <artifactId>X</artifactId> <type>war</type> </dependency> <dependency> <groupId>com.acme</groupId> <artifactId>Y</artifactId> <type>war</type> </dependency>
依赖的webapp包含以下:
WebAppX: /foo.jsp /bar.jsp /WEB-INF/web.xml WebAppY: /bar.jsp /baz.jsp /WEB-INF/web.xml /WEB-INF/special.xml
最终webapp中有效的文件为:
/foo.jsp (X) /bar.jsp (X) /baz.jsp (Y) /WEB-INF/web.xml (X) /WEB-INF/special.xml (Y)
你能够在这个插件中配置LoginServices,下面有一个为webapp设置HashLoginSercice的例子:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/test</contextPath> </webApp> <loginServices> <loginService implementation="org.eclipse.jetty.security.HashLoginService"> <name>Test Realm</name> <config>${project.basedir}/src/etc/realm.properties</config> </loginService> </loginServices> </configuration> </plugin>
若是你有额外的资源你想把它们包含在运行中的webAPP中,可是没有被打进war包中,你不能使用覆盖war包的方法,可是你能够告诉Jetty你想添加的目录的地址。当运行时,Jetty接收到一个资源的请求,它将搜索它配置下的全部资源文件。它和覆盖war包的状况很是类似,可是这不是一个war。下面有一个配置的例子:
<configuration> <webApp> <contextPath>/${build.finalName}</contextPath> <baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection"> <resourcesAsCSV>src/main/webapp,/home/johndoe/path/to/my/other/source,/yet/another/folder</resourcesAsCSV> </baseResource> </webApp> </configuration>
你可使用jetty.xml文件来配置你想要部署的额外的webapp,或者你可使用<contextHandlers> 配置元素来这么作。若是你想部署webapp A和webapp B和webapp C到你的Jetty实例中:
将这些配置信息放到webapp A的pom.xml文件中:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/test</contextPath> </webApp> <contextHandlers> <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> <war>${project.basedir}../../B.war</war> <contextPath>/B</contextPath> </contextHandler> <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> <war>${project.basedir}../../C.war</war> <contextPath>/C</contextPath> </contextHandler> </contextHandlers> </configuration> </plugin>
重要:
若是你是用ContextHandler 部署的是一个webapp,最好的作法是使用
org.eclipse.jetty.maven.plugin.JettyWebAppContext实例而不是使用
org.eclipse.jetty.webapp.WebAppContext实例。只有前者才会容许webapp 在maven环境中正常工做。
一样的,你也能够增长一个jetty.xml文件到你的webapp A中。而后复制这个到其余,并为其它两个应用增长WebAppContexts:
<Ref refid="Contexts"> <Call name="addHandler"> <Arg> <New class="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> <Set name="contextPath">/B</Set> <Set name="war">../../B.war</Set> </New> </Arg> </Call> <Call> <Arg> <New class="org.eclipse.jetty.maven.plugin.JettyWebAppContext"> <Set name="contextPath">/C</Set> <Set name="war">../../C.war</Set> </New> </Arg> </Call> </Ref>
这将把jetty.xml的地址配置到A的 Jetty插件中:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/test</contextPath> </webApp> <jettyXml>src/main/etc/jetty.xml</jettyXml> </configuration>
</plugin>
对于上面两种解决方案,其它webapp必须是已经构建好,而且它们没有被自动监听文件改变。
你能够特别的指定 name/value 对设置到系统属性中,为Jetty插件使用。这样的特性能够整理输入命令并节省大量的输入。
然而,有时不能使用这种功能来设置系统属性 - 有时使用系统属性的软件组件已经在maven运行的时候进行了初始化(这种状况下,你须要在命令行上提供系统属性),或者Jetty运行的时候。在后一种状况,你可使用maven的属性插件来定义系统属性。下面有一个例子又来配置logback日志系统做为Jetty的日志记录:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-2</version> <executions> <execution> <goals> <goal>set-system-properties</goal> </goals> <configuration> <properties> <property> <name>logback.configurationFile</name> <value>${project.baseUri}/resources/logback.xml</value> </property> </properties> </configuration> </execution> </executions> </plugin>
注意,若是一个系统属性已经被设置(例如,从命令行或者被JVM本身设置),那么默认这些属性将不会被覆盖(查看下面的<force>属性)。
这里有一个例子展现如何在POM中设置系统属性:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <systemProperties> <systemProperty> <name>fooprop</name> <value>222</value> </systemProperty> </systemProperties> <webApp> <contextPath>/test</contextPath> </webApp> </configuration> </plugin>
为了改变默认行为,使用<force>参数来保证系统属性覆盖命令行参数:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<systemProperties>
<force>true</force>
<systemProperty>
<name>fooprop</name>
<value>222</value>
</systemProperty>
</systemProperties>
<webApp>
<contextPath>/test</contextPath>
</webApp>
</configuration>
</plugin>
你也能够在一个文件中指定系统属性。在这种方式指定的系统属性将不会覆盖命令行的参数的系统属性、JVM的系统属性、POM中的系统属性。
包含以下相似内容的一个名为mysys.props的文件:
fooprop=222
这能够被配置到插件中,以下所示:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <systemPropertiesFile>${project.basedir}/mysys.props</systemPropertiesFile> <webApp> <contextPath>/test</contextPath> </webApp> </configuration> </plugin>
你也可使用经过系统属性jetty.systemPropertiesFile来设置文件位置。
若是你设置了一个非0的scanInterval 属性,那么Jetty的maven插件将会在每间隔指定的时间扫描肯定的文件,若是发现改变在须要的状况下将会从新部署。扫描的文件将依赖于执行的目标。
命令 | 扫描文件 |
jetty:run | pom.xml, <dependencies>, <classesDirectory>, <testClassesDirectory>, <webXml> 或者 <webAppSourceDirectory>/WEB-INF/web.xml, <jettyEnvXml> 或者 <webAppSourceDirectory>/WEB-INF/jetty-web.xml, <webAppSourceDirectory>/WEB-INF/jetty-web.xml, <scanTargets>, <scanTargetPatterns>, 任何webapp描述的默认描述文件和覆盖描述文件。 |
jetty:run-war | pom.xml, <war> |
jetty:run-exploded | pom.xml, <war>/WEB-INF/web.xml, <war>/WEB-INF/jetty-web.xml, <war>/WEB-INF/jetty-env.xml, <war>/WEB-INF/classes, <war>/WEB-INF/lib |
jetty:deploy-war | pom.xml, <war> |
jetty:run-forked | |
jetty:start | pom.xml, <dependencies> from the pom, <classesDirectory>, <testClassesDirectory>, <webXml> 或者 : <webAppSourceDirectory>/WEB-INF/web.xml, <jettyEnvXml> 或者: <webAppSourceDirectory>/WEB-INF/jetty-web.xml, <webAppSourceDirectory>/WEB-INF/jetty-web.xml, <scanTargets>, <scanTargetPatterns>, 任何webapp描述的默认描述文件和覆盖描述文件。 |
jetty:stop |
这个插件将帮助你预编译你的JSP并和maven war插件结合起来时候,将它们放到封装的war包中。
将jspc插件放到你的构建环境中有一个级别的安装操做:
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jspc-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <executions> <execution> <id>jspc</id> <goals> <goal>jspc</goal> </goals> <configuration> </configuration> </execution> </executions> </plugin>
配置的参数以下:
webXmlFragment
默认值为: $\{project.basedir}/target/webfrag.xml,一个包含servlet声明的文件,将会被合并到已经存在的web.xml中。
webAppSourceDirectory
默认值:$\{project.basedir}/src/main/webapp ,用来防止jsps、tags等资源的根目录。
webXml
默认值:$\{project.basedir}/src/main/webapp/WEB-INF/web.xml,web.xml文件用来和已经存在的片断合成。
includes
默认值:\/.jsp, \/.jspx ,一系列匹配须要的文件的条件。
excludes
默认值:\/.svn\/,一系列须要排除的条件。
classesDirectory
默认值:$\{project.build.outputDirectory},webapp的classes的路径。
generatedClasses
默认值: $\{project.build.outputDirectory} ,用来放置jsps生成的classes位置。
insertionMarker
默认值:none,在原web.xml文中的标记字符串,代表须要被合并到web.xml中的片断。注意,这个标记的字符串在插入的时候不会被隐藏掉。能够留空,这种状况下将会被插入到</web-app>以前。
useProvidedScope
默认值:false,若是为true,标有<scope>provided</scope> 中的jar包将会被classpath中的jar包替换。
mergeFragment
默认为true,不管是否将与源web.xml进行合并。合并后的文件将会放到与webXmlFragment文件同一路径。
keepSources
默认为false,若是为true的话,默认 .java 文件不会再执行的时候删除。
sourceVersion
jetty-9.3.6 版本之后的属性。jsp源文件的Java版本。默认为1.7。
targetVersion
jetty-9.3.6 版本之后的属性。jsps生成class文件的版本。默认为1.7。
tldJarNamePatterns
默认值:taglibs[^/]\.jar|.jstl-impl[^/]\.jar$,系统路径下,包含tlds文件的jar包匹配条件。使用 | 来分割每个条件。
jspc
默认值:被配置过的the org.apache.jasper.JspC。JspC类用来进行预编译。全部在JspC类上的setter都是有效的。
使用全部默认设置,下面有一个使用普通web.xml,包含全部jsp servlet声明的war插件配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webXml>${project.basedir}/target/web.xml</webXml> </configuration> </plugin>
通常只有在准备正式版本的时候编译jsp 可是在开发过程当中不太常常编译,最方便的作法是将插件放置到<profile> 中,这样能够伪装准备生产版本而进行编译。
例如,下面的profile只有在 -Dprod被输入时才会被执行:
<profiles> <profile> <id>prod</id> <activation> <property><name>prod</name></property> </activation> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jspc-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <!-- put your configuration in here --> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <!-- put your configuration in here --> </plugin> </plugins> </build> </profile> </profiles>
这样,下面的调用将会让你的代码进行编译,让jsp进行编译,<servlet>和<servlet-mapping>被插入到web.xml是,并将你的项目打成war包。
$ mvn -Dprod package
覆盖war时对jsp预编译须要多一点的配置。这是由于你须要分步骤解压须要覆盖的war文件,并将它们从新打包。因此jetty-jspc-maven-plugin讲有可能访问到覆盖的资源。
像下面例子中展现的,咱们将使用一个覆盖的war。覆盖的war文件将提供web.xml,可是jsps将被放置到src/main/webapp。咱们将解压war文件,编译jsps而且将servlet的定义合并到已经存在的web.xml中,而后将其打成war包。
这里有一个配置war插件的例子,分为解压步骤和打包步骤:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<goals><goal>exploded</goal></goals>
<phase>generate-resources</phase>
<configuration>
<webappDirectory>target/foo</webappDirectory>
<overlays>
<overlay />
<overlay>
<groupId>org.eclipse.jetty</groupId>
<artifactId>test-jetty-webapp</artifactId>
</overlay>
</overlays>
</configuration>
</execution>
<execution>
<id>pack</id>
<goals><goal>war</goal></goals>
<phase>package</phase>
<configuration>
<warSourceDirectory>target/foo</warSourceDirectory>
<webXml>target/web.xml</webXml>
</configuration>
</execution>
</executions>
</plugin>
接下来你让须要配置jetty-jspc-maven-plugin,这样插件才能使web.xml与额外的war文件中的servlets定义进行合并。这时在target/foo/WEB-INF/web.xml 中。使用默认的配置,web.xml将于jsp servlet 定义进行合并并放到target/web.xml中。
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jspc-maven-plugin</artifactId> <version>9.3.11.v20160721</version> <executions> <execution> <id>jspc</id> <goals> <goal>jspc</goal> </goals> <configuration> <webXml>target/foo/WEB-INF/web.xml</webXml> <includes>**/*.foo</includes> <excludes>**/*.fff</excludes> </configuration> </execution> </executions> </plugin>
附言:
Jetty文档的目录详见:http://www.cnblogs.com/yiwangzhibujian/p/5832294.html
Jetty第一章翻译详见:http://www.cnblogs.com/yiwangzhibujian/p/5832597.html
Jetty第四章(21-22)详见:http://www.cnblogs.com/yiwangzhibujian/p/5845623.html
这是翻译的第四部分的第23小节,主要是Maven的使用,若是项目不使用Maven管理能够跳过本文。一开始翻译老是感受表达不出英文的本意,不过慢慢以为我要作的不是一字不差的把原文翻译过来,而是要用中文的思惟来表达英文的意思,我以为之后的翻译会容易理解些。