Grizzly and Jersey standalone jar

后端 未结 3 961
慢半拍i
慢半拍i 2020-12-09 21:52

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.gla

相关标签:
3条回答
  • 2020-12-09 22:02

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

    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

    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.

      <!-- 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>  
    
    0 讨论(0)
  • 2020-12-09 22:13

    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.

    0 讨论(0)
  • 2020-12-09 22:21

    The following links helped me figuring out the solution below:

    • http://maven.apache.org/guides/mini/guide-assemblies.html
    • http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
    • How can I merge resource files in a Maven assembly?

    especially the answer

    • https://stackoverflow.com/a/11331222/1497139

    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.

    run

    mvn clean compile assembly:single
    

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

    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/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>
    
    0 讨论(0)
提交回复
热议问题