Jersey problems with Maven - Shade Plugin

喜你入骨 提交于 2020-01-01 10:04:22

问题


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.



来源:https://stackoverflow.com/questions/29107376/jersey-problems-with-maven-shade-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!