说明
由于EclEmma支持如下几种启动方式
Local Java application
Eclipse/RCP application
Equinox OSGi framework
JUnit test
TestNG test
JUnit plug-in test
JUnit RAP test
SWTBot test
Scala application
我们的Web程序怎么办?使用Tomcat启动的Web应用是无法使用的。
为了解决这个问题,我决定使用Jetty,通过Java application来启动web应用
配置
需要在pom中增加如下依赖
org.eclipse.jetty
jetty-server
9.2.9.v20150224
org.eclipse.jetty
jetty-webapp
9.2.9.v20150224
建立jetty\etc目录(eclipse工程下),然后再这目录下添加2个配置文件
jetty.xml
false
https
512
-
-
true
5000
webdefault.xml
Default web.xml file.
This file is applied to a Web application before it's own WEB_INF/web.xml file
org.eclipse.jetty.servlet.listener.ELContextCleaner
org.eclipse.jetty.servlet.listener.IntrospectorCleaner
default
org.eclipse.jetty.servlet.DefaultServlet
aliases
false
acceptRanges
true
dirAllowed
true
welcomeServlets
false
redirectWelcome
false
maxCacheSize
256000000
maxCachedFileSize
200000000
maxCachedFiles
2048
gzip
false
etags
false
useFileMappedBuffer
true
0
default
/
jsp
org.eclipse.jetty.jsp.JettyJspServlet
logVerbosityLevel
DEBUG
fork
false
xpoweredBy
false
compilerTargetVM
1.7
compilerSourceVM
1.7
0
jsp
*.jsp
*.jspf
*.jspx
*.xsp
*.JSP
*.JSPF
*.JSPX
*.XSP
30
index.html
index.htm
index.jsp
ar
ISO-8859-6
be
ISO-8859-5
bg
ISO-8859-5
ca
ISO-8859-1
cs
ISO-8859-2
da
ISO-8859-1
de
ISO-8859-1
el
ISO-8859-7
en
ISO-8859-1
es
ISO-8859-1
et
ISO-8859-1
fi
ISO-8859-1
fr
ISO-8859-1
hr
ISO-8859-2
hu
ISO-8859-2
is
ISO-8859-1
it
ISO-8859-1
iw
ISO-8859-8
ja
Shift_JIS
ko
EUC-KR
lt
ISO-8859-2
lv
ISO-8859-2
mk
ISO-8859-5
nl
ISO-8859-1
no
ISO-8859-1
pl
ISO-8859-2
pt
ISO-8859-1
ro
ISO-8859-2
ru
ISO-8859-5
sh
ISO-8859-5
sk
ISO-8859-2
sl
ISO-8859-2
sq
ISO-8859-2
sr
ISO-8859-5
sv
ISO-8859-1
tr
ISO-8859-9
uk
ISO-8859-5
zh
GB2312
zh_TW
Big5
Disable TRACE
/
TRACE
Enable everything but TRACE
/
TRACE
代码
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.xml.sax.SAXException;
public class JettyServer extends Server {
private String xmlConfigPath;
private String contextPath;
private String warPath;
// 根据实际情况进行修改
private String resourceBase = "./src/main/webapp";
// 根据实际情况进行修改
private String webXmlPath = "./src/main/webapp/WEB-INF/web.xml";
public JettyServer(int port,String xmlConfigPath, String contextPath,
String resourceBase, String webXmlPath) {
this(port,xmlConfigPath, contextPath, resourceBase, webXmlPath, null);
}
public JettyServer(int port,String xmlConfigPath, String contextPath) {
this(port,xmlConfigPath, contextPath, null, null, null);
}
public JettyServer(int port,String xmlConfigPath, String contextPath,
String warPath) {
this(port,xmlConfigPath, contextPath, null, null, warPath);
}
public JettyServer(int port,String xmlConfigPath, String contextPath,
String resourceBase, String webXmlPath, String warPath) {
super(port);
if (StringUtils.isNotBlank(xmlConfigPath)) {
this.xmlConfigPath = xmlConfigPath;
readXmlConfig();
}
if (StringUtils.isNotBlank(warPath)) {
this.warPath = warPath;
if (StringUtils.isNotBlank(contextPath)) {
this.contextPath = contextPath;
applyHandle(true);
}
} else {
if (StringUtils.isNotBlank(resourceBase))
this.resourceBase = resourceBase;
if (StringUtils.isNotBlank(webXmlPath))
this.webXmlPath = webXmlPath;
if (StringUtils.isNotBlank(contextPath)) {
this.contextPath = contextPath;
applyHandle(false);
}
}
}
private void readXmlConfig() {
try {
XmlConfiguration configuration = new XmlConfiguration(
new FileInputStream(this.xmlConfigPath));
configuration.configure(this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (SAXException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void applyHandle(Boolean warDeployFlag) {
ContextHandlerCollection handler = new ContextHandlerCollection();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath(contextPath);
webapp.setDefaultsDescriptor("./jetty/etc/webdefault.xml");
if (!warDeployFlag) {
webapp.setResourceBase(resourceBase);
webapp.setDescriptor(webXmlPath);
} else {
webapp.setWar(warPath);
}
handler.addHandler(webapp);
super.setHandler(handler);
}
public void startServer() {
try {
super.start();
System.out.println("current thread:"
+ super.getThreadPool().getThreads() + "| idle thread:"
+ super.getThreadPool().getIdleThreads());
super.join();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getXmlConfigPath() {
return xmlConfigPath;
}
public void setXmlConfigPath(String xmlConfigPath) {
this.xmlConfigPath = xmlConfigPath;
}
public String getContextPath() {
return contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}
public String getResourceBase() {
return resourceBase;
}
public void setResourceBase(String resourceBase) {
this.resourceBase = resourceBase;
}
public String getWebXmlPath() {
return webXmlPath;
}
public void setWebXmlPath(String webXmlPath) {
this.webXmlPath = webXmlPath;
}
public String getWarPath() {
return warPath;
}
public void setWarPath(String warPath) {
this.warPath = warPath;
}
}
启动程序,这里可以指定端口进行运行。使用Covearage的方式运行这个程序可以进行覆盖率测试。
public class JettyServerStart {
public static void main(String[] args) {
JettyServer server = new JettyServer(9090,
"./jetty/etc/jetty.xml", "/");
server.startServer();
}
}