spring 为spring boot应用程序生成war包时maven构建失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29831953/
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
maven build failing when generating war package for spring boot application?
提问by brain storm
This is an extension to my previous question hereat SO. As per this post, main method is NOT required to generate a deployable war
这是一个扩展我刚才的问题在这里的SO。根据这篇文章,不需要 main 方法来生成deployable war
I am trying to generate a deployable war
for this simple application of uploading files. The source code can be for this example application can be found here.
我正在尝试deployable war
为这个简单的上传文件应用程序生成一个。可以在此处找到此示例应用程序的源代码。
Following the instructions from spring boot for jar to war conversion, I changed my pom.xml
to reflect the following. (Just added tomcat
dependency with scope provided
).
按照从 spring boot for jar 到 war 转换的说明,我更改了我的pom.xml
以反映以下内容。(刚刚添加了tomcat
对 scope 的依赖provided
)。
参考:http: //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging
<?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>
<groupId>org.springframework</groupId>
<artifactId>gs-uploading-files</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.7</java.version>
</properties>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
Then I changed Application.java
as follows
然后我改成Application.java
如下
参考:http: //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
/*public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} */
}
I tried two scenarios. both fail with the following error (Unable to find main class
).
我尝试了两种情况。都失败并出现以下错误 ( Unable to find main class
)。
- No main method (NOTE I have commented out main method above)
- No application.Java file (commented out everything from this file - not shown here)
- 没有 main 方法(注意我已经注释掉了上面的 main 方法)
- 没有 application.Java 文件(注释掉该文件中的所有内容 - 此处未显示)
Error:
错误:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.218s
[INFO] Finished at: Thu Apr 23 11:46:24 PDT 2015
[INFO] Final Memory: 22M/222M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage (default) on project gs-uploading-files: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
PS: when I have main method intact, the build is successful
PS:当我有完整的main方法时,构建成功
回答by Naymesh Mistry
If you want to get boththe benefits - i.e. standalone executable war with embedded Tomcat andthe normal war deployable in external Tomcat - you need to have a class with main method. So,
如果你想同时获得两个好处——即具有嵌入式 Tomcat 的独立可执行War和可在外部 Tomcat 中部署的普通War——你需要有一个带有 main 方法的类。所以,
- Enable the main() method in your Application.java.
- Configure
spring-boot-maven-plugin
to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit I guess):<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot-version}</version> <configuration> <mainClass>the.package.of.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
- Remove
spring-boot-starter-tomcat
dependency in your pom withprovided
scope. Spring boot magically knows how to enable/disable embedded Tomcat.
- 在 Application.java 中启用 main() 方法。
- 配置
spring-boot-maven-plugin
为使用主类指定类(如果你有一个,Spring 无论如何都应该找到它,但我猜最好是明确的):<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot-version}</version> <configuration> <mainClass>the.package.of.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
spring-boot-starter-tomcat
使用provided
范围删除pom 中的依赖项。Spring boot 神奇地知道如何启用/禁用嵌入式 Tomcat。
With this you should be able to launch your Spring app from IDE by just running the Application.java class as a normal Java app, build a war file that is both standalone executable and deployable in external Tomcat as usual too.
有了这个,您应该能够从 IDE 启动您的 Spring 应用程序,只需将 Application.java 类作为普通 Java 应用程序运行,构建一个既是独立的可执行文件又可以像往常一样在外部 Tomcat 中部署的 war 文件。
I am currently building REST APIs with Spring Boot 1.0.1.RELEASE with this kind of setup and it works great in all three modes.
我目前正在使用这种设置使用 Spring Boot 1.0.1.RELEASE 构建 REST API,并且它在所有三种模式下都运行良好。
回答by Toothless Seer
Note that one can get a somewhat similar message along the lines of
请注意,您可以获得类似的消息
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.2.RELEASE:repackage (default) on project Xyz: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates[com.a.Application, com.a.fake.Application, com.a.main.Main1, com.a.runner.DataImportRunner, com.a.temp.dependencyinjection.MainApp, com.a.finalapp.facade.impl.V3DataImports, com.a.finalapp.service.impl.App2]
无法执行目标 org.springframework.boot:spring-boot-maven-plugin:1.3.2.RELEASE:repackage (default) on project Xyz: 目标 org.springframework.boot:spring-boot-maven-plugin 的执行默认值: 1.3.2.RELEASE:repackage failed: Unable to find a single main class from the following候选人[com.a.Application, com.a.fake.Application, com.a.main.Main1, com.a.runner.DataImportRunner , com.a.temp.dependencyinjection.MainApp, com.a.finalapp.facade.impl.V3DataImports, com.a.finalapp.service.impl.App2]
(Which I saw in my application when attempting to run a Maven Install)
(我在尝试运行 Maven 安装时在我的应用程序中看到的)
In my case, I had multiple main methods and Spring could not automatically choose the one I wanted.
就我而言,我有多个主要方法,Spring 无法自动选择我想要的方法。
The solution is the same outlined earlier by Naymesh Mistry (explicitly specify your.main.class.with.package.prefix in pom.xml)
解决方案与 Naymesh Mistry 之前概述的相同(在 pom.xml 中明确指定 your.main.class.with.package.prefix)