SpringBoot:无法与maven创建战争。无法找到主类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot:无法与maven创建战争。无法找到主类相关的知识,希望对你有一定的参考价值。

我有一个带有war和ear模块的SpringBoot项目,我使用maven(一个主要用于Java项目的自动化工具)和IntelliJ IDEAThis是我的war项目的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.bendiciones</groupId>
        <artifactId>bendiciones-parent</artifactId>
        <version>4.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>bendiciones-war</artifactId>
    <name>bendiciones war</name>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.bendicionesbendiciones</groupId>
            <artifactId>bendiciones</artifactId>
            <version>${project.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <finalName>bendiciones</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.bendiciones.Application</mainClass>
            </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <scm>
        <tag>bendiciones-parent-4.0.0-SNAPSHOT</tag>
    </scm>
</project>

但是当我建立这个类时,我遇到了这个错误。

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage (repackage) on project bendiciones-war: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to find main class -> [Help 1]
 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage (repackage) on project bendiciones-war: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to find main class
     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)

即使这个类存在于项目中

答案

添加你的main(starterApplication)类。清单中的 "Main-Class "实际上是由 "layout "属性控制的。Spring Boot插件.

    <build>
       <plugins>
        ...
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>             
                <configuration>    
                     <mainClass>com.your.starter.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

而且你需要检查 META-INF/MANIFEST.MF.

Main-Class: org.springframework.boot.loader.WarLauncher
Start-Class: com.your.starter.Application

可执行战争文件结构

example.war
 |
 +-META-INF
 |  +-MANIFEST.MF
 +-org
 |  +-springframework
 |     +-boot
 |        +-loader
 |           +-<spring boot loader classes>
 +-WEB-INF
    +-classes
    |  +-com
    |     +-mycompany
    |        +-project
    |           +-YourClasses.class
    +-lib
    |  +-dependency1.jar
    |  +-dependency2.jar
    +-lib-provided
       +-servlet-api.jar
       +-dependency3.jar

或者使用父子关系构建。

 <build>
    <pluginManagement>
        <plugins>
            ...
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
另一答案

您正在用WAR打包您的应用程序,这表明您的应用程序是Web应用程序。因此,请在 pom.xml 中检查或添加以下重要的 Spring Boot 依赖关系。

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

以上是关于SpringBoot:无法与maven创建战争。无法找到主类的主要内容,如果未能解决你的问题,请参考以下文章

使用 spring boot 和 spring-boot-maven-plugin 生成战争时排除 application.properties

Maven与warpath的瘦身战争

如何使用 maven 中的 tomcat 插件部署多个战争?

在 Maven 中,如何在版本中命名战争文件与在快照构建中不同

如何在使用 maven 构建的战争中包含系统依赖项

从 Spring Boot 项目部署战争