Web应用jetty 9 maven插件正确运行,但不适用于jetty runner

时间:2014-11-22 13:36:43

标签: jsf-2 jetty embedded-jetty

作为Maven的新手,我正在尝试用嵌入式码头制作一个可执行jar的webapp 。不幸的是,我在实现这一目标方面遇到了很多困难,并决定按照一步一步的进展:首先使用jetty-maven-plugin(好的,有很多痛苦),然后使用jetty-runner(KO,这个问题)这个SO问题),最后使用嵌入式码头(KO)。

我花了这么多时间做这件事,根据码头版本有不同的例外情况并抽出那么多头发,我终于决定向社区寻求帮助。

我希望这个问题不会被标记为SO #12241989(或任何其他类似的)的副本,因为我有同样的问题,但显然不是因为我使用了相同版本的jetty-maven-plugin和jetty-runner。

上下文

webapp使用JSF2(mojarra 2.2.8)+ CDI(焊接)+ Primefaces + Websockets

配置

1。 Jetty插件

摘自pom.xml

     <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.7</java.version>
            <jetty.groupId>org.eclipse.jetty</jetty.groupId>
            <jetty.version>9.0.4.v20130625</jetty.version>
            <jetty.descriptor>${project.basedir}/src/main/webapp/WEB-INF/web.xml</jetty.descriptor>
            <jetty.env>${project.basedir}/src/main/webapp/WEB-INF/jetty-env.xml</jetty.env>
            <jetty.contextPath>/</jetty.contextPath>
            <jetty.overrideDescriptor>${project.basedir}/src/main/webapp/WEB-INF/jetty-web-override.xml</jetty.overrideDescriptor>
            <myfaces.version>2.2.5</myfaces.version>
            <mojarra.version>2.2.8</mojarra.version>
    </properties>
    ...
    <plugin>
            <groupId>${jetty.groupId}</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty.version}</version>
            <configuration>
                <webApp>
                    <contextPath>${jetty.contextPath}</contextPath>
                    <descriptor>${jetty.descriptor}</descriptor>
                    <jettyEnvXml>${jetty.env}</jettyEnvXml>
                    <overrideDescriptor>${jetty.overrideDescriptor}</overrideDescriptor>
                </webApp>
                <contextXml>
                    ${project.basedir}/src/main/webapp/WEB-INF/jetty-context.xml
                </contextXml>
            </configuration>
        </plugin>

码头-context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
     <Set name="serverClasses">
        <Array type="java.lang.String">
            <Item>
                -org.eclipse.jetty.servlet.ServletContextHandler.Decorator
            </Item>
        </Array>
    </Set>
</Configure>

码头-env.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
 <Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">

 <New id="appManager" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>
            <Ref id="webAppCtx" />
        </Arg>
        <Arg>BeanManager</Arg>
        <Arg>
            <New class="javax.naming.Reference">
                <Arg>javax.enterprise.inject.spi.BeanManager</Arg>
                <Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
                <Arg />
            </New>
        </Arg>
    </New>
</Configure>

码头的web-Override.xml的

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
<!--    CDI-->
     <listener>
        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener>
    <resource-env-ref>
        <resource-env-ref-name>BeanManager</resource-env-ref-name>
        <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
    </resource-env-ref>
    <!-- mojarra -->
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

使用此配置,应用程序可以正常运行。 松了一口气

2。 Jetty跑步者

我相信使用jetty runner重现上述配置的最简单方法是使用描述我的webapp的jetty context xml文件。所以我最终得到了以下上下文文件:

context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war">target/myapp.war</Set>
  <Set name="descriptor">src/main/webapp/WEB-INF/web.xml</Set>
  <Set name="overrideDescriptor">src/main/webapp/WEB-INF/jetty-web-override.xml</Set>
  <Set name="serverClasses">
        <Array type="java.lang.String">
            <Item>
                -org.eclipse.jetty.servlet.ServletContextHandler.Decorator
            </Item>
        </Array>
  </Set>

  <New id="appManager" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>BeanManager</Arg>
        <Arg>
            <New class="javax.naming.Reference">
                <Arg>javax.enterprise.inject.spi.BeanManager</Arg>
                <Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
                <Arg />
            </New>
        </Arg>
    </New>

</Configure>

然后当我运行jetty runner(版本相同)时:

java -jar jetty-runner-9.0.4.v20130625.jar context.xml

......一切似乎都没问题,除了(烦人的)我的应用程序的启动监听器(用Servlet 3.0 @WebListener注释)无法启动。

如果我尝试导航到http://localhost:8080/,我会得到以下堆栈跟踪:

HTTP ERROR 500

   Problem accessing /. Reason:
    Server Error

  Caused by:

javax.faces.view.facelets.TagAttributeException: /index.xhtml @119,66 src="/WEB-INF/include/#{applicationManager.layout}.xhtml" /index.xhtml @119,66 src="/WEB-INF/include/#{appli
cationManager.layout}.xhtml": java.lang.NullPointerException
        at com.sun.faces.facelets.tag.TagAttributeImpl.getObject(TagAttributeImpl.java:358)
        at com.sun.faces.facelets.tag.TagAttributeImpl.getValue(TagAttributeImpl.java:322)
        at com.sun.faces.facelets.tag.ui.IncludeHandler.apply(IncludeHandler.java:112)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
        at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:203)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
        at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:203)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
        at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
        at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:203)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
        at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
        at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:203)
        at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
        at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
        at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
        at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
        at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:161)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:990)
        at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:698)
...
Caused by: javax.el.ELException: /index.xhtml @119,66 src="/WEB-INF/include/#{applicationManager.layout}.xhtml": java.lang.NullPointerException
        at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
        at com.sun.faces.facelets.tag.TagAttributeImpl.getObject(TagAttributeImpl.java:356)
        ... 60 more
Caused by: javax.el.ELException: java.lang.NullPointerException
        at javax.el.BeanELResolver.getValue(BeanELResolver.java:368)

如果我尝试通过在web.xml中指定来强制监听我的StartupListener类:

    <listener>
        <listener-class>fr.mygroup.myapp.listeners.StartupListener</listener-class>
    </listener>

......我得到同样的错误。

无论如何,我的StartupListener没有被监听的事实显然不是错误的原因,而是结果。但我无法弄清楚我的码头配置有什么问题。

我绝望地觉得去我的webapp的可执行文件überjar还有很长的路要走:(

附录

的pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>fr.mygroupid</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <jetty.groupId>org.eclipse.jetty</jetty.groupId>
        <jetty.version>9.0.4.v20130625</jetty.version>
        <jetty.descriptor>${project.basedir}/src/main/webapp/WEB-INF/web.xml</jetty.descriptor>
        <jetty.env>${project.basedir}/src/main/webapp/WEB-INF/jetty-env.xml</jetty.env>
        <jetty.contextPath>/</jetty.contextPath>
        <jetty.overrideDescriptor>${project.basedir}/src/main/webapp/WEB-INF/jetty-web-override.xml</jetty.overrideDescriptor>
        <myfaces.version>2.2.5</myfaces.version>
        <mojarra.version>2.2.8</mojarra.version>
    </properties>

    <build>
        <plugins>
<plugin>
                <groupId>${jetty.groupId}</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <webApp>
                        <contextPath>${jetty.contextPath}</contextPath>
                        <descriptor>${jetty.descriptor}</descriptor>
                        <jettyEnvXml>${jetty.env}</jettyEnvXml>
                        <overrideDescriptor>${jetty.overrideDescriptor}</overrideDescriptor>
                    </webApp>
                    <contextXml>
                        ${project.basedir}/src/main/webapp/WEB-INF/jetty-context.xml
                    </contextXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>
    <repositories>
        <repository>
            <id>ebi-repo</id>
            <name>The EBI internal repository</name>
            <url>http://www.ebi.ac.uk/~maven/m2repo</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>prime-repo</id>
            <name>PrimeFaces Maven Repository</name>
            <url>http://repository.primefaces.org</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>  

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${mojarra.version}</version>
            <scope>compile</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${mojarra.version}</version>
            <scope>compile</scope>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.1.2.Final</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Primefaces 5.1 -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.1</version>
        </dependency>
        <!-- Primefaces Themes -->
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.10</version>
        </dependency>
        <!-- Biomodels API -->
        <dependency>
            <groupId>uk.ac.ebi.biomodels</groupId>
            <artifactId>biomodels-wslib</artifactId>
            <version>1.21</version>
        </dependency>
        <!-- Ganymed SSH -->
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>262</version>
        </dependency>
        <!-- Atmosphere -->
        <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
            <version>2.2.3</version>
        </dependency>
        <!-- Logback -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <!-- GSON -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>
        <!-- Apache commons -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
    </dependencies>
</project>

0 个答案:

没有答案