Search code examples
javaintellij-ideajaxbquarkusmaven-plugin

JAXB generated classes not seeing their own dependencies in IntelliJ IDEA


I have a maven project that auto generates some classes using the JAXB plugin in the POM. Everything in the project works fine, the project builds correctly, and runs without any issues, however IntelliJ is not seeing that one of the generated classes relies on another, so is constantly telling me that there are issues in the project.

I can resolve these alleged issues by importing one of the generated classes into the other, however that defeats the point of an auto generated class!

The JAXB plugin in the POM is configured as follows:

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

The XSD being called upon looks as follows:

<?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 created three auto generated classes from this: NameType, Person, and ObjectFactory. The path of all three of these is target/generated-sources/xjc/generated.

generated-sources is marked in IntelliJ as the the "Generated Sources Root", and in the project settings I can see that target/generated-sources, target/generated-sources/annotations, and target/generated-sources/xjc/generated have all been added as source folders.

In order to stop IntelliJ thinking there are issues I must add an import generated.NameType; to the Person class, and import generated.NameType; import generated.Person; to the ObjectFactory.

Is there a setting I change in IntelliJ in order that it recognises these things by default. Again, I re-iterate, my code builds and runs absolutely fine - this issue just causes problems with things like Git from automatically pushing, as it believes there are errors, and it is generally annoying to be told there are issues when there aren't any.

In case any of the following is relevant:

I'm using IntelliJ IDEA 2024.1.4 (Ultimate Edition) on an Intel Mac running 13.5.1, and the general POM properties are as follows:

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

Solution

  • Your problem seems to be that you added target/generated-sources/xjc/generated instead of target/generated-sources/xjc as source folder in IntelliJ

    For test projects in jaxb-tools, the project structure is pretty much always as follow :

    project structure in test project jaxb-tools

    In this, you can see that target/generated-sources/xjc is marked as "generated" in source folders.

    In this folder, I have one package named generated since I have no namespace defined in my imported XSD (it seems the same for you).

    Just fix your config to get the base directory (as specified by the generateDirectory plugin's property which defaults to ${project.build.directory}/generated-sources/xjc)

    You can also override the generatePackage plugin's property to make your class generated in another package (like com.foo.baz.bar) instead of generated base package which is default here.