mojohaus / jaxb2-maven-plugin

JAXB2 Maven Plugin
https://www.mojohaus.org/jaxb2-maven-plugin/
Apache License 2.0
107 stars 77 forks source link

<sources> parameter for schemagen is ignored! #3

Closed rankinc closed 9 years ago

rankinc commented 9 years ago

I've been trying to upgrade to jaxb2-maven-plugin 2.1 from 1.6. For 1.6, I was using

<configuration>
    <includes>
        <include>my/app/xml/*.java</include>
    </includes>
</configuration>

For 2.1, I understand that I need to use the following instead:

<configuration>
    <sources>
        <source>src/main/java/my/app/xml</source>
    </sources>
</configuration>

However, the plugin always uses src/main/java! This cannot work, because most of these files are not JAXB classes.

I've examined the plugin source, and have noticed this in SchemaGenerationMojo:

@Parameter(defaultValue = "${project.compileSourceRoots}", readonly = true, required = true)
private List<String> sources;

I don't believe that this parameter should be annotated as read-only. And similarly for the testSources parameter in TestSchemaGenerationMojo.

rankinc commented 9 years ago

I've been experimenting in a private fork, and it looks like the @Parameter's default value is always clobbering the user's value. Basically, I can only get the source parameter to work like this:

@Parameter(required = true)
private List<String> sources;

This isn't compatible with the documented behaviour, but the lack of a default means that the parameter is definitely required now. (I'm not sure that required = true is correct in the original code).

lennartj commented 9 years ago

The 2.x version uses a slightly different configuration but the same functionality is supported.

The configuration nowadays uses your normal compile sources (typically src/main/java) unless you provide another sources configuration given as a sources element as documented in the plugin. You can then exclude files found under the source directories by providing one or more schemaSourceExcludeFilters from processing.

This is documented in the Basic SchemaGen Examples of the plugin. For example, the configuration below would make schemagen process all files in your compile sources except files that:

  1. ... have the filename jaxb.index
  2. ... have a filename ending in .txt
  3. ... have a filename ending in .mdo.
<configuration>
               ...
                <schemaSourceExcludeFilters>
                    <myExcludes implementation="org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter">
                        <patterns>
                            <pattern>jaxb\.index</pattern>
                            <pattern>\.txt</pattern>
                            <pattern>\.mdo</pattern>
                        </patterns>
                    </myExcludes>
                </schemaSourceExcludeFilters>
</configuration>
rankinc commented 9 years ago

Hi, Thanks for the reply, but what I am trying to say is that I have actually tested jaxb2-maven-plugin:2.1 and it isn't working! I have written a trivial NetBeans project with the following POM:

<?xml version="1.0" encoding="UTF-8"?>
<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>testing</groupId>
    <artifactId>test-jaxb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Test JAXB</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <sources>
                        <source>src/main/java/org/testing/xml</source>
                    </sources>
                    <transformSchemas>
                        <transformSchema>
                            <uri>example</uri>
                            <toPrefix>ex</toPrefix>
                            <toFile>example.xsd</toFile>
                        </transformSchema>
                    </transformSchemas>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>schemagen</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The JAXB classes are in the org.testing.xml package, and there is a non-JAXB class in org.testing. As you can see, I have tried to configure the plugin only to consider classes in org.testing.xml. However, the plugin ignores the <sources> section and passes org/testing/TestJaxb.java to schemagen anyway!

[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.1:schemagen (default) on project test-jaxb:
[ERROR] +=================== [SchemaGenerator Error '<unknown>']
[ERROR] |
[ERROR] | SchemaGen did not complete its operation correctly.
[ERROR] |
[ERROR] | To re-create the error (and get a proper error message), cd to:
[ERROR] | /home/chris/NetBeansProjects/test-jaxb
[ERROR] | ... and fire the following on a command line/in a shell:
[ERROR] |
[ERROR] | schemagen -encoding UTF-8 -d /home/chris/NetBeansProjects/test-jaxb/target/schemagen-work/compile_scope -classpath /home/chris/NetBeansProjects/test-jaxb/src/main/java/ -episode /home/chris/NetBeansProjects/test-jaxb/target/generated-resources/schemagen/META-INF/sun-jaxb.episode src/main/java/org/testing/TestJaxb.java src/main/java/org/testing/xml/MyType.java
[ERROR] |
[ERROR] | The following source files should be processed by schemagen:
[ERROR] | 0: file:/home/chris/NetBeansProjects/test-jaxb/src/main/java/org/testing/TestJaxb.java
[ERROR] | 1: file:/home/chris/NetBeansProjects/test-jaxb/src/main/java/org/testing/xml/MyType.java
[ERROR] | 2: file:/home/chris/NetBeansProjects/test-jaxb/src/main/java/org/testing/xml/package-info.java
[ERROR] |
[ERROR] +=================== [End SchemaGenerator Error]: java.lang.AssertionError: java.lang.ClassCastException: com.sun.tools.javac.api.JavacTrees cannot be cast to com.sun.source.util.Trees
lennartj commented 9 years ago

Revisited; you are correct - this is a bug which needs patching. I'm on it.

lennartj commented 9 years ago

Fixed by merge b7278cf5487bf0c7643f70b76c98bd632090c09f

rankinc commented 9 years ago

Thanks, Chris