2017-01-13 8 views
4

私は次の問題Jersey doesn't always work with Spring Boot fat jarsを実行していると思います。回避策は、POMJersey dependenciesrequiresUnpackに設定することです。spring Boot JARがrequiresUnpackを使用してMavenで構築されていません

マイPOMは次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<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> 

    <groupId>net.hagstrom</groupId> 
    <artifactId>demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>demo</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.3.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-jersey</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
       <configuration> 
        <requiresUnpack> 
         <dependency> 
          <groupId>org.glassfish.jersey.containers</groupId> 
          <artifactId>jersey-container-servlet</artifactId> 
         </dependency> 
         <dependency> 
          <groupId>org.glassfish.jersey.core</groupId> 
          <artifactId>jersey-client</artifactId> 
         </dependency> 
        </requiresUnpack> 
       </configuration> 
       <version>1.4.3.RELEASE</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>repackage</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

しかし、私は、私はmvn packageで構築されたJARを実行しようとすると、私はまだ次のエラーを取得する:私は、構築されたJARを実行

2017-01-13 10:44:28.229 ERROR 9289 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jerseyConfig' defined in URL [jar:file:/home/mikael/Dev/Java/Java%20Programs/springBootDemo/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/net/hagstrom/JerseyConfig.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.hagstrom.JerseyConfig]: Constructor threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/mikael/Dev/Java/Java Programs/springBootDemo/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory) 

ArtifactsのIDEでうまく動作します。

私のPOMに何か問題がありますか、またはMavenでJARを構築する方法はありますか?

+0

私は除いて(同じPOMの構成で、スプリングブーツの同じバージョンを持っている:requiresUnpackが必要とされていません)。私のために/ BOOT-INF/dirはjarファイルで作成されるのではなく、動作します。最初に作成した.jarファイルを確認して、gzipでファイルを開き、構造を表示できますか? – sytolk

答えて

2

これはすでに修正されている可能性があります。付属のソースコードを、私はunpackrepackageJersey 2依存関係を必要としなかったの両方、http://tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger-and-Docker.htmlhttp://tech.asimio.net/2016/05/07/Documenting-multiple-REST-API-versions-using-Spring-Boot-Jersey-and-Swagger.html:私はSpring BootJersey 2Dockerを使用してAPIを作成し、で入手可能なSwaggerを使用してそれらを文書に関連するブログ記事のカップルを公開しています。私は別のブログについてServices Registration and Discovery using Spring Cloud, Eureka, Ribbon and Feignのための添付のソースコードに取り組んでいたとき、一方

は、私が Spring Bootスターターを持っていない Spring BootJersey 1Jersey 1を統合した私は Jersey 1依存関係開梱-回避するために思い出すんその特定のAPIサービスのためのマルチモジュール Mavenプロジェクトを作成する必要がありました。

+0

これらの例ではどのバージョンのSpring Bootを使用していますか? – g3blv

+0

@ g3blv 'Jersey' 2に関するチュートリアルでは、Springブート1.3.5.RELEASEと1.3.6.RELEASEを使用しています。 – ootero

1

が、私は問題を発見した。

春ブーツ1.4はを容易にするために、内部ジャー構造を変更しましたスプリングブートブートストラッププロセス。

https://github.com/spring-projects/spring-boot/issues/1468#issuecomment-267357809

あなたは、このバージョン1.4.3のままにすることができます:それ `sはにプラグインのバージョンをダウングレードする必要がエラーなしで実行可能に

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.3.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

をしかし、スプリング・ブート・ジャージ脂肪ジャー1.3.8 like this:

 <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <version>1.3.8.RELEASE</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>repackage</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
+1

私が理解していることは、Spring Boot 1.4での問題の回避策は、下記(https://github.com/spring-projects/spring-boot/issues/1468#issuecomment-270565789)のように 'requiresUnpack'を使用することですあなたが参照しているコメント。 – g3blv

+0

'requiresUnpack'は次のような異なる種類の例外に対して修正されています:https://github.com/spring-projects/spring-boot/issues/1345 - > lib/jersey-client-1.11.jar(そのようなファイルやディレクトリはありません)。しかし、/ BOOT-INF/classes(そのようなファイルやディレクトリはありません)があります。これは、spring-boot-maven-plugin(1.3.8へのダウングレード)から修正することができます。あなたと同じ例外があります。例えば、プラグインのバージョン1.3.8が使用されています。この問題は私にとっては問題です。 – sytolk

0

問題は、Jerseyが新しい "fat boot jar"でクラスをスキャンできないことです。これは、ResourceConfigクラスのpackages("some.package.to.scan")メソッドを使用しようとすると発生します。

しかし、Springクラスパススキャン機能を使用しても同じ効果を得ることができます。

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); 
scanner.addIncludeFilter(new AnnotationTypeFilter(Provider.class)); 
scanner.addIncludeFilter(new AnnotationTypeFilter(Path.class)); 
config.registerClasses(scanner.findCandidateComponents("your.package.to.scan").stream() 
      .map(beanDefinition -> ClassUtils.resolveClassName(beanDefinition.getBeanClassName(), config.getClassLoader())) 
      .collect(Collectors.toSet())); 

注:この方法は、あなたはconfig.packages()へと似たパッケージをスキャンすることができorg.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListenerのソースを見てください。これはストックソリューションであり、同じことがわかります。@Pathまたは@Providerで注釈が付けられたクラスをスキャンします(ただし、スキャンメカニズムが壊れているため何も検索できません)。

(ブートプラグインの古いバージョンを使用してはあまりにも私のために働いたが、私はそれを避けるために試してみました。)

関連する問題