首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gradle + Embedded Jetty

Gradle + Embedded Jetty
EN

Stack Overflow用户
提问于 2017-03-12 06:07:12
回答 1查看 955关注 0票数 0

我已经修改了一个现有的Jetty项目,在我构建之后,我得到了404。也许我需要修改其他我不知道的文件。我正在使用gradle来构建。下面是build.gradle:

代码语言:javascript
复制
apply plugin: 'java'
apply plugin: 'jetty'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework:spring-webmvc:4.1.6.RELEASE'
    compile  'org.hibernate:hibernate-core:4.3.6.Final'
    testImplementation 'junit:junit:4.12'
    testCompile 'junit:junit:4.11'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.14'
}
test {
    exclude '**/*IntegrationTest*'
}

task integrationTest(type: Test) {
    include '**/*IntegrationTest*'
    doFirst {
        jettyRun.contextPath = '/';
        jettyRun.httpPort = 8080    // Port for test
        jettyRun.daemon = true
        jettyRun.execute()
    }
    doLast {
        jettyStop.stopPort = 8091   // Port for stop signal
        jettyStop.stopKey = 'stopKey'
        jettyStop.execute()
    }
}


// Embeded Jetty for testing
jettyRun{
    contextPath = "spring4"
    httpPort = 8080
}

jettyRunWar{
    contextPath = "spring4"
    httpPort = 8080
}


//For Eclipse IDE only
eclipse {

  wtp {
    component {

      //define context path, default to project folder name
      contextPath = 'spring4'

    }

  }
}

下面是另一个类:

代码语言:javascript
复制
package com.hello.webapp;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.hello.service.SignUpService;

@Path("/signUp")
public class SignUpWebapp {
    private static SignUpService signUpService = new SignUpService();

    @GET()
    public String hello() {
        return signUpService.sayHello();
    }
}

这里是一个简单的服务:

代码语言:javascript
复制
package com.hello.service;

public class SignUpService {

    public String sayHello() {
        return "signUp";
    }

}

这是另一个集成测试类

代码语言:javascript
复制
package com.hello.webapp;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.junit.Test;

public class SignUpIntegrationTest {
    private static String SIGNUP = "http://localhost:8080/signUp";

    @Test
    public void testHello() throws Exception {
        Client client = ClientBuilder.newClient();
        WebTarget webTarget = client.target(SIGNUP);
        String response = webTarget.request().get(String.class);

        assertThat(response, is("signUp"));
    }
}

所以,当我运行gradle integrationTest时,我得到一个错误,告诉我Jetty已经在运行了。当我尝试访问localhost/signUp时,我得到了404。

EN

回答 1

Stack Overflow用户

发布于 2017-04-19 10:21:44

一种解决方案是使用gretty:

代码语言:javascript
复制
apply plugin: 'org.akhikhl.gretty'
....
classpath 'org.akhikhl.gretty:gretty:1.4.0'
...
gretty {
    port = 8083
    contextPath = '/'
    servletContainer = 'jetty9'
    jvmArgs = [
            '-Xms700m',
            '-Xmx2048m'
            ]
    loggingLevel='INFO'
    debugPort = 5005      // default
    debugSuspend = true   // default

    httpsEnabled = true
    httpsPort = 8443
    sslKeyStorePath = 'some.jks'
    sslKeyStorePassword = 'somepwd'
}

然后gradle appStart

如果有帮助,请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42741226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档