![](/img/trans.png)
[英]Spring Boot Maven Plugin and requiresUnpack target directory
[英]Spring Boot JAR built with Maven using requiresUnpack not working
我想我一直遇到以下問題澤西島並不總是使用Spring Boot胖罐 。 解決方法應該是將POM
的Jersey依賴項設置為requiresUnpack
。
我的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時,仍然會出現以下錯誤:
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)
運行我在IDE中使用Artifacts構建的JAR工作得很好。
我的POM
或我用Maven構建JAR的方式有問題嗎?
這可能已經修復了。 我發布了Docker
與使用Spring Boot
, Jersey 2
和Docker
創建API相關的博客文章,並使用Swagger
記錄它們: http : //tech.asimio.net/2016/04/05/Microservices-using-Spring- Boot-Jersey-Swagger-and-Docker.html和http://tech.asimio.net/2016/05/07/Documenting-multiple-REST-API-versions-using-Spring-Boot-Jersey-and-Swagger。 html ,附帶源代碼,我不需要unpack
和repackage
Jersey 2
依賴項。
另一方面,當我使用Spring Cloud,Eureka,Ribbon和Feign為另一個關於服務注冊和發現的博客提供相應的源代碼時,我正在整合Spring Boot
和Jersey 1
( Jersey 1
沒有Spring Boot
我確實記得解決了為特定API服務創建多模塊Maven
項目所需的Jersey 1
依賴項的解決方法。
我發現了這個問題。
Spring Boot 1.4更改了內部Jar結構,以方便Spring Boot引導過程。
https://github.com/spring-projects/spring-boot/issues/1468#issuecomment-267357809
您可以保留此版本1.4.3:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
但是spring-boot-jersey fat-jar可以執行而沒有錯誤,它需要將插件版本降級到1.3.8,如下所示:
<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>
問題是澤西島無法掃描新的“胖靴子”中的類。 當您嘗試使用ResourceConfig
類的packages("some.package.to.scan")
方法時,會發生這種情況。
但是,您可以使用Spring類路徑掃描工具實現相同的效果。 這樣你就可以將包類似地掃描到config.packages():
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()));
注意:請查看org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener
的來源。 這是庫存解決方案,您可以看到它執行相同操作:它掃描使用@Path
或@Provider
注釋的類(但由於掃描機制損壞,無法找到任何內容)。
(使用舊版本的啟動插件也適用於我,但我試圖避免它。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.