spring boot java项目上的/BOOT-INF/classes(没有这样的文件或目录)
Posted
技术标签:
【中文标题】spring boot java项目上的/BOOT-INF/classes(没有这样的文件或目录)【英文标题】:/BOOT-INF/classes (No such file or directory) on spring boot java project 【发布时间】:2018-11-09 16:21:38 【问题描述】:在尝试制作一个可以干净独立地启动的独立 JAR 时,我遇到了涉及 Jersey 和我理想的胖 JAR 的问题。最终的 jar 将被移动到 Docker 映像中。
我得到的错误基本上是这样的:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.glassfish.jersey.server.ResourceConfig]: Factory method 'jerseyResourceConfig' threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /dir/myproject-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 48 common frames omitted
Caused by: org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /dir/myproject/target/myproject-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)
at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.create(JarZipSchemeResourceFinderFactory.java:89)
at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.create(JarZipSchemeResourceFinderFactory.java:65)
at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.addResourceFinder(PackageNamesScanner.java:282)
at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.init(PackageNamesScanner.java:198)
at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.<init>(PackageNamesScanner.java:154)
at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.<init>(PackageNamesScanner.java:110)
at org.glassfish.jersey.server.ResourceConfig.packages(ResourceConfig.java:680)
at org.glassfish.jersey.server.ResourceConfig.packages(ResourceConfig.java:660)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 49 common frames omitted
Caused by: java.io.FileNotFoundException: /dir/myproject/target/myproject-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)
这是我的 POM 文件中的构建配置文件:
<profile>
<id>d2</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<packaging.type>jar</packaging.type>
<log.dir>logs</log.dir>
<!-- updates bootstrap.properties -->
<config.override.path>./conf</config.override.path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<requiresUnpack>
<dependency>
<groupId>my.com</groupId>
<artifactId>myArtifact</artifactId>
</dependency>
</requiresUnpack>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
最后,这是我正在使用的所有球衣依赖项:
org.springframework.boot:spring-boot-starter-jersey:jar:1.5.12.RELEASE:compile
| | +- org.glassfish.jersey.core:jersey-server:jar:2.25.1:compile
| | | +- org.glassfish.jersey.core:jersey-client:jar:2.25.1:compile
| | | +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.25.1:compile
| | +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.25.1:compile
| | +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.25.1:compile
| | +- org.glassfish.jersey.ext:jersey-spring3:jar:2.25.1:compile
| | \- org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.25.1:compile
| | +- org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.25.1:compile
................................................................................
| | +- org.glassfish.jersey.connectors:jersey-apache-connector:jar:2.17:compile
................................................................................
| \- io.swagger:swagger-jersey2-jaxrs:jar:1.5.6:compile
| +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.25.1:compile
| | +- org.glassfish.hk2.external:javax.inject:jar:2.5.0-b32:compile
| | \- org.glassfish.jersey.core:jersey-common:jar:2.25.1:compile
| | +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.25.1:compile
| \- org.glassfish.jersey.media:jersey-media-multipart:jar:2.25.1:compile
我已阅读此SO question 引用的所有来源。我被引导解决了 Spring 和 Jersey 显然已修复的一些 github 问题,但我仍在处理这些复杂问题。 关于我的复杂性的独特之处在于,未找到的 boot-inf/类直接在我的 jar 中并引用了项目本身。它不是在抱怨不同的依赖 jar,而是在抱怨项目自己的 /BOOT-INF/classes。
上述 SO 问题中未提及的任何其他见解都将非常有用!我发现在 Docker 中使用 Java 比我经历过的任何其他堆栈(python-django 和 node-js)都更难使用。如果需要更多信息,请告诉我。
【问题讨论】:
我也愿意使用像spotify这样可以自动生成docker镜像的插件;但是,似乎存在相同的问题。 我在使用 ResourceConfig.package(...) 注册端点时遇到了类似的问题。 你有没有解决这个问题? 【参考方案1】:尝试添加以下插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
阅读更多关于https://maven.apache.org/plugins/maven-shade-plugin/
【讨论】:
以上是关于spring boot java项目上的/BOOT-INF/classes(没有这样的文件或目录)的主要内容,如果未能解决你的问题,请参考以下文章
Java攻城狮社区首发,简历上的Spring Boot项目首选
Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext
仅在 REST API 调用上的 Spring Boot 404