Jersey problems with Maven - Shade Plugin

2020-07-11 08:59发布

问题:

My problem is very similar to: Jersey exception only thrown when depencencies assembled into a single jar

I run my application (jetty embedded + jersey) and everything works. When i try to create a executable JAR i get the error:

org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo GRAVE: MessageBodyWriter not found for media type=application/json, type=class

My POM.XML:

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>server.application.DeepDig</mainClass>
                                </transformer>
                            </transformers>

                            <filters>
                                <!-- filter to address "Invalid signature file" issue - see https://stackoverflow.com/a/6743609/589215 -->
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

回答1:

I ran into the same problem a while ago. The problem is with the services files not getting merged. The following works for me:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>Foo</mainClass>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                </transformers>
            </configuration>
        </plugin>

The root cause of the problem is that each of several Jersey-related jars contain a "services" file in META-INF that contains metadata for Jersey to work properly. By default, the shade plugin picks one of these files and includes it in the fat jar. Because metadata from the other files is not included, Jersey doesn't work properly.

This fix includes the additional transformer when the shade plugin is invoked. This transformer merges the data in the various files rather than just picking one of the files. In this way, all of the required metadata is included in the fat jar and Jersey works properly.