java Spring Boot Maven 插件 - 没有 BOOT-INF 目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43124852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Maven Plugin - No BOOT-INF directory
提问by Damien
Between version 1.3.8.RELEASE of the spring-boot-maven-plugin and version 1.4.0.RELEASE - there has been a change in the generated package structure (if you extract the uber jar file) 1.3.8.RELEASE com, lib, META-INF and org directories 1.4.0.RELEASE has a BOOT-INF, META-INF and org directories Basically from 1.4.0.RELEASE onwards - all the classes and libs are in the BOOT-INF directory. Due to this - when you try to run a Spring Boot project on Amazon Lambda - it says that there is a jar not found as it cannot read the new Spring Boot Uber jar structure
在 spring-boot-maven-plugin 的 1.3.8.RELEASE 版本和 1.4.0.RELEASE 版本之间 - 生成的包结构发生了变化(如果您提取 uber jar 文件)1.3.8.RELEASE com, lib、META-INF 和 org 目录 1.4.0.RELEASE 有一个 BOOT-INF、META-INF 和 org 目录 基本上从 1.4.0.RELEASE 开始 - 所有的类和库都在 BOOT-INF 目录中。因此,当您尝试在 Amazon Lambda 上运行 Spring Boot 项目时,它说找不到 jar,因为它无法读取新的 Spring Boot Uber jar 结构
My question is - is it possible in the newer versions of the Spring Boot Maven Plugin to get it to generate the uber jar to be the same structure as in version 1.3.9.RELEASE?
我的问题是 - 是否有可能在较新版本的 Spring Boot Maven 插件中让它生成与 1.3.9.RELEASE 版本相同结构的 uber jar?
I tried the maven-shade-plugin - but that leads to other issues
我尝试了 maven-shade-plugin - 但这会导致其他问题
Any help is greatly appreciated
任何帮助是极大的赞赏
Thanks Damien
谢谢达米安
回答by Damien
The solution was to add the MODULE layout for the plugin in the pom.xml file
解决方案是在 pom.xml 文件中为插件添加 MODULE 布局
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>MODULE</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
回答by lauksas
In my case I'm using spring boot 2.X and I declared the spring-boot-maven-plugin
after the maven-dependency-plugin
(which I used to unpack and create exploded app in Docker) and it must be before the unpack, makes sense, it was unpacking before the spring boot maven plugin executed. Next time I'll declare it first thing in the plugin chain, lost more than 1 hour on this. Hope it helps someone.
在我的情况下,我使用的是 spring boot 2.X 并且我声明了spring-boot-maven-plugin
之后maven-dependency-plugin
(我曾经在 Docker 中解压并创建爆炸的应用程序)并且它必须在解包之前,这是有道理的,它是在 spring boot maven 之前解包的插件执行。下次我会在插件链中首先声明它,在这上浪费了 1 个多小时。希望它可以帮助某人。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${spring.boot.mainClass}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
回答by unawaz
The answer above with
上面的答案与
<layout>MODULE</layout>
does not work anymore, this is because layoutelement is deprecated in Spring Boot 2.x. I am using Spring Boot 2.0.x, I found this helpful comment on github:
不再工作,这是因为布局元素在 Spring Boot 2.x 中已弃用。我正在使用 Spring Boot 2.0.x,我在 github 上发现了这个有用的评论:
Support for the module layout was removed in Spring Boot 2.0 having been deprecated in 1.5. Unfortunately, the updates to the Maven Plugin's documentation were missed so we can use this issue to sort that out. You should use a custom LayoutFactoryinstead.
Spring Boot 2.0 中删除了对模块布局的支持,但在 1.5 中已弃用。不幸的是,Maven 插件文档的更新被遗漏了,所以我们可以利用这个问题来解决这个问题。您应该改用自定义 LayoutFactory。
But as I did not want to implement LayoutFactory I tried this second solution below that actually repackage and creates an extra jar with a classifier given name:
但由于我不想实现 LayoutFactory,我尝试了下面的第二个解决方案,实际上重新打包并创建了一个带有分类器名称的额外 jar:
This is due to the change in layout of executable jars in Spring Boot 1.4. Application classes are now packaging in BOOT-INF/classes. Your client module depends on the repackaged, fat jar of your web module. Due to the new layout that means that the client module can no longer load the web module's classes. If you want to use your web module as a dependency, you should configure Boot's repackaging to apply a classifier to the fat jar. For example:
这是由于 Spring Boot 1.4 中可执行 jar 的布局发生了变化。应用程序类现在打包在 BOOT-INF/classes 中。您的客户端模块取决于重新打包的 Web 模块的胖 jar。由于新布局,这意味着客户端模块无法再加载 Web 模块的类。如果您想使用您的 web 模块作为依赖项,您应该配置 Boot 的重新打包以将分类器应用于 fat jar。例如:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Doing so will allow other modules to depend on the original jar that does not embed the module's dependencies and has the classes at the root of the jar.
这样做将允许其他模块依赖于原始 jar,该 jar 未嵌入模块的依赖项并且在 jar 的根目录下具有类。
One original jar have the same structure as I wanted like
一个原始罐子的结构与我想要的一样
com.my-package.foo.bar
META-INF
and the second classifier have the newer structure with BOOT-INF/ etc.
第二个分类器具有更新的结构,带有 BOOT-INF/ 等。
回答by Eric Manley
For me, the solution was a bit more insidious....I had the spring-boot-maven-plugin nested under pluginManagement, (see below). Doh!
对我来说,解决方案有点阴险......我在 pluginManagement 下嵌套了 spring-boot-maven-plugin,(见下文)。呸!
The nasty thing, is that when I'd run mvn spring-boot:run, spring boot comes up just fine, and runs app! It wasn't until we tried to deploy to PCF (as a spring-boot JAR), that we'd get an error that there was something wrong with format of the binary....
令人讨厌的是,当我运行 mvn spring-boot:run 时,spring boot 运行良好,并运行 app!直到我们尝试部署到 PCF(作为 spring-boot JAR),我们才会收到一个错误,提示二进制格式有问题......
<build>
<!--
DON'T DO THIS!!
-->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<!--
DO THIS INSTEAD!!
-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Once I removed the pluginManagement tags from the POM, I would now get the ./BOOT-INF structure. Please keep in mind that pluginManagement is typically for a parent-pom structure, where you want that plugin's config used across other modules.
从 POM 中删除 pluginManagement 标记后,我现在将获得 ./BOOT-INF 结构。请记住,pluginManagement 通常用于父 pom 结构,您希望该插件的配置在其他模块中使用。
回答by Jorge Arias
I was using Gradle, instead of Maven, and this is what I had to do:
我使用的是 Gradle,而不是 Maven,这就是我必须做的:
1- In my build.gradle, I added the following properties as defined in https://spring.io/guides/gs/spring-boot-Docker/.
1- 在我的 build.gradle 中,我添加了https://spring.io/guides/gs/spring-boot-Docker/ 中定义的以下属性。
buildscript {
...
dependencies {
...
classpath('gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.13.0')
}
}
group = 'springio'
...
apply plugin: 'com.palantir.docker'
task unpack(type: Copy) {
dependsOn bootJar
from(zipTree(tasks.bootJar.outputs.files.singleFile))
into("build/dependency")
}
docker {
name "${project.group}/${bootJar.baseName}"
copySpec.from(tasks.unpack.outputs).into("dependency")
buildArgs(['DEPENDENCY': "dependency"])
}
2- My dependency folder was not being written to
2-我的依赖文件夹没有被写入
ARG DEPENDENCY=target/dependency
instead, I located it in another folder, so I changed this property in the Dockerfile:
相反,我将它放在另一个文件夹中,因此我在 Dockerfile 中更改了此属性:
ARG DEPENDENCY=build/dependency
With this I got a successful build.
有了这个,我得到了一个成功的构建。