在 Eclipse 中的 spring-boot 项目上使用 Maven “无法找到主类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42937577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Unable to find main class" with Maven on spring-boot project in Eclipse
提问by Alex
I am pretty desperate since I no longer can compile my Maven projects. I have one logical project split into 7 maven modules which so far compiled fine, but today I introduced a feature, whereby I accidentally generated a circular maven dependency. Therefore I refactored the code and eliminated the circular dependency. Ever since I updated the pom files to reflect the fix my project doesn't build anymore. When I run maven with "-X clean install" I get the following java.lang.IllegalStateException: Unable to find main class
我非常绝望,因为我不再能够编译我的 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<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>
.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]
...
This is the pom.xml of my maven parent project:
这是我的 maven 父项目的 pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
What do I need to do, to fix this? Please let me know, should you need additional information. Thank you in advance.
我需要做什么来解决这个问题?如果您需要其他信息,请告诉我。先感谢您。
回答by Pavel Grigorev
You should have this
你应该有这个
<start-class>your.package.main</start-class>
only in those modules that you want to run, but not in the parent pom.
仅在您要运行的那些模块中,而不是在父 pom 中。
回答by Carlos
You can try adding this in the properties section of your pom
您可以尝试在 pom 的属性部分添加它
<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>
回答by sashok_bg
I had the same problem. It ended being a spring boot mvn plugin declaration on a subproject that does not actually run (no main class). I just removed the spring boot mvn plugin from the submodule and everything ran fine
我有同样的问题。它最终是一个实际上没有运行的子项目(没有主类)上的 spring boot mvn 插件声明。我刚刚从子模块中删除了 spring boot mvn 插件,一切正常
回答by ali akbar azizkhani
add property to maven properties like this
像这样将属性添加到 Maven 属性中
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
回答by Boris Kuznetsov
Just for someone who faced with similar promblem. I added spring-boot-maven-plugin
in parent POM in section pluginManagement
section like this:
仅适用于面临类似问题的人。我spring-boot-maven-plugin
在这样的部分pluginManagement
部分添加了父 POM :
<properties>
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>
Then I added the plugin only in the modules using it (in plugin
section).
Project has been successfully built after that.
然后我只在使用它的模块中添加了插件(在plugin
部分)。此后项目已成功构建。
回答by surendrakumar Gadde
In project agiletunes-util pom.xml add
在项目agiletunes-util pom.xml 中添加
<profiles>
<profile>
<id>profile1</id>
<properties>
<spring.boot.mainclass>com.detaysoft.Application</spring.boot.mainclass>
</properties>
</profile>
</profiles>
回答by ?mer Enlicay
I solved that,
我解决了,
Add the following code to pom file,
将以下代码添加到 pom 文件中,
<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>
Then build the build tag as follows,
然后按如下方式构建构建标记,
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
回答by Chio Quavario
if you don't have a main class in springboot project, you can execution spring-boot-maven-plugin by follow
如果你在springboot项目中没有主类,你可以通过follow执行spring-boot-maven-plugin
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
回答by Don Rhummy
Wouldn't you simply tell it by using the annotation @SpringBootApplication
?
你不会简单地通过使用注释来告诉它@SpringBootApplication
吗?
Many Spring Boot developers always have their main class annotated with
@Configuration
,@EnableAutoConfiguration
and@ComponentScan
. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient@SpringBootApplication
alternative.
许多 Spring Boot 开发人员总是用
@Configuration
,@EnableAutoConfiguration
和注释他们的主类@ComponentScan
。由于这些注解经常一起使用(特别是如果您遵循上述最佳实践),Spring Boot 提供了一个方便的@SpringBootApplication
替代方案。
回答by barbossusus
In my case I had to add the "sourceDirectory" and my pom.xml changed from:
在我的情况下,我必须添加“ sourceDirectory”,我的 pom.xml 更改为:
...
...
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
...
...
to
到
...
...
##代码##...
...