为Spring Boot应用程序生成war软件包时,maven构建失败? [英] maven build failing when generating war package for spring boot application?
问题描述
This is an extension to my previous question here at SO. As per this post, main method is NOT required to generate a 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.
按照从spring boot到jar到war转换的说明,我更改了pom.xml
以反映以下内容. (只需将tomcat
依赖项添加到范围provided
).
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
).
<?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>
然后我按如下所示更改了Application.java
Then I changed Application.java
as follows
参考:我尝试了两种情况.都失败,并显示以下错误(Unable to find main class
).
I tried two scenarios. both fail with the following error (Unable to find main class
).
- 没有主要方法(注意,我已经在上面注释了主要方法)
- 没有application.Java文件(注释了该文件中的所有内容-这里未显示)
错误:
[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:当我的主要方法完好无损时,构建成功
推荐答案
如果您想同时获得两者的好处-即与嵌入式Tomcat 和正常运行的独立可执行文件之战可以在外部Tomcat中部署战争-您需要有一个带有main方法的类.所以,
If you want to get both the benefits - i.e. standalone executable war with embedded Tomcat and the normal war deployable in external Tomcat - you need to have a class with main method. So,
- 在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>
- 使用
provided
范围删除pom中的spring-boot-starter-tomcat
依赖项. Spring Boot神奇地知道如何启用/禁用嵌入式Tomcat.
- 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类作为普通Java应用程序运行,从IDE中启动Spring应用程序,构建一个既可以独立执行又可以像往常一样在外部Tomcat中部署的war文件.
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.
我目前正在使用Spring Boot 1.0.1.RELEASE通过这种设置来构建REST API,并且在所有三种模式下均能很好地工作.
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应用程序生成war软件包时,maven构建失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!