2

这是一个简单的问题:是否可以使用来自不同项目的嵌入式码头启动网络应用程序?我正在尝试运行(使用 JUnit)以下代码:

Server server = new Server(80);
WebAppContext context = new WebAppContext();
File webXml = new File("../Project1/src/main/webapp/WEB-INF/web.xml");
context.setDescriptor(webXml.getAbsolutePath());
context.setResourceBase("../Project1/src/main/webapp");
context.setContextPath("/");
context.setParentLoaderPriority(false);
server.setHandler(context);
server.start();

如果我从另一个项目(比如 Project2)执行此操作,码头会引发很多异常:javax.servlet.UnavailableException: com.sun.xml.ws.transport.http.servlet.WSSpringServlet java.lang.ClassNotFoundException: com.sun。 xml.ws.transport.http.servlet.WSSpringServlet

我尝试将 Project1 添加到 Project 的 2 类路径中,但这无济于事。如果我尝试在同一个 Project1 中运行相同的内容(当然,所有路径都已调整) - 一切正常。

感谢您的帮助。

4

2 回答 2

0

这可能是由于相对路径字符串。

这是使用 JUnit Assert 的另一种方法......

    Server server = new Server(80);
    WebAppContext context = new WebAppContext();
    File otherProject = new File("../Project1");
    Assert.assertTrue("Project1 should exist", otherProject.exists());

    // make path reference canonical (eliminate the relative path reference)
    otherProject = otherProject.getCanonicalFile();
    File webAppDir = new File(otherProject, "src/main/webapp");
    Assert.assertTrue("Webapp dir should exist", webAppDir.exists());
    File webXml = new File(webAppDir, "WEB-INF/web.xml");
    Assert.assertTrue("web.xml should exist", webXml.exists());

    context.setDescriptor(webXml.getAbsolutePath());
    context.setResourceBase(webAppDir.getAbsolutePath());
    context.setContextPath("/");
    context.setParentLoaderPriority(false);
    server.setHandler(context);
    server.start();

或者这可能是由于../Project1/src/main/webapp/WEB-INF/lib没有您需要的依赖项。这很重要,因为 WebAppContext 将WEB-INF/lib首先使用提供的内容,然后是服务器类路径。

于 2013-05-14T14:59:14.243 回答
0

所以我得到了解决方案,

如果不可能,您可以将原始 webapp 项目作为依赖项包含在内,或者使用自定义类加载器:

WebAppClassLoader customLoader = new WebAppClassLoader(context);
customLoader.addClassPath("../Project1/target/webapp/WEB-INF/classes");
Resource jars = Resource.newResource("../Project1/target/webapp/WEB-INF/lib");
customLoader.addJars(jars);
webapp.setClassLoader(customLoader);
于 2013-07-16T11:41:06.037 回答