如何为tomcat配置maven-war-plugin

我无法为 Tomcat 生成正确的 WAR 文件。

我正在使用 MAVEN 3.6.1、Java 12.0.1 和 IDE Eclipse。我的应用程序在 eclipse 中运行时运行良好(运行方式 > Spring Boot 应用程序),但问题是当我在生成 WAR 文件后尝试运行它时。

我正在做 java -jar .war 并且我得到:

Error: Could not find or load main class com.blw.linemanager.Application
Caused by: java.lang.ClassNotFoundException: com.blw.linemanager.Application

我正在谷歌搜索和阅读 stackoverflow,因为我发现了很多关于它的帖子,但仍然无法运行它。我做错了什么?

阅读一些内容后,我发现我有一些如何配置 maven-war-plugin(我说得对吗?)并且在 pom 中我做了一些更改,但它没有帮助。

也因为一开始我收到错误“没有主要清单属性”我添加它现在它看起来像这样


Manifest-Version: 1.0

Created-By: Apache Maven ${maven.version}

Build-Jdk: ${java.version}

我的思维方式是错误的吗?我应该能够将 .war 文件作为 java -jar .war 运行还是这是误解?


@SpringBootApplication

public class Application extends org.springframework.boot.web.support.SpringBootServletInitializer {

    public static void main(String [] args) {

        SpringApplication app = new SpringApplication(Application.class);

        app.run(args);

    }


    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

        return builder.sources(Application.class);

    }


}


白衣染霜花
浏览 117回答 2
2回答

慕哥6287543

最好从这里获取一个可用的 spring boot 应用程序,以避免版本冲突等。对于war-file 部署(例如到本地 tomcat),您的应用程序必须扩展 SpringBootServletInitializer:&nbsp; &nbsp; @SpringBootApplication&nbsp; &nbsp; public class SpringBootTomcatApplication extends SpringBootServletInitializer {&nbsp; &nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(SpringBootTomcatApplication.class, args);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {&nbsp; &nbsp; &nbsp; &nbsp; return builder.sources(SpringBootTomcatApplication.class);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }改变你pom的 packagingto war。<packaging>war</packaging>要生成war-file,您只需要spring-boot-maven-plugin:<build>&nbsp; <plugins>&nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; </plugin>&nbsp; </plugins></build>是的,你不需要maven-war-plugin。仅出于测试目的,让我们注释我们的应用程序@RestController并引入一个简单的端点:&nbsp; &nbsp; @RestController&nbsp; &nbsp; @SpringBootApplication&nbsp; &nbsp; public class SpringBootTomcatApplication extends SpringBootServletInitializer {&nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; @RequestMapping(value = "/")&nbsp; &nbsp; &nbsp; public String hello() {&nbsp; &nbsp; &nbsp; &nbsp; return "Hello World from Tomcat";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }在Local terminal(在 eclipse Ctrl+Alt+T 中)只需输入而不是从文件夹mvn package复制生成的文件,将其粘贴到本地 Tomcat 的文件夹下,请求http://localhost:8080/ {your-application-name} 然后你应该看到消息。现在您可以添加所需的依赖项并继续编码。 wartargetwebappsHello World from Tomcat

慕尼黑5688855

在 pom.xml 中添加带有版本详细信息的包装类型,例如<packaging>jar</packaging>要么<packaging>war</packaging>然后,附加构建标签来定义 jar / war 的名称,例如,<build>&nbsp; &nbsp; <finalName>YOUR-APP_NAME</finalName>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins></build>然后,以目标运行您的应用程序清洁包装在目标文件夹中创建 jar/war。将您的 war 文件重命名为 ROOT,如 ROOT.war最后一步将您创建的 jar/war 复制到 tomcat webapps 文件夹中并启动 tomcat。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java