【发布时间】:2015-07-02 03:22:48
【问题描述】:
这是我在 SO 上的上一个问题 here 的扩展。根据这篇文章,生成deployable war
我正在尝试为这个简单的uploading files 应用程序生成一个deployable war。此示例应用的源代码可以在here找到。
按照 spring boot 中关于 jar 到 war 转换的说明,我更改了我的 pom.xml 以反映以下内容。 (刚刚添加了 tomcat 依赖,范围为 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改成如下
@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)。
- 没有 main 方法(注意我上面已经注释掉了 main 方法)
- 没有 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:当我有完整的main方法时,构建成功
【问题讨论】:
-
有没有机会在 pom.xml 中仍然引用您的主类?
-
@ci_:我不确定你的确切意思。我已经更新了我的整个 pom.xml
-
除了您的链接问题的答案之外,我找不到任何证据表明您可以省略主类,即具有 main 方法的类。 spring-boot-maven-plugin 似乎正在寻找它。也许如果您没有使用该插件,但不确定是否值得。
-
如果您只想将应用程序作为标准war文件运行,您可以完全删除Boot插件或重新配置它,以便重新打包目标不绑定到Maven的生命周期
-
对于本地测试,我更喜欢嵌入式Tomcat。但为了部署,我需要战争。我不知道我怎样才能在这里获得这两种好处
标签: spring maven tomcat spring-boot