2

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?

lexicore
  • 42,748
  • 17
  • 132
  • 221
mdzh
  • 1,030
  • 2
  • 17
  • 34

1 Answers1

0

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.

glytching
  • 44,936
  • 9
  • 114
  • 120
  • The pattern src/java/foo/rest/execution/model/builder/*.java doesn't work unfortunately :( I guess the syntax is wrong. – mdzh Jan 03 '18 at 13:17
  • You might need to read [the docs](http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/filters.html) and play around with filter patterns or just provide your own implementation of `AbstractFilter`. – glytching Jan 03 '18 at 13:26
  • I believe pattern is a regex, not a fileglob pattern. So you would need something like `builder/[a-zA-Z0-9]+\.java` – Cheeso May 10 '18 at 21:22