【问题标题】:resteasy providers missing after mvn package在 mvn 包之后缺少 resteasy 提供程序
【发布时间】:2018-10-09 07:43:23
【问题描述】:

我写了一些代码,使用了一些 resteasy 库。

代码在 Eclipse 中运行良好,但在使用 maven shade 插件作为 fat-jar 构建执行时会产生异常。

原因:在 META-INF/services/javax.ws.rs.ext.Providers 下创建的 jar 中只列出了来自 resteasy-client 的提供程序。 但是,我还需要来自 resteasy-jackson2-provider 和来自 resteasy-jaxrs 的提供程序。

我认为问题可能在于,所有 3 个库(resteasy-client、resteasy-jackson2-provider、resteasy-jaxrs)都使用同名文件来列出它们的提供者(META-INF/services/javax.ws.rs .ext.Providers)。 那么也许 maven 会用其他库中的列表覆盖一个库中的提供程序列表?

我的 pom.xml 看起来像这样:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
  <dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.6.1.Final</version>
  </dependency>
  <dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson2-provider</artifactId>
    <version>3.6.1.Final</version>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.3</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <filters>
              <filter>
                <artifact>*:*</artifact>
                <excludes>
                  <exclude>META-INF/*.SF</exclude>
                  <exclude>META-INF/*.DSA</exclude>
                  <exclude>META-INF/*.RSA</exclude>
                </excludes>
              </filter>
            </filters>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <manifestEntries>
                  <Main-Class>playarounds.ServiceMainClass</Main-Class>
                </manifestEntries>
              </transformer>
            </transformers>
            <artifactSet/>
            <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

【问题讨论】:

    标签: java maven resteasy


    【解决方案1】:

    好的,我找到了解决方案。

    Maven shade 插件允许你强制追加同名文件...

    所以 pom.xml 中的 maven-shade-plugin 滞后是:

    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
    </transformer>
    

    https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

    【讨论】:

    • 救了我的命 :)
    【解决方案2】:

    接受的答案是正确的,但适用于 Maven 的 Shade 插件。如果你使用 Gradle 的 Shadow 插件,你必须merge service descriptor files

    使用 Gradle 的 Kotlin DSL:

    tasks {
        withType<ShadowJar> { mergeServiceFiles() }
    }
    

    使用 Gradle 的 Groovy DSL:

    shadowJar { mergeServiceFiles() }
    

    【讨论】:

      猜你喜欢
      • 2016-08-02
      • 2015-10-13
      • 1970-01-01
      • 2018-05-30
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2018-06-10
      相关资源
      最近更新 更多