Maven jaxb2:xjc 无法生成代码

问题描述 投票:0回答:10

我在 pom.xml 中的 Maven 构建中添加了以下插件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration> 
                <extension>true</extension>                             
                <clearOutputDir>false</clearOutputDir>
                <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
                <schemaFiles>myapp.xsd</schemaFiles>                                                        
                <outputDirectory>${basedir}/src/main/java</outputDirectory>         
                <bindingDirectory>src/main/resources/xsd</bindingDirectory>     
                <bindingFiles>myapp-bindings.xjb</bindingFiles>
            </configuration>
        </execution>
    </executions>                       

</plugin>

以下是构建错误

[INFO] Ignored given or default xjbSources [C:\WorkSpace\MyApp\src\main\xjb], since it is not an existent file or directory.
[INFO] Ignored given or default sources [C:\WorkSpace\MyApp\src\main\xsd], since it is not an existent file or directory.
[WARNING] No XSD files found. Please check your plugin configuration.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.273s
[INFO] Finished at: Tue May 12 16:24:26 EDT 2015
[INFO] Final Memory: 9M/124M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "dev-artifactory" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.1:xjc (default) on project pml-jasypt-authentication-service: MojoExecutionException: NoSchemasException -> [Help 1]

我很困惑,为什么插件没有引用配置中指定的路径和文件。

maven build jaxb xjc
10个回答
22
投票

2.1 版改变了源的指定方式

http://mojo.codehaus.org/jaxb2-maven-plugin/xjc-mojo.html#sources

例如

<configuration>
...
  <sources>
     <source>some/explicit/relative/file.xsd</source>
     <source>/another/absolute/path/to/a/specification.xsd</source>
     <source>a/directory/holding/xsds</source>
 </sources>
</configuration>

我遇到了很多其他问题,所以按照 jshark 的建议坚持使用 1.6 是一个好计划


19
投票

2.1版本有一个bug

您可以使用

<version>2.2</version>
和新语法:

<configuration>
...
  <sources>
     <source>some/explicit/relative/file.xsd</source>
     <source>/another/absolute/path/to/a/specification.xsd</source>
     <source>a/directory/holding/xsds</source>
 </sources>
</configuration>

您可以使用

<version>1.6</version>
与旧语法:

<configuration>
    ...
    <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
    <schemaFiles>myapp.xsd</schemaFiles> 
</configuration>

7
投票

我今天遇到了同样的问题,并通过放置解决了它:

<version>1.6</version>

关于插件定义(通常是好的做法)


3
投票

我通过将编译器版本设置为

JDK 1.8
jaxb2-maven-plugin
版本
1.5
来让它工作 根据 documention 它将与最低 JDK 1.6 一起工作 [如果在站点中更改链接可能会失效]。例如:

<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.mntq.jaxb.xsd.to.pojo</groupId>
  <artifactId>XsdToPojo</artifactId>
  <version>0.0.1-SNAPSHOT</version>


  <build>
        <finalName>PersistencePoJO</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
             <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 package of your generated sources -->
                <packageName>com.mntq.jaxb.pojo</packageName>
            </configuration>
        </plugin>
        </plugins>
    </build>
</project> 

1
投票

我们也可以像下面这样使用:

           <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>

0
投票

我通过在 eclipse maven update 中选中“Force Update of Snapshot/Release”选项解决了这个问题。

它强制它更新所有相关的依赖项。


0
投票

如果你只是将 xerces 依赖添加到你的 jaxb 插件声明的末尾,它就会起作用。很简单:插件只需要一些类来执行。现在给他:) I. Senatov.

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <id>xjc-dtd</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <arguments>-dtd</arguments>
                        <dtd>true</dtd>
                        <!-- The package of your generated sources -->
                        <packageName>mi.minet.lurc.dtd</packageName>
                        <schemaDirectory>src/main/resources/dtd</schemaDirectory>
                        <schemaFiles>lurc.dtd</schemaFiles>
                    </configuration>
                </execution>
                <execution>
                    <phase>generate-sources</phase>
                    <id>xjc-xsd</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <packageName>mi.minet.lurc.xsd.configuration</packageName>
                        <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                        <schemaFiles>configuration.xsd</schemaFiles>
                        <clearOutputDir>false</clearOutputDir>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>xerces</groupId>
                    <artifactId>xercesImpl</artifactId>
                    <version>2.11.0</version>
                </dependency>
            </dependencies>
        </plugin>

0
投票

就我而言,我通过将我的 jdk 编译器版本从 jdk 10 降级到 jdk 1.8 来实现它


0
投票

在某些情况下,例如,您应该在构建之前使用

mvn clean

mvn clean compile

0
投票

对于最近的,你可能想尝试thisthis .对我来说,第一个有效。

© www.soinside.com 2019 - 2024. All rights reserved.