JAXB 生成的类在 IntelliJ IDEA 中看不到自己的依赖项

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

我有一个 Maven 项目,它使用 POM 中的 JAXB 插件自动生成一些类。项目中的一切工作正常,项目构建正确,并且运行没有任何问题,但是 IntelliJ 没有看到生成的类之一依赖于另一个类,因此不断告诉我项目中存在问题。

我可以通过将生成的类之一导入另一个类来解决这些所谓的问题,但这违背了自动生成的类的意义!

POM中的JAXB插件配置如下:

        <plugin>
            <groupId>org.jvnet.jaxb</groupId>
            <artifactId>jaxb-maven-plugin</artifactId>
            <version>4.0.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>src/main/resources/XSD</schemaDirectory>
                <schemaIncludes>
                    <include>*.xsd</include>
                </schemaIncludes>
            </configuration>
        </plugin>

被调用的 XSD 如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- Definition of the root element -->
    <xs:element name="Person">
        <xs:complexType>
            <xs:sequence>
                <!-- First Name field -->
<!--                <xs:element name="FirstName" type="xs:string" />-->
                <xs:element name="FirstName" type="NameType" />
                <!-- Middle Name field -->
                <xs:element name="MiddleName" type="xs:string" />
<!--                <xs:element name="MiddleName" type="NameType" />-->

                <!-- Surname field -->
<!--                <xs:element name="Surname" type="xs:string" />-->
                <xs:element name="Surname" type="NameType" />

                <!-- Postcode field -->
<!--                <xs:element name="Postcode" type="xs:string" />-->
                <xs:element name="Postcode" type="NameType" />

                <!-- Date of Birth field -->
                <xs:element name="DateOfBirth" type="DateType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- Custom complex type for Name with value attribute -->
    <xs:complexType name="NameType">
        <xs:attribute name="value" type="xs:string" use="required" />
    </xs:complexType>

    <!-- Custom simple type for Date of Birth with format validation -->
    <xs:simpleType name="DateType">
        <xs:restriction base="xs:string">
            <xs:pattern value="\d{4}\d{2}\d{2}" /> <!-- Pattern for 'yyyyMMdd' -->
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

JAXB 由此创建了三个自动生成的类:NameType、Person 和 ObjectFactory。这三个的路径都是 target/ generated-sources/xjc/ generated 。

generated-sources 在 IntelliJ 中被标记为“生成的源根”,在项目设置中我可以看到

target/generated-sources
target/generated-sources/annotations
target/generated-sources/xjc/generated
都已添加为源文件夹。

为了阻止 IntelliJ 认为存在问题,我必须将

import generated.NameType;
添加到 Person 类,并将
import generated.NameType; import generated.Person;
添加到 ObjectFactory。

我是否在 IntelliJ 中更改了某个设置,以便它默认识别这些内容。再次,我重申,我的代码构建并运行得非常好 - 这个问题只会导致 Git 等自动推送出现问题,因为它认为存在错误,而且在没有错误的情况下被告知存在问题通常很烦人没有。

如果存在以下任何相关情况:

我在运行 13.5.1 的 Intel Mac 上使用 IntelliJ IDEA 2024.1.4(终极版),一般 POM 属性如下:

<properties>
        <compiler-plugin.version>3.12.1</compiler-plugin.version>
        <maven.compiler.release>21</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>3.9.3</quarkus.platform.version>
        <skipITs>true</skipITs>
        <surefire-plugin.version>3.2.5</surefire-plugin.version>
    </properties>
java intellij-idea jaxb quarkus maven-plugin
1个回答
0
投票

您的问题似乎是您在 IntelliJ 中添加了

target/generated-sources/xjc/generated
而不是
target/generated-sources/xjc
作为源文件夹

对于 jaxb-tools 中的测试项目,项目结构几乎总是如下:

project structure in test project jaxb-tools

在此,您可以看到

target/generated-sources/xjc
在源文件夹中被标记为“生成”。

在此文件夹中,我有一个名为

generated
的包,因为我在导入的 XSD 中没有定义命名空间(对您来说似乎是一样的)。

只需修复您的配置即可获取基本目录(由

generateDirectory
插件属性指定,默认为
${project.build.directory}/generated-sources/xjc

您还可以覆盖

generatePackage
插件的属性,使您的类在另一个包(如
com.foo.baz.bar
)中生成,而不是在此处默认的
generated
基本包中。

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