Spring 启动 : can't create war with maven: Unable to find main class

标签 spring spring-boot maven spring-mvc war

我有一个带有 war 和 ear 模块的 SpringBoot 项目,我使用 maven(主要用于 Java 项目的构建自动化工具)和 IntelliJ IDEA 构建该项目。
这是我的 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) 类。 list 中的“Main-Class”实际上是由 Spring Boot plugin 的“布局”属性控制的。 .

    <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

The Executable War File Structure
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>

关于 Spring 启动 : can't create war with maven: Unable to find main class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61768002/

相关文章:

java - 为什么 LDAP 身份验证例程通常使用两个绑定(bind)?

java - Spring @Resource 注入(inject)返回 NullPointerException

java.lang.NoSuchMethodError : org. apache.xml.security.transforms.Transform.init()V

windows - gradle 为 windows 生成的 startScript 产生 "input line is too long"

maven - 在 Maven 中禁用特定插件的并行构建

java - 将 Spring bean 注入(inject)到使用 new() 创建的外部类中

date - application.yaml 和属性中的 Spring 启动日期处理

java - 无法找到 activemq.xml 的 Spring NamespaceHandler

java - 为什么 Sonar 只分析pom.xml?

maven - 禁用在 maven 中查找错误