如何通过Maven正确安装和配置JSF库?

时间:2011-11-10 14:41:29

标签: maven jsf tomcat configuration installation

我正在尝试将基于JSF的应用程序部署到Tomcat 6.我的构建系统的设置方式,WAR本身没有任何库,因为该服务器共提供43个应用程序。而是将库复制到共享库文件夹中并在应用程序之间共享。部署时,我收到此错误

SEVERE: Error deploying configuration descriptor SSOAdmin.xml
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1667)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:108)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:58)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:297)
    at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:1078)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:261)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4611)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:675)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:601)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

现在在我的研究中,我发现这应该通过下载JSF源代码并自己编译来解决。在我的案例中,这是一个可怕的解决方案。这会给我的团队带来巨大的问题,我们必须应对各种配置。还有其他解决办法吗?

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nms.sso</groupId>
  <artifactId>SSOAdmin</artifactId>
  <version>09142011-BETA</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
      <groupId>asm</groupId>
      <artifactId>asm</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <!-- <dependency> -->
    <!-- <groupId>com.sun.faces</groupId> -->
    <!-- <artifactId>jsf-api</artifactId> -->
    <!-- <scope>${myExeScope}</scope> -->
    <!-- </dependency> -->
    <!-- <dependency> -->
    <!-- <groupId>com.sun.faces</groupId> -->
    <!-- <artifactId>jsf-impl</artifactId> -->
    <!-- <scope>${myExeScope}</scope> -->
    <!-- </dependency> -->
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.faces</groupId>
      <artifactId>javax.faces-api</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>net.sf.jt400</groupId>
      <artifactId>jt400</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>nmsc</groupId>
      <artifactId>nmsc_api</artifactId>
      <version>09142011-BETA</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.icefaces</groupId>
      <artifactId>icefaces</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.icefaces</groupId>
      <artifactId>icefaces-ace</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.icefaces</groupId>
      <artifactId>icefaces-compat</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.jibx</groupId>
      <artifactId>jibx-extras</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.jibx</groupId>
      <artifactId>jibx-run</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
    <dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <scope>${myExeScope}</scope>
    </dependency>
  </dependencies>
  <parent>
    <groupId>nmsc</groupId>
    <artifactId>nmsc_lib</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../libs</relativePath>
  </parent>
  <build>
    <finalName>SSOAdmin</finalName>
  </build>
  <name>SSOAdmin Maven Webapp</name>
</project>

这里必须有一个解决方案。我不能一秒钟相信,为JSF分发的Maven只能用于编译,不适合部署。

1 个答案:

答案 0 :(得分:28)

当你面临一个“奇怪的”异常时,表明类/方法/文件/组件/标签不存在或不同,而它们似乎明确地包含在Web应用程序中,如下面的那些,

  

java.lang.ClassFormatError:在类文件javax / faces / webapp / FacesServlet中非本地或抽象的方法中的Absent Code属性

  

java.util.MissingResourceException:找不到javax.faces.LogStrings包

  

com.sun.faces.vendor.WebContainerInjectionProvider无法强制转换为com.sun.faces.spi.InjectionProvider

  

com.sun.faces.config.ConfigurationException:CONFIGURATION FAILED

  

来自命名空间http://xmlns.jcp.org/jsf/html的名为inputFile的标记已定义了null处理程序类。

  

javax.faces.CurrentThreadToServletContext.getFallbackFactory上的java.lang.NullPointerException

  

javax.faces.application.ViewHandlerWrapper.getWebsocketURL上的java.lang.AbstractMethodError

或当你面临“奇怪的”运行时行为时,例如破坏的HTTP会话(jsessionid出现在所有地方的链接URL中),和/或破坏的JSF视图范围(它表现为请求作用域),以及/或者破坏了CSS / JS /图像资源,那么webapp的运行时类路径被重复的不同版本化JAR文件污染的可能性很大。

ClassFormatError上使用FacesServlet的特定情况下,这意味着第一次找到包含所提到的类的JAR文件实际上是一个“蓝图”API JAR文件,意图对于实现供应商(例如为Mojarra和MyFaces工作的开发人员)。它包含只包含类和方法签名的类文件,没有任何代码体和资源文件。这正是“缺席代码属性”的意思。它纯粹是为了javadocs和编译。

始终将服务器提供的库标记为provided

Maven中标记为“Java Specifications”并且工件ID中带有-api后缀的所有依赖项都是那些蓝图API。您绝对不应该在运行时类路径中拥有它们。如果你真的需要把它放在你的pom中,你应该总是标记它们<scope>provided</scope>。一个众所周知的例子是Java EE (Web) API

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version><!-- 6.0 or 7.0 or newer --></version>
    <scope>provided</scope>
</dependency>

如果provided范围不存在,则此JAR将以webapp /WEB-INF/lib结束,从而导致您现在面临的所有问题。此JAR还包含蓝图类FacesServlet

在您的特定情况下,您有一个不必要的JSF API依赖项:

<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-api</artifactId>
</dependency>

这会导致问题,因为它包含蓝图类FacesServlet。如上所示,删除它并依赖provided Java EE(Web)API应解决它。

Tomcat作为一个准系统的JSP / Servlet容器已经提供了JSP,Servlet和EL(以及8也是WebSocket)。因此,您应至少将jsp-apiservlet-apiel-api标记为provided。 Tomcat只提供了JSF(和JSTL)。所以你需要通过webapp安装它。

完整的Java EE服务器,如WildFly,TomEE,GlassFish,Payara,WebSphere等已经提供了整个Java EE API,包括JSF。所以你绝对不需要通过webapp安装JSF。如果服务器已经提供了不同的实现和/或版本,它只会导致冲突。您需要的唯一依赖是完全如上所示的javaee-web-api

在Tomcat上安装JSF

our JSF wiki - Installing JSF中提到了在Tomcat中安装JSF的正确方法。有两个JSF实现,Mojarra and MyFaces。您应该选择安装其中一个,因此

在Tomcat上安装Mojarra:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.faces</artifactId>
    <version><!-- Check https://javaserverfaces.github.io --></version>
</dependency>

您还可以查看org.glassfish:javax.faces repository以获取当前最新发布的版本(当前为2.2.13)。另请参阅Mojarra's own installation instructions

在Tomcat上安装MyFaces:

<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-api</artifactId>
    <version><!-- Check http://myfaces.apache.org --></version>
</dependency>
<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-impl</artifactId>
    <version><!-- Check http://myfaces.apache.org --></version>
</dependency>

您还可以查看org.apache.myfaces.core:myfaces-bundle repository以获取当前最新发布的版本(目前为2.2.10)。

请注意,Tomcat 6作为Servlet 2.5容器支持max JSF 2.1。顺便说一下,别忘了安装JSTL。另请参阅our JSF wiki - Installing JSF

另见: