这是一个扩展我刚才的问题在这里的SO。根据这篇文章,不需要main方法来生成一个deployable war
deployable war
我正在尝试为上传文件的deployable war这种简单应用程序生成一个。
遵循从spring boot进行jar到war转换的说明,我更改了我的内容pom.xml以反映以下内容。(只需tomcat在scope中添加依赖项provided)。
pom.xml
tomcat
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>
然后我改变Application.java如下
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); } */ }
我尝试了两种情况。都失败,并显示以下错误(Unable to find main class)。
Unable to find main class
错误:
[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方法的类。所以,
spring-boot-maven-plugin
<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
这样,您只需通过将Application.java类作为普通Java应用程序运行即可从IDE启动Spring应用程序,并构建一个既可以独立执行又可以像往常一样在外部Tomcat中部署的war文件。
我目前正在使用Spring Boot 1.0.1.RELEASE通过这种设置来构建REST API,并且在所有三种模式下均能很好地工作。