首先让我解释一下我的要求。 我正在尝试构建一个带有嵌入式jetty服务器的JAX-RS应用程序来提供服务:
/ => Single Page Application (static index.html, JS, images )
/api => Driving API (JAX-RS resources)
我遇到的问题是使用API 不,而是使用静态资源。我目前的配置是:
public static void main( final String[] args ) throws Exception { // create our server on port 4567 final Server server = new Server( 4567 ); // create a default context handler for our server to root path with sessions enabled final ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS ); // our root static path // create container for JAX-RS servlet resources mapped with configuration final ServletContainer servletContainer = new ServletContainer( new MyConfig() ); // create our servlet holder for our multiple JAX-RS resources final ServletHolder apiServletHolder = new ServletHolder( servletContainer ); // map our JAX-RS resources to begin at root route context.addServlet( apiServletHolder, "/api/*" ); final ServletHolder staticServlet = new ServletHolder( new DefaultServlet() ); staticServlet.setInitParameter( "resourceBase", Main.class.getClassLoader().getResource( "." ).toString() ); context.addServlet( staticServlet, "/" ); server.setHandler( context ); server.start(); server.join(); }
我的文件夹配置如下:
├───main
│ ├───java
│ │ └───com
│ │ └───bar
│ │ └───foo
│ │ └───controllers
│ └───static
│ └───images
└───test
└───java
└───com
└───bar
└───foo
我的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>com.foo.bar</groupId>
<artifactId>jettyApp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jettyApp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>com.bar.foo.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.foo.bar.Main</Main-Class>
<Class-Path>.</Class-Path>
<Build-Number>123</Build-Number>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<jetty.version>9.3.0.M2</jetty.version>
<jersey.version>2.7</jersey.version>
</properties>
</project>
当我创建一个带阴影的jar时,应用程序失败,因为静态资源的位置显示我正在运行应用程序的文件夹:
$ pwd
/home/workspace/example
$ java -jar example.jar
# server starts
$ curl localhost:4567
# tree of /home/workspace/example
毫无疑问,这是因为在Main.class中设置了资源库。
我的问题:
如何打包并以适用于IDE和独立超级jar的方式对其进行配置?
提前致谢:)