org.springframework.web.SpringServletContainerInitializer无法强制转换为javax.servlet.ServletContainerInitializer

时间:2014-03-29 07:22:27

标签: tomcat spring-mvc gradle

尝试通过gradle运行tomcatRun任务时出现此错误 显然这是由于依赖性问题引起的。我的build.gradle文件粘贴在下面:

    apply plugin: 'war'
apply plugin: 'tomcat'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

//these create the 'provided' scope, mainly for the servlet dependency
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-eclipse'

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

  dependencies {
    classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
    classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.1'
  }
}

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

dependencies {

    def tomcatVersion = '7.0.42'
    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.springframework:spring-core:4.0.2.RELEASE'
    compile 'org.springframework:spring-webmvc:4.0.2.RELEASE'

    compile 'org.springframework.data:spring-data-jpa:1.5.1.RELEASE'
    compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final'
    compile 'org.hibernate:hibernate-entitymanager:4.3.4.Final'

    compile 'postgresql:postgresql:9.1-901.jdbc4'

    provided 'javax.servlet:javax.servlet-api:3.0.1'

    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'

    compile 'org.slf4j:slf4j-api:1.7.5'
    runtime 'org.slf4j:slf4j-jdk14:1.7.5'

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

// tag::wrapper[]
task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}
// end::wrapper[]


tomcatRunWar.contextPath = ''

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

spring-data-jpa 1.5.1 depends on servlet-api 2.5

所以至少存在一些冲突。 检查项目的dependency tree是否存在可能的冲突,特别是检查spring框架和servlet api依赖项。