为 spring 启动应用程序生成 war 包时,maven 构建失败?
maven build failing when generating war package for spring boot application?
这是对我之前在 SO 的问题 的扩展。根据此 post,生成 deployable war
不需要 main 方法
我正在尝试为 uploading files. The source code can be for this example application can be found here 的这个简单应用程序生成一个 deployable war
。
按照从 spring boot for jar 到 war 转换的说明,我更改了我的 pom.xml
以反映以下内容。 (刚刚添加了范围为 provided
的 tomcat
依赖项)。
<?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
)。
- 没有主要方法(注意我在上面注释掉了主要方法)
- 没有 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方法时,构建成功
如果您想获得 两者 的好处 - 即独立可执行文件 war 嵌入 Tomcat 和 正常的 war 可部署在外部 Tomcat - 你需要有一个 class with main 方法。所以,
- 在您的 Application.java 中启用 main() 方法。
- 配置
spring-boot-maven-plugin
以指定 class 与主要的 class (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>
- 删除 pom 中
provided
范围的 spring-boot-starter-tomcat
依赖项。 Spring 引导神奇地知道如何 enable/disable 嵌入 Tomcat.
有了这个,您应该可以通过 运行 Application.java class 从 IDE 启动您的 Spring 应用程序,就像正常的 Java 应用程序,构建一个 war 文件,既可以独立执行又可以像往常一样在外部部署 Tomcat。
我目前正在使用 Spring Boot 1.0 构建 REST API。1.RELEASE 使用这种设置,它在所有三种模式下都运行良好。
请注意,您可以按照
的方式得到类似的消息
无法执行目标org.springframework.boot:spring-boot-maven-plugin:1.3.2.RELEASE:项目Xyz上的重新打包(默认):目标的执行默认值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]
(我在尝试 运行 Maven 安装时在我的应用程序中看到)
就我而言,我有多个主要方法,Spring无法自动选择我想要的方法。
解决方案与 Naymesh Mistry 之前概述的解决方案相同(在 pom.xml 中明确指定 your.main.class.with.package.prefix)
这是对我之前在 SO 的问题 deployable war
我正在尝试为 uploading files. The source code can be for this example application can be found here 的这个简单应用程序生成一个 deployable war
。
按照从 spring boot for jar 到 war 转换的说明,我更改了我的 pom.xml
以反映以下内容。 (刚刚添加了范围为 provided
的 tomcat
依赖项)。
<?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
)。
- 没有主要方法(注意我在上面注释掉了主要方法)
- 没有 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方法时,构建成功
如果您想获得 两者 的好处 - 即独立可执行文件 war 嵌入 Tomcat 和 正常的 war 可部署在外部 Tomcat - 你需要有一个 class with main 方法。所以,
- 在您的 Application.java 中启用 main() 方法。
- 配置
spring-boot-maven-plugin
以指定 class 与主要的 class (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>
- 删除 pom 中
provided
范围的spring-boot-starter-tomcat
依赖项。 Spring 引导神奇地知道如何 enable/disable 嵌入 Tomcat.
有了这个,您应该可以通过 运行 Application.java class 从 IDE 启动您的 Spring 应用程序,就像正常的 Java 应用程序,构建一个 war 文件,既可以独立执行又可以像往常一样在外部部署 Tomcat。
我目前正在使用 Spring Boot 1.0 构建 REST API。1.RELEASE 使用这种设置,它在所有三种模式下都运行良好。
请注意,您可以按照
的方式得到类似的消息无法执行目标org.springframework.boot:spring-boot-maven-plugin:1.3.2.RELEASE:项目Xyz上的重新打包(默认):目标的执行默认值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]
(我在尝试 运行 Maven 安装时在我的应用程序中看到)
就我而言,我有多个主要方法,Spring无法自动选择我想要的方法。
解决方案与 Naymesh Mistry 之前概述的解决方案相同(在 pom.xml 中明确指定 your.main.class.with.package.prefix)