Java 如何在Springboot中创建可执行WAR或JAR

Java 如何在Springboot中创建可执行WAR或JAR,java,spring-boot,Java,Spring Boot,我在eclipse中编写了一个简单的HELLOWORLD Springboot/thymeleaf项目,我可以运行它。 然后我尝试创建一个可执行的war(或jar)文件,但这是不可能的。我需要的是一个可执行的Jar或war文件,在我的计算机上用作可执行(可运行)文件,而不使用eclipse 应用程序类别: package com.example.handlingformsubmission; import org.springframework.boot.SpringApplication

我在eclipse中编写了一个简单的HELLOWORLD Springboot/thymeleaf项目,我可以运行它。 然后我尝试创建一个可执行的war(或jar)文件,但这是不可能的。我需要的是一个可执行的Jar或war文件,在我的计算机上用作可执行(可运行)文件,而不使用eclipse

应用程序类别:

package com.example.handlingformsubmission;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HandlingFormSubmissionApplication {

    public static void main(String[] args) {
        SpringApplication.run(HandlingFormSubmissionApplication.class, args);
    }

}
控制器->

package com.example.handlingformsubmission;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
        return "greeting";
    }

    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
        model.addAttribute("greeting", greeting);
        return "result";
    }

}
Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>handling-form-submission</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>handling-form-submission</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4.0.0
org.springframework.boot
spring启动程序父级
2.3.2.2发布
com.example
处理表格提交
0.0.1-快照
处理表格提交
SpringBoot的演示项目
1.8
org.springframework.boot
弹簧启动装置
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测试
org.junit.vintage
朱尼特老式发动机
org.springframework.boot
springbootmaven插件
http文件:

<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head> 
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{content}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

入门:处理表单提交
形式
身份证:

信息:

我找到了解决方案:

1-在pom文件中添加此代码:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

org.springframework.boot
springbootmaven插件
2-然后将项目作为MAVEN构建运行

3-在目标字段中填写“包”


4-单击运行。

我建议从生成的项目开始。它将确保您已经配置了所有必要的位,以使用Web项目构建可运行的jar(只需选择“Web”和“Thymeleaf”)。(如果这就是您在这里所做的,那么您还没有显示一个主类。)我忘记添加应用程序类。我就这么做了。我可以在eclipse中运行该程序。我需要将它导出为一个可执行的jar或war。在这种情况下,普通的
mvn包应该可以满足您的需要。请详细解释这“不可能”的原因。请检查图片。我右键单击项目并选择导出。然后我尝试了两种选择:作为WAR文件或作为可运行的jar。但是对于第一个,我需要一个web项目文件,对于jar,我需要一个配置文件,我不知道它们是什么。请转到pom.xml文件所在的文件夹(不在eclipse中,请转到文件资源管理器)并运行mvn包。参考:你的问题说你的POM中有这个。