2011-08-31 105 views
14

我想基本上從我的模型類生成XSD架構使用Maven插件在JAXB中註釋。下面是我的POM相關代碼:在Maven中從JAXB類型生成XSD模式?

<plugin> 
    <groupId>com.sun.tools.jxc.maven2</groupId> 
    <artifactId>maven-jaxb-schemagen-plugin</artifactId> 
    <version>1.2</version> 
    <executions> 
     <execution> 
      <phase>generate-resources</phase> 
      <goals> 
       <goal>generate</goal> 
      </goals> 
     </execution> 
    </executions> 

    <configuration> 
     <project>${project}</project> 
     <destdir>${project.build.directory}/schemas</destdir> 
     <srcdir>${project.build.sourceDirectory}/com/floodstream/wowza/mastercontrol/model</srcdir> 
     <verbose>true</verbose> 
    </configuration> 

    <dependencies> 
     <dependency> 
      <groupId>javax.xml.bind</groupId> 
      <artifactId>jaxb-api</artifactId> 
      <version>2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.xml.bind</groupId> 
      <artifactId>jaxb-xjc</artifactId> 
      <version>2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.xml.bind</groupId> 
      <artifactId>jaxb-impl</artifactId> 
      <version>2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.xml.bind</groupId> 
      <artifactId>jaxb-xjc</artifactId> 
      <version>2.2</version> 
     </dependency> 
    </dependencies> 
</plugin> 

<!-- other stuff omitted for brevity --> 

<pluginRepository> 
    <id>maven-repository.dev.java.net</id> 
    <name>Java.net Maven 2 Repository</name> 
    <url>http://download.java.net/maven/2</url> 
</pluginRepository> 

我收到以下錯誤:

[ERROR] FATAL ERROR 
[INFO] ------------------------------------------------------------------------ 
[INFO] org/apache/tools/ant/taskdefs/Javac 
org.apache.tools.ant.taskdefs.Javac 
[INFO] ------------------------------------------------------------------------ 
[INFO] Trace 
java.lang.NoClassDefFoundError: org/apache/tools/ant/taskdefs/Javac 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:634) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) 
     at java.net.URLClassLoader.access$000(URLClassLoader.java:73) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:212) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:205) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:321) 
     at org.codehaus.classworlds.RealmClassLoader.loadClassDirect(RealmClassLoader.java:195) 
     at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:255) 
     at org.codehaus.classworlds.RealmClassLoader.loadClass(RealmClassLoader.java:214) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:634) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) 
     at java.net.URLClassLoader.access$000(URLClassLoader.java:73) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:212) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:205) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:321) 
     at org.codehaus.classworlds.RealmClassLoader.loadClassDirect(RealmClassLoader.java:195) 
     at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:255) 
     at org.codehaus.classworlds.RealmClassLoader.loadClass(RealmClassLoader.java:214) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:634) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) 
     at java.net.URLClassLoader.access$000(URLClassLoader.java:73) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:212) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:205) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:321) 
     at org.codehaus.classworlds.RealmClassLoader.loadClassDirect(RealmClassLoader.java:195) 
     at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:255) 
     at org.codehaus.classworlds.RealmClassLoader.loadClass(RealmClassLoader.java:214) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
     at com.sun.tools.jxc.maven2.SchemaGenMojo.execute(SchemaGenMojo.java:143) 
     at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490) 
     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694) 
     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556) 
     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535) 
     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387) 
     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348) 
     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180) 
     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328) 
     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138) 
     at org.apache.maven.cli.MavenCli.main(MavenCli.java:362) 
     at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:616) 
     at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) 
     at org.codehaus.classworlds.Launcher.launch(Launcher.java:255) 
     at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) 
     at org.codehaus.classworlds.Launcher.main(Launcher.java:375) 
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.taskdefs.Javac 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:217) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:205) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:321) 
     at org.codehaus.classworlds.RealmClassLoader.loadClassDirect(RealmClassLoader.java:195) 
     at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:255) 
     at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:274) 
     at org.codehaus.classworlds.RealmClassLoader.loadClass(RealmClassLoader.java:214) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
     ... 59 more 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 4 seconds 
[INFO] Finished at: Tue Aug 30 17:40:29 PDT 2011 
[INFO] Final Memory: 16M/170M 
[INFO] ------------------------------------------------------------------------ 
+1

檢查相關的帖子。我認爲jaxb有兩個插件,其中一個比另一個(輕描淡寫)好得多。我認爲正確的在artifact-id中有「jaxb2」。 –

回答

25

這裏的勝利:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>jaxb2-maven-plugin</artifactId> 
    <version>1.3.1</version> 
    <executions> 
     <execution> 
      <goals> 
       <goal>schemagen</goal> 
      </goals> 
      <phase>generate-resources</phase> 
      <configuration> 
       <includes> 
        <include>com/myproject/model/*.java</include> 
       </includes> 
       <outputDirectory>${project.build.directory}/schemas</outputDirectory> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
+1

由於'schemagen'的功能正在被'javac'取代,那裏的方法是什麼?只需將javac與相應的標誌添加到目標中? – mike

0

提高對Naftuli Tzvi Kay's answer,我發現它使用起來更加方便jaxb2-maven-plugin的當前版本2.2,如本示例中所示(因爲2.x版本需要稍微不同的配置):

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>jaxb2-maven-plugin</artifactId> 
    <version>2.2</version> 
    <executions> 
     <execution> 
      <goals> 
       <goal>schemagen</goal> 
      </goals> 
      <phase>generate-resources</phase> 
      <configuration> 
       <sources> 
        <source>src/main/java/com/foo/model/xml</source> 
       </sources> 
       <outputDirectory>${project.build.directory}/schemas</outputDirectory> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

查看plugin documentation瞭解更多詳情。