Gradle Eclipse JSF 项目 java.lang.ClassNotFoundException javax.faces.webapp.FacesServlet

Gradle Eclipse JSF Project java.lang.ClassNotFoundException javax.faces.webapp.FacesServlet

我正在使用 Eclipse 中的 Gradle 插件创建 JSF 项目。我在 Tomcat 9 上 运行 宁项目。当我在 Eclipse 中的服务器上尝试 运行 时,我得到 java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet 异常。

我的 gradle.build 看起来像这样:

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:21.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    //Jersey
    implementation 'org.glassfish.jersey.containers:jersey-container-servlet:2.26'
    implementation 'org.glassfish.jersey.inject:jersey-hk2:2.26'
    implementation 'org.glassfish.jersey.core:jersey-common:2.26'

    //JAXB
    implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:2.26'

    //Javax Servlet
    implementation 'javax.servlet:javax.servlet-api:3.1.0'

    //JSF
    implementation 'org.glassfish:javax.faces:2.2.16'
}

eclipse {
    wtp {
        facet {
            facet name: "java", version: "1.8"          // Java version
            facet name: "jst.web", version: "3.1"       // Dynamic Web Application
            facet name: "jst.jsf", version: "2.2"       // Java Server Faces
            facet name: "wst.jsdt.web", version: "1.0"  // JavaScript
        }
    }
}

project.webAppDirName='WebContent'

我的 web.xml 看起来像这样:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4" id="WebApp_ID">
    <display-name>com.xxx</display-name>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

我可以看到 gradle 已将正确的库添加到我的资源中:

我想不出还有什么可能是错误的。感谢任何帮助。

更新: 如果我从项目构建一个 WAR 文件,然后将那个 WAR 作为一个新的动态 Web 项目导入,它 运行很好,没有错误。所以,这里显然还有其他问题,可能与 Gradle.

有关

更新2: 堆栈跟踪:

SEVERE: Servlet [Faces Servlet] in web application [/com.foo.bar] threw load() exception
java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:540)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:521)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1032)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:971)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4765)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5075)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:353)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:493)

构建路径:

Class 位置:

将所需库的 "implementation" 更改为 "compile" 解决了该问题。

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * user guide available at https://docs.gradle.org/3.5/userguide/java_library_plugin.html
 */

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:21.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    //Jersey
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.26'
    compile 'org.glassfish.jersey.inject:jersey-hk2:2.26'
    compile 'org.glassfish.jersey.core:jersey-common:2.26'

    //JAXB
    compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.26'

    //Javax Servlet
    compile 'javax.servlet:javax.servlet-api:3.0.1'

    //JSF
    compile 'org.glassfish:javax.faces:2.2.16'

    compile 'javax.inject:javax.inject:1'

    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.25.1'
}

eclipse {
    wtp {
        facet {

            facets = []
            facet name: "java", version: "1.8"          // Java version
            facet name: "jst.web", version: "3.0"       // Dynamic Web Application
            facet name: "jst.jsf", version: "2.2"       // Java Server Faces
            facet name: "wst.jsdt.web", version: "1.0"  // JavaScript
        }
    }
}

project.webAppDirName='WebContent'