Java运行时异常:“找不到内容类型的编写器"在构建uberjar和zip包时 [英] java runtime exception: "could not find writer for content type" when building uberjar and zip package

查看:84
本文介绍了Java运行时异常:“找不到内容类型的编写器"在构建uberjar和zip包时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用maven程序集插件构建uberjar并将其打包为zip文件时,遇到运行时失败:

When using the maven assembly plugin to build an uberjar and then package it into a zip file, I encounter a runtime failure:

java.lang.RuntimeException: could not find writer for content-type text/xml type: java.lang.String

当我在eclipse中运行项目时,或者当我使用eclipse Export-> Runnable Jar File创建并执行.jar时,都不会发生此故障,因此我怀疑我使用maven创建uberjar.

This failure does not occur when I run my project within eclipse, or when I create and executable .jar using the eclipse Export -> Runnable Jar File so I suspect there is something wrong with the way I'm using maven creating the uberjar.

如何解决此问题?

推荐答案

原来,我的问题的根源是与javax.ws.rs.ext.Providers文件的冲突,该文件在maven程序集插件创建jar时发生. (可以在META-INF->服务-> javax.ws.rs.ext.Providers中的uberjar中找到此文件)

Turns out, the root of my problem was a conflict with the javax.ws.rs.ext.Providers file that occurs when the maven assembly plugin creates the jar. (This file can be found in the uberjar within META-INF -> services -> javax.ws.rs.ext.Providers)

提供者文件包含可用提供者类的列表.在我的项目的依存关系中,此文件存在多个位置,并且不同的副本包含不同的提供程序列表. maven程序集插件只是选择了要包含在jar中的一个版本,因此在运行时找不到所需的"writer"类:该类未在jar的Providers文件中列出.

The Providers file contains a list of available provider classes. Within the dependencies of my project this file exists in more than one place, and the different copies contain different provider lists. The maven assembly plugin simply chooses one version to include in the jar, and so at runtime the required "writer" class cannot be found: This class is not listed in the Providers file within the jar.

我使用了Maven Shade插件来解决此问题. shade插件包含一种工具,可以有选择地合并依赖关系树中包含的重复文件.在pom.xml中:

I used the maven shade plugin to overcome this problem. The shade plugin contains a facility to selectively merge duplicate files contained within the dependency tree. Within 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进行合并.

Tells maven to merge by appending any duplicates of javax.ws.rs.ext.Providers.

此外,通过将Maven Shade插件设置为在构建的package阶段执行,然后将Maven程序集插件在install阶段执行,我能够创建可执行文件uberjar,然后将其打包.zip文件中的uberjar,全部使用简单的mvn clean install调用.

Also, by setting the maven shade plugin to execute during the package phase of my build, and then the maven assembly plugin to execute at the install phase, I was able to create an executable uberjar, then package that uberjar within a .zip file, all with a simple mvn clean install invocation.

这是我的pom.xml的样子:

Here's what my pom.xml looks like:

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

And here is 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>

这篇关于Java运行时异常:“找不到内容类型的编写器"在构建uberjar和zip包时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆