2013-05-14 62 views
2

這裏是一個快速的問題:是否有可能從一個不同的項目啓動帶有嵌入式碼頭的web-app? 我試圖運行(使用JUnit)下面的代碼:如何從一個不同的項目啓動webapp與嵌入式碼頭

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到項目的類路徑2,但這不利於這種情況。 如果我嘗試在相同的Project1中運行相同的程序(當然,所有路徑都進行了調整) - 一切正常。

謝謝你的幫助。

回答

0

所以我得到了解決,

你要麼包括原始的Web應用程序項目作爲一個依賴或使用自定義類加載器,如果這是不可能的:

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); 
0

這可能是由於相對路徑字符串。

下面是使用JUnit斷言過的另一種方法...

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內容,然後是服務器類路徑。

+0

儘管如此,應用程序丟失所有類。如果您正在目標文件夾下查找Web應用程序,則所有庫和類都可以在WEB-INF/lib中找到。如果我將webAppDir作爲目標/ webappname鏈接,則它會啓動。如果它是一個你想要啓動的源文件(src/main/webapp),所有的類和庫鏈接到項目中(在這種情況下不同)。這裏的任何想法?也許將服務器類路徑擴展到project1? 不過,謝謝你的幫助。 – artemonster 2013-05-16 06:34:13