Gradle JavaEE:使用 Angular 部署时出现 ClassNotFoundException

Gradle JavaEE: ClassNotFoundException when deploying with Angular

我有三个 gradle 模块:一个 JavaEE API、一个 Angular 前端和一个 Gradle 将这两个模块打包到一个 EAR 中的项目。

将两者之一打包到 EAR 中,一切正常(API 或 FrontEnd 运行 都可以)

一旦我将两者(API 和前端)打包到 EAR 中,我在部署时得到 ClassNotFoundExceptions:

09-Nov-2017 16:44:07.180 WARNUNG [http-nio-8080-exec-4] org.apache.openejb.server.rest.RESTService.appProviders can't load 'de.my.app.application.appRequestFloodingFilter'
 java.lang.ClassNotFoundException: de.my.app.application.appRequestFloodingFilter
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    at org.apache.tomee.catalina.TomEEWebappClassLoader.loadWithDelegate(TomEEWebappClassLoader.java:211)
    at org.apache.tomee.catalina.TomEEWebappClassLoader.loadClass(TomEEWebappClassLoader.java:201)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    at org.apache.openejb.server.rest.RESTService.appProviders(RESTService.java:567)
    at org.apache.openejb.server.rest.RESTService.addAppProvidersIfNeeded(RESTService.java:296)
    at org.apache.openejb.server.rest.RESTService.afterApplicationCreated(RESTService.java:132)
    at org.apache.tomee.webservices.TomeeJaxRsService.afterApplicationCreated(TomeeJaxRsService.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.apache.openejb.observer.ObserverManager$MethodInvocation.invoke(ObserverManager.java:406)
    at org.apache.openejb.observer.ObserverManager.doFire(ObserverManager.java:111)
    at org.apache.openejb.observer.ObserverManager.fireEvent(ObserverManager.java:100)
    at org.apache.openejb.loader.SystemInstance.fireEvent(SystemInstance.java:217)
    at org.apache.tomee.catalina.TomcatWebAppBuilder.afterStart(TomcatWebAppBuilder.java:1740)
    at org.apache.tomee.catalina.GlobalListenerSupport.lifecycleEvent(GlobalListenerSupport.java:116)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:395)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.tomee.catalina.TomcatWebAppBuilder.deployWar(TomcatWebAppBuilder.java:651)
    at org.apache.tomee.catalina.TomcatWebAppBuilder.deployWebApps(TomcatWebAppBuilder.java:589)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:996)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:717)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:713)
    at org.apache.openejb.assembler.DeployerEjb.deploy(DeployerEjb.java:254)
    at org.apache.openejb.assembler.DeployerEjb.deploy(DeployerEjb.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    at org.apache.openejb.security.internal.InternalSecurityInterceptor.invoke(InternalSecurityInterceptor.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:181)
    at org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85)
    at org.apache.openejb.core.singleton.SingletonContainer._invoke(SingletonContainer.java:256)
    at org.apache.openejb.core.singleton.SingletonContainer.invoke(SingletonContainer.java:212)
    at org.apache.openejb.server.ejbd.EjbRequestHandler.doEjbObject_BUSINESS_METHOD(EjbRequestHandler.java:371)
    at org.apache.openejb.server.ejbd.EjbRequestHandler.processRequest(EjbRequestHandler.java:182)
    at org.apache.openejb.server.ejbd.EjbDaemon.processEjbRequest(EjbDaemon.java:360)
    at org.apache.openejb.server.ejbd.EjbDaemon.service(EjbDaemon.java:247)
    at org.apache.openejb.server.ejbd.EjbServer.service(EjbServer.java:104)
    at org.apache.openejb.server.httpd.ServerServlet.service(ServerServlet.java:60)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
    at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.tomee.catalina.OpenEJBSecurityListener$RequestCapturer.invoke(OpenEJBSecurityListener.java:97)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    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)

简而言之:

  1. 注释掉 API 的依赖项并注释掉 APIs webModule == WORK
  2. 注释掉 FrontEnd 的依赖项并注释掉 FrontEnds webModule == WORK
  3. 在两个依赖项中注释,在两个 webModule == ClassNotFound 中注释

我读了很多书,尝试了很多书,但现在我不知道该寻找什么。

EAR 部署处理器

ear {
    deploymentDescriptor {
        version = "6"
        initializeInOrder = true

        applicationName = "My-App"
        displayName = "My-App"


        libDirName = "/lib"

        // WARs
        webModule(project(':MyAppClient').name + '-' + project(':MyAppClient').version + '.war', "/client/client")
        webModule(project(':MyAppServerAPI').name + '-' + project(':MyAppServerAPI').version + '.war', "/api/api")
    }
}

API 依赖关系

dependencies {
  //////////////////////////////////////
  // Third party dependencies
  //////////////////////////////////////

  // Logging
  compile 'org.slf4j:slf4j-api'

  // JavaEE
  compileOnly 'javax:javaee-api'

  // Jackson Json
  compile 'com.fasterxml.jackson.core:jackson-core'
  compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider'

  //apache commons
  compile group: 'org.apache.commons', name: 'commons-lang3'
}

EAR 的 application.xml

<?xml version="1.0"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="6">
  <application-name>My-App</application-name>
  <display-name>My-App</display-name>
  <initialize-in-order>true</initialize-in-order>
  <module>
    <web>
      <web-uri>MyAppClient-0.1.0-SNAPSHOT.war</web-uri>
      <context-root>/</context-root>
    </web>
  </module>
  <module>
    <web>
      <web-uri>MyAppServerAPI-0.1.0-SNAPSHOT.war</web-uri>
      <context-root>/api</context-root>
    </web>
  </module>
  <library-directory>/lib</library-directory>
</application>

2017 年 11 月 20 日更新

问题不属于Angular。在另一个用例中,我有一个 WAR 依赖于项目的另一个模块,JavaEE 和类似的问题发生在从模块 A 部署 WAR 并依赖于模块 B

我的错:我有两个包含 JAVA 代码和路由的 WAR。所以这两个文件都有一个 web.xml 文件。我(无论如何)在创建第一个模块时明确设置了一个 web-app:id 。拆分成两个项目时,我设置了一个新的display-name,但没有改变web-app:id

现在我有了这个:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-name>My Module 1 API</display-name>
</web-app>

还有那个:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-name>My Module 2 API</display-name>
</web-app>