SpringBoot App打包为jar时无法启动

时间:2016-08-04 07:24:15

标签: maven jar spring-boot

我有一个运行良好的SpringBoot应用程序,但是当我尝试将其打包为jar时,它会因以下错误消息而崩溃

java -jar target/Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar

登录

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::

2016-08-04 09:13:49.743  INFO 10008 --- [           main] de.myapp.Run              : Starting Run on d6889988 with PID 10008 (started by User in X:\Myapp)
2016-08-04 09:13:49.747  INFO 10008 --- [           main] de.myapp.Run              : No active profile set, falling back to default profiles: default
2016-08-04 09:13:49.865  INFO 10008 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@11f66796: startup date [Thu Aug 04 09:13:49 CEST 2016]; root of context hierarchy
2016-08-04 09:13:50.479  WARN 10008 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
2016-08-04 09:13:50.488 ERROR 10008 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at de.arbeitsagentur.amsel.Run.main(Run.java:53) [Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
        ... 8 common frames omitted

2016-08-04 09:13:50.490  INFO 10008 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/X:/Myapp/target/Amsel-1.0-SNAPSHOT-jar-with-dependencies.jar]

my Run.class with的入口点是以下

@SpringBootApplication
@ComponentScan("de.myapp")
public class Run {
    public static void main(String[] args) throws Exception{
        SpringApplication.run(Run.class, args);
    }
}

这是pom.xm l,用于生成jar

<?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>de.myapp.amsel</groupId>
    <artifactId>Amsel</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Amsel</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>de.myapp.Run</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </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>
        </dependency>
</project>

任何想法可能是什么问题?

1 个答案:

答案 0 :(得分:0)

好的,我自己找到了答案,当涉及到打包为jar文件时,springboot似乎有点不同,所以我不得不添加springboot-maven插件

http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html