Java 我如何告诉 Spring Boot 哪个主类用于可执行 jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23217002/
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
How do I tell Spring Boot which main class to use for the executable jar?
提问by Thilo
Execution default of goal
org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage
failed:
Unable to find a single main class from the following candidates
My project has more than one class with a main
method. How do I tell the Spring Boot Maven plugin which of the classes it should use as the main class?
我的项目有不止一个带有main
方法的类。我如何告诉 Spring Boot Maven 插件它应该使用哪个类作为主类?
采纳答案by ludo_rj
Add your start class in your pom:
在你的 pom 中添加你的起始类:
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>
or
或者
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.mycorp.starter.HelloWorldApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
回答by Renaud
If you do NOT use the spring-boot-starter-parent pom, then from the Spring documentation:
如果您不使用 spring-boot-starter-parent pom,则来自Spring 文档:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.3.RELEASE</version>
<configuration>
<mainClass>my.package.MyStartClass</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
回答by Marcelo C.
For those using Gradle (instead of Maven) :
对于那些使用 Gradle(而不是 Maven)的人:
springBoot {
mainClass = "com.example.Main"
}
回答by mojave
If you're using spring-boot-starter-parent in your pom, you simply add the following to your pom:
如果您在 pom 中使用 spring-boot-starter-parent,只需将以下内容添加到您的 pom 中:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Then do your mvn package.
然后做你的 mvn 包。
请参阅此 Spring 文档页面。
A very important aspect here is to mention that the directory structure has to be src/main/java/nameofyourpackage
这里一个非常重要的方面是提到目录结构必须是 src/main/java/nameofyourpackage
回答by Anthony De Smet
I had renamed my project and it was still finding the old Application
class on the build path. I removed it in the 'build' folder and all was fine.
我重命名了我的项目,它仍然Application
在构建路径上找到旧类。我在“build”文件夹中删除了它,一切都很好。
回答by TRIDIB BOSE
I tried the following code in pom.xml and it worked for me
我在 pom.xml 中尝试了以下代码,它对我有用
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>myPackage.HelloWorld</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javaw.exe</executable>
</configuration>
</plugin>
</plugins>
回答by tan9
Since Spring Boot 1.5, you can complete ignore the error-prone string literal in pom or build.gradle. The repackaging tool (via maven or gradle plugin) will pick the one annotated with @SpringBootApplication
for you. (Refer to this issue for detail: https://github.com/spring-projects/spring-boot/issues/6496)
从 Spring Boot 1.5 开始,你可以完全忽略 pom 或 build.gradle 中容易出错的字符串文字。重新打包工具(通过 maven 或 gradle 插件)会@SpringBootApplication
为你选择一个带注释的。(详细参考本期:https: //github.com/spring-projects/spring-boot/issues/6496)
回答by Shane Lu
For those using Gradle (instead of Maven), referencing here:
对于那些使用 Gradle(而不是 Maven)的人,参考这里:
The main class can also be configured explicitly using the task's mainClassName property:
也可以使用任务的 mainClassName 属性显式配置主类:
bootJar {
mainClassName = 'com.example.ExampleApplication'
}
Alternatively, the main class name can be configured project-wide using the mainClassName property of the Spring Boot DSL:
或者,可以使用 Spring Boot DSL 的 mainClassName 属性在项目范围内配置主类名:
springBoot {
mainClassName = 'com.example.ExampleApplication'
}
回答by dy10
Have seen this issue with Java 1.9 and SpringBoot 1.5.x, when main-class is not specified explicitly.
当未明确指定 main-class 时,在 Java 1.9 和 SpringBoot 1.5.x 中已经看到了这个问题。
With Java 1.8, it is able to find main-class without explicit property and 'mvn package' works fine.
使用 Java 1.8,它可以在没有显式属性的情况下找到主类,并且“mvn 包”工作正常。