Grizzly和Jersey独立罐子

时间:2012-03-20 13:00:36

标签: java maven jersey grizzly

我正在尝试使用Maven shade插件将Grizzly与Jersey打包成一个单独的jar。但我总是得到消息No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler

代码在Eclipse中运行良好,但不适用于打包的jar:

public class Main {

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost/").port(9998).build();
}

public static final URI BASE_URI = getBaseURI();

protected static HttpServer startServer() throws IOException {
    System.out.println("Starting grizzly...");
    ResourceConfig rc = new PackagesResourceConfig("share.test");
    rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
}

public static void main(String[] args) throws IOException {
    HttpServer httpServer = startServer();
    System.in.read();
    httpServer.stop();
}    
}

这是完整的例外

$ java -jar target/webServiceTest-0.0.1-SNAPSHOT.jar
Starting grizzly...
Mar 20, 2012 12:48:53 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  share.test
Mar 20, 2012 12:48:54 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class share.test.NonJAXBBeanResource
  class share.test.Hello
Mar 20, 2012 12:48:54 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Exception in thread "main" java.lang.IllegalArgumentException: No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:196)
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:134)
    at com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory.createHttpServer(GrizzlyServerFactory.java:242)
    at share.test.Main.startServer(Main.java:27)
    at share.test.Main.main(Main.java:31)

我正在使用maven和

构建jar包
<build>
 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-shade-plugin</artifactId>
   <version>1.5</version>
   <configuration>
   <transformers>
   <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
     <manifestEntries>
      <Main-Class>share.test.Main</Main-Class>
      <Build-Number>1</Build-Number>
     </manifestEntries>
    </transformer>
  </transformers>
 </configuration>
<executions>
 <execution>
  <phase>package</phase>
   <goals>
    <goal>shade</goal>
   </goals>
 </execution>
</executions>
</plugin>
</plugins>
</build>

我是否需要更改阴影插件以包含其他内容?

3 个答案:

答案 0 :(得分:8)

错误看起来插件没有正确合并来自不同jar的META-INF /服务记录 - 如果在几个jar的META-INF / services目录中有多个具有相同名称的文件,则需要将它们合并,没有被另一个替换。检查是否是这种情况。

答案 1 :(得分:1)

以下链接帮助我找出了以下解决方案:

尤其是答案

不是使用jar-with-dependencies作为程序集插件配置的descriptorRef,而是创建我们自己的例如。在src / assembly / depmerge.xml中(见下文)。此程序集配置将添加一个关注META-INF / services的containerDescriptorHandler。

运行

mvn clean compile assembly:single

获取目标中的jar文件,您可以使用

调用
java -jar target/x.y.-version-jar-with-dependencies.jar

<强>的pom.xml:

      <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>${mainClass}</mainClass>
                    </manifest>
                </archive>
          <descriptor>src/assembly/depmerge.xml</descriptor>
          </configuration>
        </plugin>

<强>的src /组件/ depmerge.xml:

<!-- 
 see http://maven.apache.org/guides/mini/guide-assemblies.html 
 see http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html 
-->
<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">
    <!-- TODO: a jarjar format would be better -->
    <id>jar-with-dependencies-and-services</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <!-- 
      https://stackoverflow.com/questions/1607220/how-can-i-merge-resource-files-in-a-maven-assembly
     -->
    <containerDescriptorHandlers>
        <containerDescriptorHandler>
            <handlerName>metaInf-services</handlerName>
        </containerDescriptorHandler>
    </containerDescriptorHandlers>
</assembly>

答案 2 :(得分:0)

我刚犯了愚蠢的错误。在pom中配置maven-assembly-plugin。

程序集似乎取代了META-INF / services并覆盖了“com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainerProvider” “在文件 com.sun.jersey.spi.container.ContainerProvider

正如提到的指南http://maven.apache.org/plugins/maven-assembly-plugin/,如果您的项目想要将工件打包在超级jar中,则程序集插件仅提供基本支持。要获得更多控制,请使用Maven Shade插件。

  <!-- mvn assembly:assembly -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>XXX.DaemonMain</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>