使用JDK 1.6的Maven构建的带注释的JAXB 2.2.4-1 Web服务失败 [英] Maven build of annotated JAXB 2.2.4-1 Web Service fails with JDK 1.6

查看:66
本文介绍了使用JDK 1.6的Maven构建的带注释的JAXB 2.2.4-1 Web服务失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Maven 3.0.3使用JAXB(2.2.4-1)和CXF 2.4.2构建Web服务失败,并出现以下错误:

Using Maven 3.0.3 to build a web service using JAXB (2.2.4-1) and CXF 2.4.2 fails with the following error:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project cxfmaventest: Compilation failure  
/Users/ksuda/Documents/workspace/cxfmaventest/src/main/java/cxfmaventest/CXFMavenTest.java:[18,83] annotation type not applicable to this kind of declaration  

失败的行包含JAXB的 @XMLElement(required = true)允许在方法参数上。 JDK 1.6包含以前版本的@XMLElement,不允许在方法参数上。这意味着它是一个路径问题,它从JDK的bootclasspath中找到@XMLElement,而不是在查找JDK类之前先在依赖项列表中找到它。

The line that fails contains an @XMLElement(required = true) that by JAXB is allowed to be on a method argument. JDK 1.6 includes a previous version of @XMLElement that is not allowed to be on a method argument. This means it is a pathing issue with it finding the @XMLElement from the JDK's bootclasspath instead of finding it first in the dependencies list before looking in the JDK classes.

源代码文件在Eclipse中构建得很好,它似乎将依赖项放在JDK类之前。

The source files build fine in Eclipse, where it seems to put the dependencies before the JDK classes.

我尝试使用 compilerArguments bootclasspath -Xbootclasspath / p:在pom.xml中获取jaxb-api jar文件之前jdk类,但是当我使用 mvn -X clean compile 构建时,似乎都没有将它包含在命令行选项中。我也试图改变它使用的maven-compiler-plugin版本,但是从 -X 它没有改变它。

I have tried using compilerArguments and either bootclasspath or -Xbootclasspath/p: in the pom.xml to pickup the jaxb-api jar file before the jdk classes, but neither seems to include it with the command line options when I build with mvn -X clean compile. I also tried to change which maven-compiler-plugin version it used, but from the -X it didn't change it.

我创建了一个精简项目来演示错误。您应该只需要前两个文件进行复制。其他文件作为工作示例包含在内,假设构建工作使用独立的jetty Web服务和spring公开Web服务。

I created a stripped down project to demonstrate the error. You should only need the first two files to replicate. The other files are included as a working sample assuming the build works that exposes a web service using a standalone jetty web service and spring.

CXFMavenText.java:

CXFMavenText.java:

package cxfmaventest;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlElement;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
@WebService(targetNamespace = "http:/cxfmaventest/", name = "CXFMavenTest")
@Service("cxfmaventestws")
public class CXFMavenTest extends SpringBeanAutowiringSupport {

    @WebMethod
    @WebResult(name = "test")
    public String getHelloWorld(@WebParam(mode = WebParam.Mode.IN, name = "mode") @XmlElement(required = true) String mode) throws Exception {
        return "Hello World";
    }
}

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>cxfmaventest</groupId>
    <artifactId>cxfmaventest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>cxfmaventest</name>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.2.4-1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.4-1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-bundle</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>7.4.2.v20110526</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin></artifactId>
        <version>2.3.2</version>
        <configuration>
          <compilerVersion>1.6</compilerVersion>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin></artifactId>
                <version>2.8</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

applicationContet.xml:

applicationContet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <context:component-scan base-package="cxfmaventest" />

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <jaxws:endpoint id="ws" implementor="#cxfmaventestws" address="/">
        <jaxws:features>
            <!-- <bean class="org.apache.cxf.feature.LoggingFeature"/> -->
        </jaxws:features>
    </jaxws:endpoint>

    <context:annotation-config />

</beans>

WEB-INF / web.xml:

WEB-INF/web.xml:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>CXFMavenTest</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>

CXFMavenTextStandalone.java:

CXFMavenTextStandalone.java:

package cxfmaventest;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class CXFMavenTestStandalone {

    public static void main(String[] args) {
        try {
            String root = ".";
            String port = "8080";
            for (int x = 0; x < args.length; ++x) {
                if (args[x].startsWith("-root=")) {
                    root = args[x].substring(6);
                } else if (args[x].startsWith("-port=")) {
                    port = args[x].substring(6);
                } else {
                    System.out.println("Arguments:");
                    System.out.println(" -root=<path> - Set the root path for the webapp files. Defaults to the current directory.");
                    System.out.println(" -port=<port#> - Set the port number to run the server on. Defaults to port 8080.");
                    System.exit(1);
                }
            }
            Server server = new Server(Integer.parseInt(port));
            WebAppContext context = new WebAppContext();
            context.setDescriptor(root + "/WEB-INF/web.xml");
            context.setResourceBase(root);
            context.setContextPath("/cxfmaventest");
            context.setParentLoaderPriority(true);
            server.setHandler(context);
            server.start();
            server.join();
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
}


推荐答案

这是一个错字。在插件artifactId的末尾有一个> 。在类之前添加JAXB jar的正确代码是(用这个替换问题中POM.XML中的插件部分):

It is a typo. There is an > at the end of the plugin artifactId. The correct code for prepending the JAXB jar before the classes is (replace the plugin section in the POM.XML in the question with this):

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
      <compilerArgument>-Xbootclasspath/p:${settings.localRepository}/javax/xml/bind/jaxb-api/2.2.3/jaxb-api-2.2.3.jar</compilerArgument>
      <compilerVersion>1.6</compilerVersion>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>

可能有更好的方法来识别正确的jar来预先添加bootclasspath,但至少这个工作。

There is probably a better way to identify the correct jar to prepend the bootclasspath with, but this at least works.

谢谢!

这篇关于使用JDK 1.6的Maven构建的带注释的JAXB 2.2.4-1 Web服务失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆