我有一个java项目,我使用嵌入式jetty,我还需要将它打包成一个独立的JAR。我成功完成了所有这些并获得了一个servlet(HelloServlet)来正确响应所有内容。问题是,如果我转到root(本例中为localhost:8080),我会得到一个文件下载。当我检查它时,我注意到它与我的JAR大小相同,所以我将它重命名为.zip并打开它,瞧,这是我独立的JAR。
为什么会这样?
我该怎样防止这种情况? (我做错了什么/时髦吗?)
欢迎对结构进行评论,如果不清楚,我会很乐意提供更多信息。
/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 :(得分:0)
尝试以这种方式配置maven-assembly-plugin:
<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>