jaxb2-maven-plugin - how to turn off recursive directory traversal when generating xsd from classes

1.8k Views Asked by At

I am using jaxb2-maven-plugin for generating XSD from jaxb annotated classes.

The configuration looks like that

<execution>
    <id>rest-api-execution-schemagen</id>
    <goals>
        <goal>schemagen</goal>
    </goals>
    <phase>generate-resources</phase>
    <configuration>
        <sources>
            <source>src/java/foo/rest/execution/model</source>
        </sources>
        <outputDirectory>${project.build.directory}/execution-api-xml-schema</outputDirectory>
    </configuration>
</execution>

The package foo/rest/execution/model contains many classes, that is why I wish to avoid listing all of them in separate <source> elements. Instead, I specified that I wish to include the whole src/java/foo/rest/execution/model directory, using a single <source> element.

The problem is that there are subpackages:

foo/rest/execution/model/builder

... which contain other classes that are not jaxb annotated and should not be part of the schema. Unfortunately, the schemagen goal attempts to traverse the foo/rest/execution/model directory recursively and therefore attempts to generate schemas for the classes in the subdirectories.

Is there a way to avoid that?

1

There are 1 best solutions below

3
On

You can use xjcSourceExcludeFilters to filter the sources defined by source i.e. to exclude packages, files etc.

For example:

<configuration>
    <sources>
        <source>src/java/foo/rest/execution/model</source>
    </sources>
    <xjcSourceExcludeFilters>
        <filter implementation="org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter">
            <patterns>
                <pattern>src/java/foo/rest/execution/model/builder/*.java</pattern>
            </patterns>
        </filter>
    </xjcSourceExcludeFilters>
    ...
</configuration>

If the built-in filter support cannot meet your needs then you can provide your own implementation by replacing org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter with your own implementation of AbstractFilter.

More details on using this here and more details on defining filters here.