【问题标题】:"Unable to find main class" with Maven on spring-boot project in Eclipse在 Eclipse 的 spring-boot 项目中使用 Maven “无法找到主类”
【发布时间】:2017-08-13 17:40:51
【问题描述】:

我非常绝望,因为我不再能够编译我的 Maven 项目。我有一个逻辑项目分为 7 个 maven 模块,到目前为止编译得很好,但是今天我介绍了一个功能,我不小心生成了一个循环的 maven 依赖项。因此我重构了代码并消除了循环依赖。自从我更新了 pom 文件以反映修复后,我的项目不再构建。当我使用“-X clean install”运行 maven 时,我得到以下 java.lang.IllegalStateException: Unable to find main class

[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] agiletunes-parent
[INFO] agiletunes-util
[INFO] agiletunes-security-common
[INFO] agiletunes-marketing-persistence
[INFO] agiletunes-common-ui
[INFO] agiletunes-security-client
[INFO] agiletunes-marketing-application
[INFO] agiletunes-security-server
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building agiletunes-parent 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ agiletunes-parent ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ agiletunes-parent ---
[INFO] Installing C:\Users\Alexander\Documents\Development\agileTunes\agiletunes-parent\pom.xml to C:\Users\Alexander\Documents\Development\mavenArtifacts\com\agiletunes\agiletunes-parent\0.0.1\agiletunes-parent-0.0.1.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building agiletunes-util 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ agiletunes-util ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Alexander\Documents\Development\agileTunes\agiletunes-util\src\main\resources
[INFO] skip non existing resourceDirectory C:\Users\Alexander\Documents\Development\agileTunes\agiletunes-util\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ agiletunes-util ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ agiletunes-util ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ agiletunes-util ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ agiletunes-util ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ agiletunes-util ---
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ agiletunes-util ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] agiletunes-parent .................................. SUCCESS [  1.492 s]
[INFO] agiletunes-util .................................... FAILURE [  1.095 s]
[INFO] agiletunes-security-common ......................... SKIPPED
[INFO] agiletunes-marketing-persistence ................... SKIPPED
[INFO] agiletunes-common-ui ............................... SKIPPED
[INFO] agiletunes-security-client ......................... SKIPPED
[INFO] agiletunes-marketing-application ................... SKIPPED
[INFO] agiletunes-security-server ......................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.674 s
[INFO] Finished at: 2017-03-21T21:53:04+01:00
[INFO] Final Memory: 31M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project agiletunes-util: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.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:1.5.2.RELEASE:repackage (default) on project agiletunes-util: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find main class
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find main class
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 20 more
Caused by: java.lang.IllegalStateException: Unable to find main class
    at org.springframework.boot.loader.tools.Repackager.buildManifest(Repackager.java:354)
    at org.springframework.boot.loader.tools.Repackager.repackage(Repackager.java:273)
    at org.springframework.boot.loader.tools.Repackager.repackage(Repackager.java:258)
    at org.springframework.boot.loader.tools.Repackager.repackage(Repackager.java:190)
    at org.springframework.boot.maven.RepackageMojo.repackage(RepackageMojo.java:212)
    at org.springframework.boot.maven.RepackageMojo.execute(RepackageMojo.java:199)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    ... 21 more
[ERROR] 
...

这是我的 maven 父项目的 pom.xml:

<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>

<groupId>com.agiletunes</groupId>
<artifactId>agiletunes-parent</artifactId>
<version>0.0.1</version>

<packaging>pom</packaging>
<name>agiletunes-parent</name>
<description>Maven parent project of agiletunes</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath></relativePath>
</parent>

<modules>
    <module>../agiletunes-common-ui</module>
    <module>../agiletunes-marketing-application</module>
    <module>../agiletunes-marketing-persistence</module>
    <module>../agiletunes-security-client</module>
    <module>../agiletunes-security-common</module>
    <module>../agiletunes-security-server</module>
    <module>../agiletunes-util</module>
</modules>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- Java -->
    <java.version>1.8</java.version>

    <!-- Spring -->
    <spring.boot.version>1.5.2.RELEASE</spring.boot.version>

    <!-- persistence -->
    <mysql-connector-java.version>6.0.5</mysql-connector-java.version>

    <!-- util -->
    <guava.version>17.0</guava.version>

    <!-- testing -->
    <mockito.version>1.9.5</mockito.version>

</properties>

<dependencies>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-java.version}</version><!--$NO-MVN-MAN-VER$ -->
        <scope>runtime</scope>
    </dependency>

    <!-- utils -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>

    <!-- unit test framework -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>${mockito.version}</version>
        <scope>test</scope>
    </dependency>

</dependencies>

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

我需要做什么来解决这个问题?如果您需要更多信息,请告诉我。提前谢谢你。

【问题讨论】:

  • 是什么让你认为是分支的问题?我可以回去,因为我有一个 git 版本控制。但是不知道是不是eclipse版本的问题?
  • 亲爱的 asettouf,感谢您的反馈。从 maven 父级的角度来看,我有许多 main 方法,因为多个(但不是所有)模块都有一个 main,因为它们可以在另一个 maven 项目中使用或独立使用。我认为这对 Maven 来说是可行的,到目前为止这从来都不是问题。 util 模块是内置站点,实际上没有主模块。所以,恐怕你指出的另一篇文章并没有解决这个问题,因为据我了解,它只需要整个项目中的一个主要方法。
  • 亲爱的 Ortomala Lokni。我犹豫要不要重置分支,但你是对的。重置后一切正常。非常感谢!

标签: eclipse maven spring-boot


【解决方案1】:

你应该有这个

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

仅在您要运行的那些模块中,而不是在父 pom 中。

【讨论】:

  • 介意解释为什么它不从父 pom 继承而必须完全包含在子 pom 中?
  • 因为在多模块项目中,并不是所有的子模块都应该是可执行的。
  • 谢谢你!所以这个插件定义了哪个模块是可执行的,对吧?
  • 我做了这个,仍然有同样的问题
【解决方案2】:

仅适用于面临类似问题的人。我在 pluginManagement 部分的父 POM 中添加了 spring-boot-maven-plugin,如下所示:

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

然后我只在使用它的模块中添加了插件(在plugin部分)。之后项目已成功构建。

【讨论】:

    【解决方案3】:

    您可以尝试在 pom 的属性部分添加它

    <start-class>your.package.main</start-class>
    

    【讨论】:

    • 哦,不,它帮助了我,但是在部署它之后,应用程序崩溃,因为“线程中的异常”main“java.lang.ClassNotFoundException: my.package.main”:(
    • 如何判断 jar 是否不是可执行文件,而是简单的库 jar 或依赖项 jar。我在 2.1.3.RELEASE 上遇到 main class not found 错误,但在 2.0.8 上没有出现
    • 在以前的版本中,start-class 不是必需的 :(
    【解决方案4】:

    我遇到了同样的问题。它最终成为一个实际上没有运行的子项目(没有主类)上的 spring boot mvn 插件声明。我刚刚从子模块中删除了 spring boot mvn 插件,一切都运行良好

    【讨论】:

    • 简单而好的答案。它对我有用,就像没有运行库项目一样。
    • 他的意思是从父 POM 中删除 org.springframework.bootspring-boot-maven-plugin将其添加到可运行模块的 POM 中。谢谢人
    【解决方案5】:

    如果springboot项目中没有主类,可以follow执行spring-boot-maven-plugin

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

    【讨论】:

    • 当您在不可执行的 spring-boot 应用程序中没有主类时,这是一个很好的答案。谢谢。
    【解决方案6】:

    像这样向 Maven 属性添加属性

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <start-class>org.roshan.Application</start-class>
    </properties>
    

    【讨论】:

    • 如果我没有主课怎么办。只是一个图书馆罐子。
    【解决方案7】:

    在项目agiletunes-util pom.xml 中添加

    <properties>
       <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
    </properties>
    

    【讨论】:

      【解决方案8】:

      我在 pom.xml 中遇到了类似的问题,该pom.xml 用于将我的数据模型分离到一个新模块中。

      阅读这篇文章后,我发现我不小心将&lt;configuration&gt;&lt;skip&gt;true&lt;/skip&gt;&lt;/configuration&gt; 放在了&lt;executions&gt; 块中。下面是我的更正 pom.xml 构建部分。仅供参考,我的pom.xml 中有没有 &lt;packaging&gt; 条目。

      使用 Ubuntu 20.04 LTS、Java 11.0.7、maven 3.6.3、Spring Starter 2.3.2.RELEASE、IntelliJ 2020.2

      我的数据模型模块的 pom.xml 的工作构建配置

          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                      <executions>
                          <execution>
                              <goals>
                                  <goal>repackage</goal>
                              </goals>
                          </execution>
                      </executions>
                      <configuration>
                          <skip>true</skip>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      

      【讨论】:

      • 谢谢,为我工作。有同样的问题。
      【解决方案9】:

      我解决了,

      将以下代码添加到pom文件中,

      <profiles>
          <profile>
              <id>profile1</id>
              <properties>
                  <spring.boot.mainclass>com.detaysoft.Application</spring.boot.mainclass>
              </properties>
          </profile>
      </profiles>
      

      然后如下构建build标签,

          <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
                  <executions>
                      <execution>
                          <goals>
                              <goal>repackage</goal>
                          </goals>
                          <configuration>
                              <mainClass>${spring.boot.mainclass}</mainClass>
                          </configuration>
                      </execution>
                  </executions>
              </plugin>
          </plugins>
      </build>
      

      【讨论】:

      • 它只在配置标签在外面而不是在执行时才有效。
      【解决方案10】:

      你不直接用注解@SpringBootApplication告诉它吗?

      https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html

      许多 Spring Boot 开发人员的主类总是使用 @Configuration@EnableAutoConfiguration@ComponentScan 注释。由于这些注解经常一起使用(特别是如果您遵循上述最佳实践),Spring Boot 提供了一个方便的@SpringBootApplication 替代方案。

      【讨论】:

        【解决方案11】:

        我也偶然发现了这个问题,结果发现包装类型错误。当我将packagingpom 更改为jar 时,一切都开始正常工作了。

        【讨论】:

          【解决方案12】:

          我也遇到了同样的问题,添加&lt;id&gt;repackage&lt;/id&gt;解决了。

           <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                      <executions>
                          <execution>
                              <id>repackage</id>
                              <goals>
                                  <goal>repackage</goal>
                              </goals>
                              <configuration>
                                  <skip>true</skip>
                              </configuration>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          

          【讨论】:

            【解决方案13】:

            就我而言,我必须添加“sourceDirectory”并且我的 pom.xml 更改为:

            ...

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

            ...

            ...

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

            ...

            【讨论】:

              【解决方案14】:

              在我的例子中,我有一个名为 java 的代码源文件,尽管我明确使用了 kotlin

              <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
              <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
              

              【讨论】:

                【解决方案15】:

                如果您有多个模块的 maven 项目,作为第一个解决方法,只需禁用重新打包即可避免此异常:

                <properties>
                    <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
                </properties>
                

                其他解决方法:

                <executions>
                      <execution>
                          <goals>
                               <goal>repackage</goal>
                           </goals>
                      </execution>
                </executions>
                <configuration>
                      <skip>true</skip>
                </configuration>
                

                作为解决方案:

                  <properties>
                        <spring.boot.mainclass>ir.shaparak.Application</spring.boot.mainclass>
                     </properties>
                

                【讨论】:

                  【解决方案16】:

                  使用 Maven,您可以通过以下方式判断哪个是主类:

                  <build>
                    <plugins>
                      <plugin>
                        <configuration>
                          <mainClass>com.xxx.yyy.mainApplication<mainClass>
                        <configuration>
                      <plugin>
                    <plugins>
                  </build>
                  

                  【讨论】:

                    【解决方案17】:

                    我在多模块项目中遇到了同样的问题,我添加了 &lt;mainClass&gt;package-name-in-which-main-method-resides&lt;/mainClass&gt; 并且成功了。

                    <build>
                    <plugins>
                        <plugin>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                        <mainClass>package-name-where-@SpringBootApplication-main-method-is(com.agiletunes)</mainClass>
                        </configuration>
                        </plugin>
                    </plugins>
                    </build>
                    

                    【讨论】:

                      【解决方案18】:

                      如果您使用 maven 手动构建 Sprint Boot 项目,请确保您的项目有一个类似“class Application extends SpringBootServletInitializer”的类,其中包含 main 方法。

                      【讨论】:

                        【解决方案19】:

                        就我而言,我错过了 progect pom 中的波纹管标签:

                        <packaging>jar</packaging>
                        

                        <packaging>war</packaging>
                        

                        所以,我添加了,效果很好。

                        【讨论】:

                          【解决方案20】:

                          在我的例子中,我有一个 kotlin 项目,但 src 目录被 IDE 命名为 java 而不是 kotlin。重构名称解决了这个问题。

                          【讨论】:

                            【解决方案21】:

                            我也有同样的问题,通过添加应用程序类解决了

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

                            我错过了在项目配置期间添加应用程序起点。

                            【讨论】:

                              【解决方案22】:

                              有两种方法可以解决这个问题:

                              1. 创建主类:
                              public class Main {public static void main(String[] args){}}
                              
                              1. 从 pom.xml 中删除插件:
                              <plugin>
                                   <groupId>org.springframework.boot</groupId>
                                   <artifactId>spring-boot-maven-plugin</artifactId>
                              </plugin>
                              

                              【讨论】:

                                猜你喜欢
                                • 2013-10-11
                                • 1970-01-01
                                • 2017-07-17
                                • 1970-01-01
                                • 2019-01-23
                                • 2020-09-22
                                • 2016-01-18
                                • 1970-01-01
                                • 2019-01-27
                                相关资源
                                最近更新 更多