Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resteasy providers missing after mvn package

I have written some code that uses some resteasy libraries.

The code works fine in Eclipse but produces exceptions when excecuted as fat-jar build with the maven shade plugin.

The reason: in the created jar under META-INF/services/javax.ws.rs.ext.Providers only the providers from resteasy-client are listed. I however also need the providers from resteasy-jackson2-provider and from resteasy-jaxrs.

I think the issue might be, that all 3 libraries (resteasy-client, resteasy-jackson2-provider, resteasy-jaxrs) use an identically named file to list their providers (META-INF/services/javax.ws.rs.ext.Providers). So maybe maven overwrites the provider list from one library with the list from the others?

My pom.xml looks like this:

<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>
like image 933
Bob Sheknowdas Avatar asked Dec 08 '22 13:12

Bob Sheknowdas


2 Answers

Ok, i found the solution.

Maven shade plugin allows you to force append files that are identically named...

So what the maven-shade-plugin in the pom.xml lags is:

<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

like image 61
Bob Sheknowdas Avatar answered Dec 23 '22 14:12

Bob Sheknowdas


The accepted answer is correct, but is for Maven's Shade plugin. If you're using Gradle's Shadow plugin, you must merge service descriptor files.

Using Gradle's Kotlin DSL:

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

Using Gradle's Groovy DSL:

shadowJar { mergeServiceFiles() }
like image 32
Neel Kamath Avatar answered Dec 23 '22 14:12

Neel Kamath