【问题标题】:How to create a Executable Jar for a non-web application with Spring Boot如何使用 Spring Boot 为非 Web 应用程序创建可执行 Jar
【发布时间】:2017-08-30 12:52:06
【问题描述】:

我正在为非 Web 应用程序创建一个 Spring-Boot 应用程序,我想知道我必须使用什么插件或程序。

对于微服务开发,在build.gradle中添加的插件是:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
    }
}

apply plugin: 'org.springframework.boot'

但是,当你执行生成的Jar时,jar会启动一个Tomcat,这种软件不是必需的:

`java -jar consoleApp-0.7.0-SNAPSHOT.jar demo=demo``

2017-08-30 14:48:13.505  INFO 82718 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-30 14:48:13.510  INFO 82718 --- [           main] spring.ParamLoader                       : Loading data...
2017-08-30 14:48:13.510  INFO 82718 --- [           main] spring.ParamLoader                       : item:
2017-08-30 14:48:13.523  INFO 82718 --- [           main] spring.ConsoleApp                        : Started ConsoleApp in 4.648 seconds (JVM running for 5.257)
^C2017-08-30 14:48:18.502  INFO 82718 --- [       Thread-3] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6477463f: startup date [Wed Aug 30 14:48:09 CEST 2017]; root of context hierarchy

还有其他选择吗?

非常感谢。

胡安·安东尼奥

【问题讨论】:

  • 您不需要其他任何东西。启动的内容取决于依赖项。如果您不包含 tomcat(或其他受支持的 servlet 容器之一),它将作为非 Web 应用程序启动。

标签: spring-boot console-application executable-jar


【解决方案1】:

你所拥有的是一个好的开始。

现在添加以下依赖项以获取所有 Spring 基础知识。

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

然后您的主类可以实现org.springframework.boot.CommandLineRunner,您的应用可以在run 方法中开始工作。

@SpringBootApplication
public class MyApplication implements CommandLineRunner {
  @Override 
  public void run(String[] args) {
    // do your work here
  }

  public static void main(String[] args) {
     SpringApplication.run(MyApplication.class, args);
  }
}

PS。如果您首先启动 Tomcat,请确保您的依赖列表包含spring-boot-starter-web

【讨论】:

  • 嗨@Strelok,正如你所说,问题是依赖关系。 compile("org.springframework.boot:spring-boot-starter:1.5.6.RELEASE") 以前: compile("org.springframework.boot:spring-boot-starter-web:1.5.6.RELEASE") 当我删除了后缀:web,该应用程序具有预期的行为。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 2017-08-31
  • 1970-01-01
  • 2016-02-09
  • 2017-08-20
  • 2017-02-19
相关资源
最近更新 更多