4

我有一个采用以下结构的多模块 Maven 项目:

root-module
    |__module-a
    |    |__src
    |        |__main
    |            |__xsd
    |            |    |__my.xsd
    |            |__xjb
    |                 |__my.xjb
    |__module-b

根模块的 POM 只是聚合了模块 a 和 b(除其他外):

<project>
  <artifactId>root-module</artifactId>
  <packaging>pom</packaging>
  <modules>
    <module>module-a</module>
    <module>module-b</module>
  </modules>
</project>

模块 a 的 POM 如下(除其他外):

<project>
  <parent>
    <artifactId>root-module</artifactId>
  </parent>
  <artifactId>module-a</artifactId>
  <properties>
    <my-definitions.xsd>${basedir}/src/main/xsd/my.xsd</my-definitions.xsd>
    <my-bindings.xjb>${basedir}/src/main/xjb/my.xjb</my-bindings.xjb>
    <my.output>${basedir}/target/generated-sources/jaxb/my</my.output>
  </properties>
  <build>
    <plugins>
        <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-my-classes</id>
                        <phase>generate-sources</phase>
                        <goals><goal>xjc</goal></goals>
                        <configuration>
                            <sources><source>${my-definitions.xsd}</source></sources>
                            <xjbSources><xjbSource>${my-bindings.xjb}</xjbSource></xjbSources>
                            <outputDirectory>${my.output}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
        </plugin>
    </plugins>
  </build>
</project>

因此,当我在 module-a 上运行 mvn 时,一切正常并且构建成功。但是当我在根模块运行它时,我从 XJC 插件中得到一个异常,它试图在根模块下找到绑定文件:

com.sun.istack.SAXParseException2; IOException thrown when processing "file:/home/root-module/src/main/xjb/my.xjb". Exception: java.io.FileNotFoundException: /home/root-module/src/main/xjb/my.xjb (The system cannot find the path specified).

有趣的是,它能够正确定位 XSD:

[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.1:xjc (generate-my-classe) on project module-a:
[ERROR] +=================== [XJC Error]
[ERROR] |
[ERROR] | 0: file:/home/root-module/module-a/src/main/xsd/my.xsd
[ERROR] |
[ERROR] +=================== [End XJC Error]
  • 有什么线索吗?
  • 这是构建脚本中的配置问题吗?

我的构建系统的细节:

Using Maven 3.2.5

<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.1</version>

从这里参考 JAXB2 Maven 插件文档。还搜索了一些关于 SO 的相关问题,但它们没有在任何地方解释我的具体问题。

更新:看起来像一个未解决的问题。如果有解决方法,请保持线程打开。

4

2 回答 2

3

更新到插件的 2.2 版似乎可以工作。

  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>2.2</version>

我在使用 2.1 版插件时遇到了同样的问题。只需更改为 2.2 版即可解决此问题。

于 2016-01-27T17:41:46.090 回答
0

在插件解决方案可用之前,我正在使用以下 ant-run hack:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <id>generate-my-classes</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                    <tasks>
                        <mkdir dir="${project.build.directory}/generated-sources/jaxb/my" />
                        <exec executable="${env.JAVA_HOME}/bin/xjc.exe" dir="${project.basedir}/src/main/xsd">
                            <arg value="-p" />
                            <arg value="my.package" />
                            <arg value="-b" />
                            <arg value="${project.basedir}/src/main/xjb" />
                            <arg value="-d" />
                            <arg value="${project.build.directory}/generated-sources/jaxb" />
                            <arg value="." />
                        </exec>
                    </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>

更新:

Github上的讨论。

考虑将Apache CXF Utils作为替代方案。

于 2015-06-15T04:32:23.527 回答