2013-07-04 68 views
10

這些天我花了一段時間在JAXB上將XSD轉換爲Java類,反之亦然。這是一個非常好的初學者教程,http://www.journaldev.com/1312/how-to-generate-java-classes-from-xsd-using-xjc-maven-plugin。我嚴格按照步驟操作,但當mvn clean installMaven Jaxb2 xjc插件錯誤沒有模式被發現

這裏是我的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>jd</groupId> 
    <artifactId>jd</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 


    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 


    <build> 
     <plugins> 
      <!-- Plugin required to build java classes from XSD using XJC --> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>jaxb2-maven-plugin</artifactId> 
       <version>1.5</version> 
       <executions> 
        <execution> 
         <id>xjc</id> 
         <goals> 
          <goal>xjc</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <!-- The name of your generated source package --> 
        <arguments>-extension -npa -b ${project.basedir}/src/main/java/com/moodys/jaxb/global.xjb</arguments> 
       </configuration> 
      </plugin> 

     </plugins> 
    </build> 

</project> 

但是當我鍵入mvn clean install,它總是給我的錯誤如下:

C:\Users\congy\Desktop\Work\workspace\JaxbFromClass>mvn clean jaxb2:xjc 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building jd 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ jd --- 
[INFO] Deleting C:\Users\congy\Desktop\Work\workspace\JaxbFromClass\target 
[INFO] 
[INFO] --- jaxb2-maven-plugin:1.5:xjc (default-cli) @ jd --- 
[INFO] Generating source... 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 0.487s 
[INFO] Finished at: Thu Jul 04 19:09:37 CST 2013 
[INFO] Final Memory: 4M/122M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:1.5:xjc (default-cli) on project jd: No schemas have been found -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException 

誰能告訴我原因,或簡單地告訴我,我應該指的進一步的信息?

另一個問題是:根據這個問題Difference of Maven JAXB plugins,至少有三個jaxb插件。所有這些插件都是爲了同樣的目的而生成的?如果是這樣,爲什麼?

在此先感謝!

回答

13

由於您沒有提供任何schemaDirectory,插件正嘗試從the default schema directory中的所有XML模式文件生成Java源文件。你應該根據documentation配置插件:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>jaxb2-maven-plugin</artifactId> 
    <version>1.5</version> 
    <executions> 
    <execution> 
     <id>id1</id> 
     <goals> 
    <goal>xjc</goal> 
     </goals> 
     <configuration> 
     <outputDirectory>target/generated-sources/jaxb</outputDirectory> 
     <packageName>com.your.package.jaxb</packageName> 
     <schemaDirectory>src/main/xsd</schemaDirectory> 
     <schemaFiles>jaxb.xsd</schemaFiles> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
+0

這樣做,作爲下面使用。仍然給出同樣的錯誤。 – jamie

+0

嘗試刪除以下行: target/generated-sources/jaxb BlueLettuce16

2

儘量確保jaxb.xsd的src/main /資源存在,這個插件是華林,因爲它coudn't找到方案在指定的位置。

+0

實際上是什麼jaxb.xsd?它是一個文件名嗎? – zygimantus

0

我們可以在pom.xml文件

<plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>jaxb2-maven-plugin</artifactId> 
     <version>1.5</version> 
     <executions> 
      <execution> 
       <id>id1</id> 
       <goals> 
        <goal>xjc</goal> 
       </goals> 
       <configuration> 
        <outputDirectory>src/main/java</outputDirectory> 
        <clearOutputDir>false</clearOutputDir> 
        <packageName>com.subu.xsd.model</packageName> 
        <schemaDirectory>src/main/java/schemadir</schemaDirectory> 
        <schemaFiles>XYZ.xsd</schemaFiles> 
       </configuration> 
      </execution> 
     </executions> 
    </plugin>