2011-10-04 62 views
0

使用Maven 3.0.3构建使用JAXB(2.2.4-1)和CXF 2.4.2的Web服务失败,并显示以下错误:带注释的JAXB 2.2.4-1 Web服务的Maven构建失败,出现JDK 1.6

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 

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

源代码在Eclipse中很好地构建,它似乎将依赖关系放在JDK类之前。

我已经在pom.xml皮卡的JDK类前JAXB的API jar文件使用compilerArguments,要么bootclasspath-Xbootclasspath/p:尝试过,但似乎都不当我建立与mvn -X clean compile使用命令行选项,包括它。我也试着改变它使用的maven-compiler-plugin版本,但是从-X它没有改变它。

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

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:

<?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中:

<?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:

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(); 
     } 
    } 
} 

回答

2

这是一个错字。插件artifactId的末尾有一个>。在班前预先考虑的JAXB罐子正确的代码(替换在pom.xml插件部分与这个问题):

<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> 

有可能是一个更好的方式来确定正确的罐子预先设置bootclasspath,但这至少起作用。

谢谢!