Gradle任务tomcatRun成功但webapp抛出404错误

时间:2015-02-05 01:34:20

标签: gradle tomcat7 build.gradle gradle-eclipse gradle-plugin

我在Eclipse中有一个Spring MVC webapp,我用Gradle构建。我也在使用Gradle Tomcat插件。

当我运行Gradle tomcatRunWar 任务时,一切都按预期工作,我的webapp应该是响应的。但是,当我运行Gradle tomcatRun 任务时,尽管它似乎成功并且说“服务器正在http://localhost:8080/运行”,但webapp仅抛出404错误并显示消息“请求的资源是无法使用”。

然后我使用-i(info)和-d(调试)选项运行Gradle任务,当我尝试命中服务器时,我看到以下内容:

17:28:43.030 [DEBUG] [org.apache.tomcat.util.threads.LimitLatch] Counting up[http-bio-8080-Acceptor-0] latch=1
17:28:43.031 [DEBUG] [org.apache.catalina.connector.CoyoteAdapter] The variable [uriBC] has value [/]
17:28:43.031 [DEBUG] [org.apache.catalina.connector.CoyoteAdapter] The variable [semicolon] has value [-1]
17:28:43.031 [DEBUG] [org.apache.catalina.connector.CoyoteAdapter] The variable [enc] has value [UTF-8]
17:28:43.032 [DEBUG] [org.apache.catalina.authenticator.AuthenticatorBase] Security checking request GET /
17:28:43.032 [DEBUG] [org.apache.catalina.realm.RealmBase]   No applicable constraints defined
17:28:43.032 [DEBUG] [org.apache.catalina.authenticator.AuthenticatorBase]  Not subject to any constraint
17:28:43.032 [DEBUG] [org.apache.coyote.http11.Http11Protocol] Socket: [org.apache.tomcat.util.net.SocketWrapper@5a2e857e:Socket[addr=/0:0:0:0:0:0:0:1,port=52381,localport=8080]], Status in: [OPEN_READ], State out: [OPEN]
17:28:45.216 [DEBUG] [org.apache.catalina.loader.WebappClassLoader] modified()
17:28:45.216 [DEBUG] [org.apache.catalina.loader.WebappClassLoader]     Failed tracking modifications of '/WEB-INF/lib'
17:29:05.220 [DEBUG] [org.apache.catalina.session.ManagerBase] Start expire sessions StandardManager at 1423099745220 sessioncount 0
17:29:05.220 [DEBUG] [org.apache.catalina.session.ManagerBase] End expire sessions StandardManager processingTime 0 expired sessions: 0

如果我再等10秒左右,我会看到这个:

17:29:35.224 [DEBUG] [org.apache.catalina.loader.WebappClassLoader] modified()
17:29:35.225 [DEBUG] [org.apache.catalina.loader.WebappClassLoader]     Failed tracking modifications of '/WEB-INF/lib'
17:29:43.033 [DEBUG] [org.apache.coyote.http11.Http11Processor] Error parsing HTTP request header
java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:519)
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:504)
    at org.apache.coyote.http11.Http11Processor.setRequestLineReadTimeout(Http11Processor.java:167)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:982)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
17:29:43.033 [DEBUG] [org.apache.coyote.http11.Http11Protocol] Socket: [org.apache.tomcat.util.net.SocketWrapper@5a2e857e:Socket[addr=/0:0:0:0:0:0:0:1,port=52381,localport=8080]], Status in: [OPEN_READ], State out: [CLOSED]
17:29:43.034 [DEBUG] [org.apache.tomcat.util.threads.LimitLatch] Counting down[http-bio-8080-exec-9] latch=1
17:29:45.225 [DEBUG] [org.apache.catalina.loader.WebappClassLoader] modified()

我很困惑,非常感谢你的帮助!我的build.gradle包含在下面。

apply plugin: 'war'
apply plugin: 'com.bmuschko.tomcat'
apply plugin: 'java'
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-eclipse'
apply plugin: 'eclipse'
apply plugin: 'idea'

buildscript {
  repositories {
    mavenCentral()
    maven { url "http://download.java.net/maven/2" }
    maven { url 'http://repo.spring.io/plugins-release' }

    jcenter()
  }

  dependencies {
    // classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:2.0'
    classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
    classpath 'com.bmuschko:gradle-tomcat-plugin:2.0'
  }
}

repositories {
  mavenCentral()
  maven { url 'http://repo.spring.io/milestone/'}
}

dependencies {

    compile fileTree(dir: 'lib', include: '*.jar')

    def tomcatVersion = '7.0.57'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
      exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
    }

    compile 'org.codemonkey.simplejavamail:simple-java-mail:2.1'

    compile 'org.springframework:spring-core:4.1.4.RELEASE'
    compile 'org.springframework:spring-webmvc:4.1.4.RELEASE'
    compile 'com.jayway.jsonpath:json-path:0.8.1'

    // {!begin hateoas}
    compile 'org.springframework.hateoas:spring-hateoas:0.16.0.RELEASE'
    // {!end hateoas}

    compile 'org.springframework.security:spring-security-web:3.2.5.RELEASE'
    compile 'org.springframework.security:spring-security-core:3.2.5.RELEASE'
    compile 'org.springframework.security:spring-security-config:3.2.5.RELEASE'

    compile 'org.slf4j:slf4j-api:1.7.5'
    //runtime 'org.slf4j:slf4j-jdk14:1.7.5'
    runtime 'org.slf4j:slf4j-log4j12:1.7.5'
    //compile 'log4j:log4j:1.2.16'

    runtime 'com.fasterxml.jackson.core:jackson-core:2.2.2'
    runtime 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
    runtime 'javax.xml.bind:jaxb-api:2.2.9'

    // providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'


    testCompile 'com.jayway.jsonpath:json-path-assert:0.8.1'
    testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
    testCompile 'junit:junit:4.+'
    testCompile "org.mockito:mockito-all:1.9.5"

    compile 'org.springframework:spring-jdbc:4.1.4.RELEASE'
    compile 'mysql:mysql-connector-java:5.1.26'

    compile 'com.google.guava:guava:16.0.1' 
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.2.1'
}

tomcatRunWar.contextPath = ''
tomcatRun.contextPath = ''

tomcatRun.outputFile = file('tomcatRunLog.txt')
tomcatRunWar.outputFile = file('tomcatRunWarLog.txt')

3 个答案:

答案 0 :(得分:0)

即使您使用tomcatRun.webDefaultXml声明另一个web.xml,您的项目布局也必须至少包含src / main / webapp / WEB-INF / web.xml。

答案 1 :(得分:0)

也许您应该删除 tomcatRunWar.contextPath 。我不确定''是否被部署为ROOT上下文。 404表示战争未部署到ROOT上下文中。如果删除了contextPath,则应该可以调用http://localhost:8080/

答案 2 :(得分:0)

我们需要在Deployment Assembly中添加Web Content文件夹,因为Gradle默认情况下不会对其进行配置。