2015-11-05 29 views
0

我有一個java項目,我正在使用嵌入式jetty,而且我還需要將其打包到獨立的JAR中。我成功地完成了所有這些工作,並獲得了servlet(HelloServlet)以正確響應所有內容。問題是,如果我去根(本例中是8080),我得到一個文件下載。當我檢查它時,我注意到它與我的JAR具有相同的大小,所以我將它重命名爲.zip並將它打開,看哪,這是我的獨立JAR。爲什麼Jetty獨立JAR會自行下載? (與Maven打包在一起)

這是怎麼發生的?

我該如何預防? (我是否做錯了什麼/時髦?)

對結構的評論是受歡迎的,如果有什麼不清楚的話我會很樂意提供更多的信息。

/src/main/java/my/package/MyServer.java

package my.package; 

import org.apache.log4j.BasicConfigurator; 
import org.apache.log4j.Logger; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.webapp.WebAppContext; 

import java.net.URL; 
import java.security.ProtectionDomain; 

public class MyServer 
{ 
    private Server server; 

    private MyServer() {} 

    public static void main(String[] args) throws Exception 
    { 
     BasicConfigurator.configure(); 
     Logger logger = Logger.getLogger("MyServer"); 

     int port = 8080; 

     Server server = new Server(port); 

     logger.info("Server created at port " + port); 

     ProtectionDomain protectionDomain = MyServer.class.getProtectionDomain(); 

     logger.info(protectionDomain); 

     URL url = protectionDomain.getCodeSource().getLocation(); 

     logger.info(url); 

     String rootPath = url.toExternalForm(); 

     logger.info("rootPath: " + rootPath); 

     WebAppContext webAppContext = new WebAppContext(); 
     webAppContext.setContextPath("/"); 
     webAppContext.setResourceBase(rootPath); 
     webAppContext.setParentLoaderPriority(true); 
     webAppContext.setWar(rootPath); 

     webAppContext.addServlet(HelloServlet.class, "/hello"); 

     server.setHandler(webAppContext); 

     try 
     { 
     server.start(); 
     server.join(); 
     } 
     catch (Exception e) 
     { 
     e.printStackTrace(); 
     System.exit(100); 
     } 

    } 
} 

/src/main/java/my/package/HelloServlet.java

package my.package; 

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()); 
    } 
} 

/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>my.package</groupId> 
    <artifactId>myserver</artifactId> 
    <packaging>war</packaging> 
    <version>1.1-SNAPSHOT</version> 
    <name>My Server</name> 
    <url>http://www.fake.nogo</url> 

    <properties> 
    <jettyVersion>9.2.13.v20150730</jettyVersion> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.17</version> 
    </dependency> 
    <dependency> 
     <groupId>org.eclipse.jetty.aggregate</groupId> 
     <artifactId>jetty-all</artifactId> 
     <version>9.2.13.v20150730</version> 
    </dependency> 
    </dependencies> 

    <build> 
    <finalName>myserver</finalName> 

    <plugins> 
     <plugin> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-maven-plugin</artifactId> 
     <version>9.2.6.v20141205</version> 
     </plugin> 

     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-jar-plugin</artifactId> 
     <version>2.6</version> 
     <executions> 
      <execution> 
      <id>package-jar</id> 
      <phase>package</phase> 
      <goals> 
       <goal>jar</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 

     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <version>2.6</version> 
     <configuration> 
      <descriptors> 
      <descriptor>src/main/assembly/distribution.xml</descriptor> 
      </descriptors> 
      <archive> 
      <manifest> 
       <mainClass>my.package.MyServer</mainClass> 
      </manifest> 
      </archive> 
     </configuration> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

/src/main/assembly/distribution.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> 
    <id>standalone</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <baseDirectory></baseDirectory> 
    <dependencySets> 
     <dependencySet> 
      <unpack>true</unpack> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

/src/main/webapp/WEB-INF/web.xml

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>My Server</display-name> 

    <servlet> 
     <servlet-name>default</servlet-name> 
     <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class> 
     <init-param> 
      <param-name>dirAllowed</param-name> 
      <param-value>false</param-value> 
     </init-param> 
    </servlet> 

</web-app> 

我做mvn clean package然後java -jar target/myserver-standalone.jar運行它。

+0

什麼記錄爲您ROOTPATH?如果你從一個jar開始你的應用程序,它應該是一個文件:url指向jar,而不是包含jar的文件夾。我想用這個URL作爲資源庫應該是你問題的原因。 –

+0

@MichaelKoch謝謝你的迴應!記錄的rootPath實際上指向獨立的jar(否則我猜HelloServlet將無法工作?) – bananpermobil

+0

我設法讓它工作,通過廢除所有東西並從此構建 - > https://github.com/thomasWeise/standAloneJSPsWithJetty – bananpermobil

回答

0

嘗試配置Maven的組裝插件這種方式:

<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-assembly-plugin</artifactId> 
<version>2.6</version> 
<configuration> 
    <descriptorRefs> 
     <descriptorRef>jar-with-dependencies</descriptorRef> 
    </descriptorRefs> 
    <finalName>myserver-standalone</finalName> 
    <appendAssemblyId>false</appendAssemblyId> 
    <archive> 
     <manifest> 
      <mainClass>my.package.MyServer</mainClass> 
     </manifest> 
    </archive> 
</configuration> 
+0

感謝您的迴應!我嘗試了你的解決方案,並從我的開始解決方案中添加了執行標籤(否則它不會構建最終的獨立jar)。但它的工作原理與我的初始解決方案一樣。 – bananpermobil

相關問題