首页 文章

在为spring boot应用程序生成war包时,maven build失败了吗?

提问于
浏览
7

这是我在SO上的上一个问题here的扩展 . 根据这篇文章,主要方法不需要生成 deployable war

我正在尝试为这个简单的uploading files应用程序生成 deployable war . 源代码可以为此示例应用程序找到here .

按照 spring 启动的说明进行jar到war的转换,我更改了 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

参考: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 ) .

  • 没有主要方法(注意我已经注释掉上面的主要方法)

  • 没有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: when I have main method intact, the build is successful

2 回答

  • 4

    如果你想获得 both 的好处 - 即与嵌入式Tomcat的独立可执行战争以及可在外部Tomcat中部署的正常战争 - 你需要有一个带有main方法的类 . 所以,

    • 在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 .

    有了这个,您应该能够通过运行Application.java类作为普通的Java应用程序从IDE启动Spring应用程序,构建一个既可以独立执行也可以在外部Tomcat中部署的war文件 .

    我目前正在使用Spring Boot 1.0.1.RELEASE构建REST API并使用这种设置,它在所有三种模式下都能很好地工作 .

  • 12

    请注意,人们可以得到类似的消息


    无法执行目标org.springframework.boot:spring-boot-maven-plugin:1.3.2.RELEASE:项目Xyz上的重新打包(默认):目标org.springframework.boot的执行默认值:spring-boot-maven-plugin: 1.3.2.RELEASE:重新包装失败: 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)

相关问题