Java 在 Eclipse 中将 Spring Boot 应用程序导出为 JAR 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37016040/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:55:50  来源:igfitidea点击:

Exporting Spring Boot application as JAR file in eclipse

javaeclipsespringrestmaven

提问by Viggo Lundén

I'm using the Spring STS in Eclipse to create a simple web-based spring boot project. I can run it fine in Eclipse, but when I try to export it as a JAR file I get:

我在 Eclipse 中使用 Spring STS 创建一个简单的基于 Web 的 Spring Boot 项目。我可以在 Eclipse 中正常运行它,但是当我尝试将它导出为 JAR 文件时,我得到:

rg.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

My public static void mainis located in Application.java, with the @SpringBootApplicationannotation.

Mypublic static void main位于Application.java,带有@SpringBootApplication注释。

I've double checked all the Maven dependencies a hundred times.

我已经仔细检查了所有 Maven 依赖项一百次。

What am I doing wrong?

我究竟做错了什么?

采纳答案by chrylis -cautiouslyoptimistic-

Most likely, you're using the built-in Eclipse exporterto generate your jar, which only includes the target files actually produced in that project. In order to have a "fat" (standalone executable) jar, you need to use the Spring Boot Maven or Gradle plugin to "repackage" the jar.

最有可能的是,您正在使用内置的 Eclipse 导出器来生成您的 jar,它只包含该项目中实际生成的目标文件。为了有一个“fat”(独立的可执行文件)jar,你需要使用 Spring Boot Maven 或 Gradle 插件来“重新打包”jar。

First, make sure that you have the repackagegoal included in your build setup, then use the Maven packagetarget. The simplest way to do this is to run mvn packagefrom the command line (you may need to install the Maven CLI package for your OS); you can also right-click the POM in Eclipse and "Run As" to execute specific Maven operations from within Eclipse.

首先,请确保您有repackage包含在构建设定目标,然后使用Mavenpackage目标。最简单的方法是从命令行运行mvn package(您可能需要为您的操作系统安装 Maven CLI 包);您还可以在 Eclipse 中右键单击 POM 并“运行为”以从 Eclipse 中执行特定的 Maven 操作。

回答by vimal krishna

It is a single line command, on window 7/10 machine, with command prompt to your project folder (Inside your project workspace). I do not do with Eclipse IDE POM maven goals, but you can do with maven goal there also. ON window machine I prefer cmd.exe for exporting and running.

这是一个单行命令,在 7/10 窗口机器上,带有指向您的项目文件夹的命令提示符(在您的项目工作区中)。我不使用 Eclipse IDE POM maven 目标,但您也可以使用 Maven 目标。在窗口机器上,我更喜欢 cmd.exe 用于导出和运行。

mvnw clean package

on unix kernel based

基于unix内核

./mvnw clean package

You have to go inside workspace and than to the project root folder. You will see a maven wrapper mvnw, with that you don't need to have maven installed and .mvn folder at the same level provides necessary jar for that. For a project

您必须进入工作区而不是项目根文件夹。您将看到一个 maven 包装器 mvnw,您不需要安装 maven,同一级别的 .mvn 文件夹为此提供了必要的 jar。对于一个项目

D:\workspace\Zuteller Workspace\zusteller>mvnw clean package

it will create zusteller-0.0.1-SNAPSHOT.jarin the targetfolder at the same level.

它将在同一级别zusteller-0.0.1-SNAPSHOT.jar目标文件夹中创建。

D:\workspace\Zuteller Workspace\zusteller>java -jar target\zusteller-0.0.1-SNAPSHOT.jar

You can run self-contained application(embedded Tomcat) and access at localhost:8080/your project

您可以运行自包含应用程序(嵌入式 Tomcat)并访问 localhost:8080/您的项目