使用maven程序集插件构建uberjar然后将其打包成zip文件时,遇到运行时故障:
java.lang.RuntimeException: could not find writer for content-type text/xml type: java.lang.String
当我在eclipse中运行我的项目时,或者当我使用eclipse Export创建和执行.jar时,不会发生这种失败 - > Runnable Jar File所以我怀疑我使用maven创建uberjar的方式有问题。
如何解决此问题?
答案 0 :(得分:4)
事实证明,我的问题的根源是与maven程序集插件创建jar时发生的javax.ws.rs.ext.Providers文件冲突。 (此文件可在META-INF中的uberjar中找到 - > services - > javax.ws.rs.ext.Providers)
Providers文件包含可用提供程序类的列表。在我的项目的依赖项中,此文件存在于多个位置,并且不同的副本包含不同的提供程序列表。 maven程序集插件只是选择一个版本包含在jar中,因此在运行时无法找到所需的“writer”类:此类未在jar中的Providers文件中列出。
我使用了maven shade插件来克服这个问题。 shade插件包含有选择地合并依赖关系树中包含的重复文件的工具。在pom.xml中:
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
</transformer>
通过附加任何重复的javax.ws.rs.ext.Providers告诉maven合并。
此外,通过将maven shade插件设置为在构建的package
阶段执行,然后在install
阶段执行maven程序集插件,我能够创建可执行的uberjar ,然后将该uberjar封装在.zip文件中,所有这些都使用简单的mvn clean install
调用。
这是我的pom.xml的样子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
...
</parent>
<groupId>com.foo.bar</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>2.1.0.0-SNAPSHOT</version>
<name>My App</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<issues-product>MyApp</issues-product>
<issues-component>MY-APP</issues-component>
</properties>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.foo.bar.MyMainClass</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/assembly/my-app-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</project>
这是my-app-assembly.xml:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.2.2 http://maven.apache.org/xsd/assembly-2.2.2.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory/>
<includes>
<include>Readme.pdf</include>
<include>config\</include>
<include>input\</include>
<include>output\</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>bin\java\</outputDirectory>
<includes>
<include>my-app.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>