在Eclipse中的Spring-boot项目中使用Maven“无法找到主类”

时间:2017-03-21 20:30:56

标签: eclipse maven spring-boot

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

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

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

20 个答案:

答案 0 :(得分:47)

你应该有这个

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

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

答案 1 :(得分:24)

您可以尝试在pom的属性部分添加此内容

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

答案 2 :(得分:13)

我遇到了同样的问题。它结束了一个实际上没有运行的子项目上的spring boot mvn插件声明(没有主类)。我刚刚从子模块中删除了spring boot mvn插件,一切运行正常

答案 3 :(得分:12)

add property to maven properties like this

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

答案 4 :(得分:12)

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

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

答案 5 :(得分:4)

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

在阅读此内容时,我发现我不小心将<configuration><skip>true</skip></configuration>放在了<executions>块中。以下是我的更正pom.xml构建部分。仅供参考,我的<packaging>中没有 pom.xml项。

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

答案 6 :(得分:3)

我解决了这个问题,

将以下代码添加到pom文件

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

然后按如下方式构建构建标记

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

答案 7 :(得分:2)

您是不是只想使用注释@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替代方案。

答案 8 :(得分:2)

我也偶然发现了这个问题,结果证明包装类型是错误的。当我将#include <iostream> #include <string> class Person{ public: std::string name; }; class Printer{ public: void print(const Person& person ){ printf("%s", person.name.c_str()); } }; int main() { Person one; one.name = "name"; Printer printer; printer.print(one); } packaging更改为pom时,一切开始正常工作。

答案 9 :(得分:1)

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

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

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

答案 10 :(得分:1)

如果springboot项目中没有主类,则可以按照以下步骤执行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>

答案 11 :(得分:1)

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

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

答案 12 :(得分:1)

如果您有多模块 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>

答案 13 :(得分:0)

就我而言,我错过了 progect pom 中的 bellow 标签:

<packaging>jar</packaging>

<packaging>war</packaging>

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

答案 14 :(得分:0)

我也遇到了同样的问题,通过添加 <id>repackage</id> 得以解决。

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

答案 15 :(得分:0)

就我而言,即使我显式使用java,我也有一个名为kotlin的代码源文件:

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

答案 16 :(得分:0)

我也遇到了同样的问题,并通过将Application类添加为

解决了
@SpringBootApplication
public class Application {

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

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

答案 17 :(得分:0)

如果您使用maven手动构建Sprint Boot项目,请确保您的项目具有类似“类应用程序扩展SpringBootServletInitializer类”的类,其中包含主要方法。

答案 18 :(得分:0)

在我的情况下,我必须添加“ 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>

...

答案 19 :(得分:-1)

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

  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>