无法导入类,IntelliJ 显示 BOOT-INF 前缀,似乎相关
这是与 Java 和 Maven 相关的 - 我正在尝试从一个项目中导入一些类,这些类可以在我的机器上构建到本地 mvn 存储库,或者我可以从公司的外部 mvn 存储库下载它,它已经是一个打包的 jar.在查看外部库"并扩展相关库时,我在左侧项目"窗格中查看 IntelliJ 时确实注意到,相关 jar 下的所有类都有一个BOOT-INF.classes"前缀.如果有帮助,它也是一个 springboot 项目,尽管我可以从外部存储库导入所有 springboot 类和所有其他类.
(外部库"下左窗格中的 IntelliJ 项目视图内部)
Maven:org.springframework.boot:spring-boot-starter-test:2.0.0.RELEASE
Maven:com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.8.1
---jackson-core-2.8.1.jar
------com.faster.xml.jackson.core
------com.faster.xml.jackson.core.async
........(列出更多包)
Maven:com.mycompany.my.project:component-two-1.0.0-SNAPSHOT
Maven:com.mycompany.my.project:component-three-1.0.0-SNAPSHOT
---com.mycompany.my.project:component-1.0.0-20181201.jar
------BOOT-INF.classes
------BOOT-INF.classes.com.mycompany.project.my.package.one
---------MyClassOne
---------MyClassTwo
------BOOT-INF.classes.com.mycompany.project.my.package.one
------BOOT-INF.classes.com.mycompany.project.my.package.one.alpha
------BOOT-INF.classes.com.mycompany.project.my.package.one.bravo
解决方案听起来您正在尝试使用 Spring Boot 应用程序作为依赖项.一般来说,不建议这样做,因为像 war 文件一样,Spring Boot 应用程序不打算用作依赖项.
Spring Boot 文档 说明如下:
<块引用>如果您的应用程序包含您想与其他项目共享的类,推荐的方法是将该代码移动到一个单独的模块中.然后,您的应用程序和其他项目可以依赖单独的模块.
如果这不是一个选项,那么您需要配置您的项目以构建应用程序 jar 和一个适合用作依赖项的 jar.从文档的同一部分:
<块引用>如果您无法按照上述建议重新排列代码,则必须将 Spring Boot 的 Maven 和 Gradle 插件配置为生成适合用作依赖项的单独工件.可执行存档不能用作依赖项,因为可执行 jar 格式将应用程序类打包在 BOOT-INF/classes 中.这意味着当可执行 jar 用作依赖项时,它们将无法找到.
<块引用>
要生成两个工件,一个可以用作依赖项,另一个是可执行的,必须指定一个分类器.此分类器应用于可执行存档的名称,将默认存档用作依赖项.
您使用的是 Maven,因此适当的配置如下所示:
<build><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><配置><分类器>执行器</分类器></配置></插件></插件></构建>
如果您使用的是 Gradle,则适当的配置应如下所示:
jar {启用 = 真}引导罐{分类器 = '执行'}
使用任一构建系统,您的应用程序的可执行 fat jar 现在将使用 exec
分类器发布.可以用作依赖的普通jar将是未分类的.
This is with Java and Maven - I am trying to import some classes from a project that I could either build on my machine to the local mvn repository or I can download it from company's external mvn repository already a packaged jar. I did notice when looking on IntelliJ at the left "project" pane when looking at "External Libraries" and expanding the library in question that there is a "BOOT-INF.classes" prefix to all the classes underneath the jar in question. It's also a springboot project if that helps, although I'm able to import all the springboot classes and all the other classes from external repository just fine.
(Inside of IntelliJ Project View in Left Pane under "External Libraries")
Maven: org.springframework.boot:spring-boot-starter-test:2.0.0.RELEASE
Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.8.1
---jackson-core-2.8.1.jar
------com.faster.xml.jackson.core
------com.faster.xml.jackson.core.async
........(More packages listed)
Maven: com.mycompany.my.project:component-two-1.0.0-SNAPSHOT
Maven: com.mycompany.my.project:component-three-1.0.0-SNAPSHOT
---com.mycompany.my.project:component-1.0.0-20181201.jar
------BOOT-INF.classes
------BOOT-INF.classes.com.mycompany.project.my.package.one
---------MyClassOne
---------MyClassTwo
------BOOT-INF.classes.com.mycompany.project.my.package.one
------BOOT-INF.classes.com.mycompany.project.my.package.one.alpha
------BOOT-INF.classes.com.mycompany.project.my.package.one.bravo
解决方案It sounds like you are trying to use a Spring Boot application as a dependency. Generally speaking this isn’t recommended as, like a war file, a Spring Boot application is not intended to be used as a dependency.
The Spring Boot documentation says the following:
If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.
If that’s not an option then you’ll need to configure your project to build both the application jar and one that is suitable for use as a dependency. From the same section of the documentation:
If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.
You’re using Maven so the appropriate configuration would look something like this:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
If you were using Gradle, the appropriate configuration would look something like this:
jar {
enabled = true
}
bootJar {
classifier = 'exec'
}
With either build system, your application’s executable fat jar will now be published with an exec
classifier. The normal jar that can be used as a dependency will be unclassified.
相关文章