事實證明,我的問題的根源是與當行家組件插件創建的jar發生javax.ws.rs.ext.Providers文件有衝突。 (該文件可以在META-INF - > services - > javax.ws.rs.ext.Providers中的uberjar中找到)
Providers文件包含可用提供程序類的列表。在我的項目的依賴項中,該文件存在於多個位置,並且不同的副本包含不同的提供者列表。 Maven的組裝插件只需選擇一個版本的jar包括,所以在運行時所需要的「作家」類不能被發現:這個類不是在罐子內的供應商文件中列出。
我使用Maven的遮陽插件來克服這個問題。陰影插件包含一個工具來選擇性地合併依賴樹中包含的重複文件。在pom.xml中:
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
</transformer>
告訴Maven通過附加javax.ws.rs.ext.Providers的任何重複合併。
此外,通過設置Maven的樹蔭插件在我構建的package
階段來執行,然後Maven的組裝插件在install
階段來執行,我能創造一個可執行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>
這裏是我的-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>
+1爲察覺'javax.ws.rs.ext。 Providers'。 – tarka