【问题标题】:Maven Jaxb2 xjc plugin error No schemas have been foundMaven Jaxb2 xjc 插件错误没有找到模式
【发布时间】:2013-07-04 13:12:20
【问题描述】:

这些天我花了一些时间在 JAXB 上将 XSD 转换为 Java 类,反之亦然。这是一个非常适合初学者的教程,http://www.journaldev.com/1312/how-to-generate-java-classes-from-xsd-using-xjc-maven-plugin。我严格按照步骤操作,但mvn clean install时总是出错

这是我的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插件。那么所有这些插件都是为了相同的目的而生成的吗?如果是这样,为什么?

提前致谢!

【问题讨论】:

    标签: maven xsd jaxb2


    【解决方案1】:

    由于您没有提供任何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>
    

    【讨论】:

    • 尝试删除以下行:target/generated-sources/jaxb
    【解决方案2】:

    尝试确保 jaxb.xsd 存在于 src/main/resources 下,插件正在警告,因为它无法在指定位置找到方案.

    【讨论】:

    • jaxb.xsd 究竟是什么?是文件名吗?
    • @zygimantus 是的,这是您要生成 Java 对象类的模式
    【解决方案3】:

    我们可以在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      相关资源
      最近更新 更多