Maven编译得很好,但是jar中生成的toString的cxf因NoClassDefFoundError而失败

时间:2012-06-06 19:25:58

标签: maven cxf

我添加了一个cxf插件,用于在所有创建的类上生成toString()。添加该方法并编译正常,但是当我将maven打包的jar添加到另一个项目中的构建路径并将一个对象添加到我收到的toString时:

java.lang.NoClassDefFoundError: org/apache/cxf/xjc/runtime/JAXBToStringStyle
    at com.resx.services.Profile.toString(Profile.java:420)
    at java.lang.String.valueOf(String.java:2826)
    ...
Caused by: java.lang.ClassNotFoundException: org.apache.cxf.xjc.runtime.JAXBToStringStyle

我假设我的pom设置错误,但我不确定。这是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>com.trx</groupId>
  <artifactId>resxWebservices</artifactId>
  <version>10.4.1</version>

  <name>resxWebservices</name>
  <packaging>jar</packaging>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>2.6.0</version>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
              <sourceRoot>src</sourceRoot>
              <wsdlOptions>
                <wsdlOption>
                  <wsdl>./resxWebservices.wsdl</wsdl>
                  <extraargs>
                    <extraarg>-xjc-Xts</extraarg>
                  </extraargs>
                </wsdlOption>
              </wsdlOptions>
            </configuration>
            <goals>
              <goal>wsdl2java</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.apache.cxf.xjc-utils</groupId>
            <artifactId>cxf-xjc-runtime</artifactId>
            <version>2.6.0</version>
          </dependency>
          <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
          </dependency>
          <dependency>
            <groupId>org.apache.cxf.xjcplugins</groupId>
            <artifactId>cxf-xjc-ts</artifactId>
            <version>2.6.0</version>
          </dependency>
        </dependencies>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <verbose>true</verbose>
          <fork>true</fork>
          <executable>${JAVA_HOME}/bin/javac</executable>
          <compilerVersion>1.6</compilerVersion>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.apache.cxf.xjc-utils</groupId>
      <artifactId>cxf-xjc-runtime</artifactId>
      <version>2.6.0</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf.xjcplugins</groupId>
      <artifactId>cxf-xjc-ts</artifactId>
      <version>2.6.0</version>
    </dependency>
  </dependencies>
</project>

我也尝试过添加

<scope>compile</scope>

到具有相同问题的依赖项。

1 个答案:

答案 0 :(得分:2)

当你运行其他项目时,你需要确保cxf jar在类路径上可用。

您应该将您的第一个项目列为第二个项目的依赖项,以确保正确的依赖项可以传递(而不是仅仅将jar本身添加到构建路径中)

或者,为包含cxf jar的第一个项目创建一个jar-with-dependencies,并将其添加到第二个项目的duild / runtime类路径