Java Grizzly 和 Jersey 独立罐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9787265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:39:34  来源:igfitidea点击:

Grizzly and Jersey standalone jar

javamavenjerseygrizzly

提问by André Ricardo

I am trying to package Grizzly with Jersey as a single jar using Maven shade plugin. But I always get the message No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler

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

The code works fine in Eclipse, but not in a packaged jar:

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

Here is the complete exception

这是完整的例外

$ 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)

I am building the jar package using maven with

我正在使用 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>

Do I need to change the shade plugin to include anything else?

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

采纳答案by Martin Matula

The error looks like the plugin is not merging the META-INF/services records from different jars correctly - if there are multiple files with the same name in META-INF/services directory of several jars, they need to be merged, not replaced one by the other. Check if that is the case.

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

回答by Brian Ling

I just made the stupid mistake. Configure maven-assembly-plugin in pom as well.

我只是犯了一个愚蠢的错误。也在 pom 中配置 maven-assembly-plugin。

Assembly seems to replace META-INF/services and override "com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainerProvider " in File com.sun.jersey.spi.container.ContainerProvider

Assembly 似乎替换了 META-INF/services 并覆盖了文件com.sun.jersey.spi.container.ContainerProvider 中的“com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainerProvider”

As guide http://maven.apache.org/plugins/maven-assembly-plugin/mentioned, If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin.

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

  <!-- 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>  

回答by Wolfgang Fahl

The following links helped me figuring out the solution below:

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

especially the answer

特别是答案

Instead of using the jar-with-dependencies as the descriptorRef of your assembly-plugin configuration you create our own e.g. in src/assembly/depmerge.xml (see below). This assembly configuration will add a containerDescriptorHandler that cares for the META-INF/services.

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

run

mvn clean compile assembly:single

to get a jar file in target which you can call with

在目标中获取一个 jar 文件,您可以调用它

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

pom.xml:

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/assembly/depmerge.xml:

src/assembly/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>