当前位置: 首页 > 知识库问答 >
问题:

为Spring Boot应用程序生成war包时maven build失败?

阙辰龙
2023-03-14

这是对我前面问题的延伸。根据本文,main方法不需要生成可部署的WAR

参考: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);
    } */
} 
    null

错误:

[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:当我的主方法完好无损时,构建就成功了

共有1个答案

程亦
2023-03-14

如果您想要同时获得这两个好处--即使用嵌入式Tomcat的独立可执行war和可在外部Tomcat中部署的普通war--您需要有一个带有main方法的类。所以,

  • 在application.java中启用main()方法。
  • 配置spring-boot-maven-plugin以指定带有主类的类(如果有的话Spring应该会找到它,但我想明确一点是好的): spring-boot-maven-plugin ${spring-boot-version}
  • 删除pom中的spring-boot-starter-tomcat依赖项,并提供作用域。Spring boot神奇地知道如何启用/禁用嵌入式Tomcat。

通过这样做,您应该能够从IDE启动您的Spring应用程序,只需将Application.Java类作为普通Java应用程序运行,并像往常一样在外部Tomcat中构建一个独立的可执行和可部署的war文件。

我目前正在用Spring Boot 1.0.1构建REST API,用这种设置发布,它在所有三种模式下都工作得很好。

 类似资料: