2012-03-20 67 views
8

我想用澤西包裹灰熊作爲使用Maven樹蔭插件的單個罐子。但是,我總是得到消息No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler灰熊和澤西島獨立瓶子

的代碼工作正常在Eclipse中,但不是在一個包裝罐子:

public class Main { 

private static URI getBaseURI() { 
    return UriBuilder.fromUri("http://localhost/").port(9998).build(); 
} 

public static final URI BASE_URI = getBaseURI(); 

protected static HttpServer startServer() throws IOException { 
    System.out.println("Starting grizzly..."); 
    ResourceConfig rc = new PackagesResourceConfig("share.test"); 
    rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); 
    return GrizzlyServerFactory.createHttpServer(BASE_URI, rc); 
} 

public static void main(String[] args) throws IOException { 
    HttpServer httpServer = startServer(); 
    System.in.read(); 
    httpServer.stop(); 
}  
} 

下面是完整的例外

$ java -jar target/webServiceTest-0.0.1-SNAPSHOT.jar 
Starting grizzly... 
Mar 20, 2012 12:48:53 PM com.sun.jersey.api.core.PackagesResourceConfig init 
INFO: Scanning for root resource and provider classes in the packages: 
    share.test 
Mar 20, 2012 12:48:54 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses 
INFO: Root resource classes found: 
    class share.test.NonJAXBBeanResource 
    class share.test.Hello 
Mar 20, 2012 12:48:54 PM com.sun.jersey.api.core.ScanningResourceConfig init 
INFO: No provider classes found. 
Exception in thread "main" java.lang.IllegalArgumentException: No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler 
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:196) 
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:134) 
    at com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory.createHttpServer(GrizzlyServerFactory.java:242) 
    at share.test.Main.startServer(Main.java:27) 
    at share.test.Main.main(Main.java:31) 

我建立的jar包使用maven

<build> 
<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-shade-plugin</artifactId> 
    <version>1.5</version> 
    <configuration> 
    <transformers> 
    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
    <manifestEntries> 
     <Main-Class>share.test.Main</Main-Class> 
     <Build-Number>1</Build-Number> 
    </manifestEntries> 
    </transformer> 
    </transformers> 
</configuration> 
<executions> 
<execution> 
    <phase>package</phase> 
    <goals> 
    <goal>shade</goal> 
    </goals> 
</execution> 
</executions> 
</plugin> 
</plugins> 
</build> 

是否需要更換燈罩包含其他內容?

回答

8

錯誤看起來像插件沒有正確合併來自不同jar的META-INF/services記錄 - 如果在多個jar的META-INF/services目錄中有多個具有相同名稱的文件,它們需要合併,而不是由另一個取代。檢查是否是這種情況。

+3

補充說: 「用於合併META-INF /服務資源」 的ServicesResourceTransformer沒有解決我的問題,謝謝馬丁。只需要在pom.xml中添加「'到轉換器參考:[link](http://maven.apache.org /plugins/maven-shade-plugin/examples/resource-transformers.html#ServicesResourceTransformer) – 2012-03-22 11:17:06

0

我只是犯了一個愚蠢的錯誤。在pom中配置maven-assembly-plugin。

大會似乎取代META-INF /服務,並在文件越權 「com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainerProvider 」 com.sun.jersey.spi.container.ContainerProvider

作爲指南http://maven.apache.org/plugins/maven-assembly-plugin/提到,如果您的項目想要將工件封裝在超級罐中,彙編插件僅提供基本支持。要獲得更多控制權,請使用Maven Shade插件。

<!-- mvn assembly:assembly --> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <mainClass>XXX.DaemonMain</mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
     </plugin> 
1

以下鏈接幫我搞清楚下面的解決方案:

尤其答案

而是使用jar-具有依賴性爲您彙編插件配置的descriptorRef的創建我們自己的例如在src/assembly/depmerge.xml中(見下文)。此程序集配置將添加一個關注META-INF /服務的containerDescriptorHandler。

運行

mvn clean compile assembly:single 

以獲得目標jar文件,你可以用

java -jar target/x.y.-version-jar-with-dependencies.jar 

POM調用。XML:

 <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.5.3</version> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>${mainClass}</mainClass> 
        </manifest> 
       </archive> 
      <descriptor>src/assembly/depmerge.xml</descriptor> 
      </configuration> 
     </plugin> 

的src /組裝/ depmerge.xml:

<!-- 
see http://maven.apache.org/guides/mini/guide-assemblies.html 
see http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html 
--> 
<assembly 
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> 
    <!-- TODO: a jarjar format would be better --> 
    <id>jar-with-dependencies-and-services</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
     <dependencySet> 
      <outputDirectory>/</outputDirectory> 
      <useProjectArtifact>true</useProjectArtifact> 
      <unpack>true</unpack> 
      <scope>runtime</scope> 
     </dependencySet> 
    </dependencySets> 
    <!-- 
     https://stackoverflow.com/questions/1607220/how-can-i-merge-resource-files-in-a-maven-assembly 
    --> 
    <containerDescriptorHandlers> 
     <containerDescriptorHandler> 
      <handlerName>metaInf-services</handlerName> 
     </containerDescriptorHandler> 
    </containerDescriptorHandlers> 
</assembly>