highsource / jaxb2-annotate-plugin

Add arbitrary annotations to JAXB classes.
Other
72 stars 28 forks source link

Unable to run annotate and remove annotate simultaneously #54

Closed wrestang closed 5 years ago

wrestang commented 5 years ago

I am using the plugin with wsdl2java because I have wsdls I need to convert to java. I've been able to successfully use either the annotate plugin or the remove annotation plugin, but not both at the same time; I have to comment out the extra arg and it's corresponding binding file. When I try to run both I get the following:

add-annotations.xjb [29,25]: "annox:annotateClass" is not a valid customization.

Those numbers might be off for this as I'm obfuscating the exact name of files to try to simplify this post. Also, I can't change the schema or wsdl files because they are not created by me and thus out of my control, so I'm going with binding files.

Here is my pom code:

<plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>${cxf.version}</version>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
              <sourceRoot>${project.basedir}/src/main/java</sourceRoot>
              <wsdlOptions>
                <wsdlOption>
                  <bindingFiles>
                    <bindingFile>${project.basedir}/src/main/resources/binding1.wsdl</bindingFile>
                    <bindingFile>${project.basedir}/src/main/resources/binding2.wsdl</bindingFile>
                    <bindingFile>${project.basedir}/src/main/resources/add-annotations.xjb</bindingFile>
                    <bindingFile>${project.basedir}/src/main/resources/remove-annotations.xjb</bindingFile>
                  </bindingFiles>
                  <wsdl>${project.basedir}/src/main/resources/wsdl-file.wsdl</wsdl>
                  <extraargs>-xjc-Xannotate</extraargs>
                  <extraargs>-xjc-XremoveAnnotation</extraargs>
                </wsdlOption>
              </wsdlOptions>
            </configuration>
            <goals>
              <goal>wsdl2java</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-annotate</artifactId>
            <version>1.1.0</version>
          </dependency>
        </dependencies>
      </plugin>

Here is my add annotations xjb binding:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jaxb:extensionBindingPrefixes="xjc annox"
    version="2.1">
    <jaxb:bindings schemaLocation="a-schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='AType']">
            <annox:annotateClass>@javax.xml.bind.annotation.XmlRootElement</annox:annotateClass>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

And here is the remove binding:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jaxb:extensionBindingPrefixes="xjc annox"
    version="2.1">

    <jaxb:bindings schemaLocation="b-schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='BType']//xs:attribute[@ref='xml:value']">
            <annox:removeAnnotation class="javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter" target="field" />
            <annox:removeAnnotation class="javax.xml.bind.annotation.XmlSchemaType" target="field" />
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>
wrestang commented 5 years ago

I've found another plugin that will alter the auto-generated package-info file (I'd use this one if I knew how to change that file) to add namespaces to it. I'm seeing issues when trying to run both of those plugins too, so I'm starting to think either it's a configuration issue and/or some limitation when running within the wsdl2java realm which under the covers uses xjc... something is different in how things run.

wrestang commented 5 years ago

OK... so after all this... I feel like a downright idiot and kinda wish I could scrub all of this...

It was a configuration issue... The xjc options need to be bunched together in a comma separated list and I just completely missed that... so solution:

<extraargs>-xjc-Xannotate,-xjc-XremoveAnnotation</extraargs>

As a side note, in case you actually want to add other args that are not xjc, or want them listed separately (which is what my real problem was, and considering I've made mojo's before I don't know how I didn't get this) use something like this:

<extraargs>
    <extraarg>-xjc-Xannotate</extraarg>
    <extraarg>-xjc-XremoveAnnotation</extraarg>
    <extraarg>-mark-generated</extraarg>
</extraargs>