使用requiresUnpack无法正常使用Maven构建的Spring Boot JAR

时间:2017-01-13 12:58:15

标签: java spring maven spring-boot spring-boot-maven-plugin

我想我一直遇到以下问题Jersey doesn't always work with Spring Boot fat jars。解决方法应该是将POM中的Jersey dependencies设置为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的方式有问题吗?

3 个答案:

答案 0 :(得分:2)

这可能已经修复过了。我发布了一些与使用Spring BootJersey 2Docker创建API相关的博客文章,并使用Swaggerhttp://tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger-and-Docker.html和{{{ 3}},两者都附带源代码,我不需要unpackrepackage Jersey 2依赖项。

另一方面,当我正在处理有关http://tech.asimio.net/2016/05/07/Documenting-multiple-REST-API-versions-using-Spring-Boot-Jersey-and-Swagger.html的其他博客的随附源代码时,我正在整合Spring BootJersey 1Jersey 1没有有一个Spring Boot启动程序,我记得解决了为特定API服务创建多模块Jersey 1项目所需的解包Maven依赖项。

答案 1 :(得分: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>

答案 2 :(得分:1)

问题是泽西岛无法扫描新的“胖靴子”中的类。当您尝试使用packages("some.package.to.scan")类的ResourceConfig方法时会发生这种情况。

但是,使用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注释的类(但由于扫描机制损坏,无法找到任何内容)。 / p>

(使用旧版本的启动插件也适用于我,但我试图避免使用它。)