gradle应用程序插件已关闭

时间:2015-10-20 03:51:51

标签: plugins gradle

我正在使用gradle应用程序插件来运行我的应用程序。下面是我的build.gradle:

group 'demo.jersey'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.5

mainClassName = "com.example.Main"

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    compile 'org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.22.1'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

当我运行gradle run时,我可以看到我的应用程序已启动,但它立即关闭。以下是我的主课程。你知道如何在不关闭的情况下阻止应用程序吗?

package com.example;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import java.io.IOException;
import java.net.URI;

public class Main {

    public static final String BASE_URI = "http://localhost:8080/myapp/";
    public static HttpServer startServer() {
        final ResourceConfig rc = new ResourceConfig().packages("com.example");
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }

    public static void main(String[] args) throws IOException {
        final HttpServer server = startServer();
        System.out.println(String.format("Jersey app started with WADL available at "
            + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
        System.in.read();
        server.stop();
    }
}

1 个答案:

答案 0 :(得分:0)

它会因为您明确调用server.stop();而停止,因此程序结束。注释掉这一行,它将继续运行并监听传入的请求。