4

I want to generate "@java.lang.SuppressWarnings("all")" before the generated "Doc" class.

Problem: jaxb2-annotate-plugin don't generate annotations.

My pom.xml:

...
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <extension>true</extension>
                        <arguments>-Xannotate</arguments>
                        <args>
                        <arg>-Xannotate</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics-annotate</artifactId>
                            </plugin>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
                            </plugin>
                        </plugins>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics-annotate</artifactId>
                    <version>0.6.4</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.codemodel</groupId>
                    <artifactId>codemodel</artifactId>
                    <version>2.6</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics-annotate</artifactId>
        <version>0.6.4</version>
    </dependency>
    <dependency>
        <groupId>com.sun.codemodel</groupId>
        <artifactId>codemodel</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>${jaxb-api.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>${jaxb.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-xjc</artifactId>
        <version>${jaxb.version}</version>
    </dependency>
</dependencies>

<properties>
    <jaxb.version>2.2.5-2</jaxb.version>
    <jaxb-api.version>2.1</jaxb-api.version>
</properties>

My XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="..."
targetNamespace="..." elementFormDefault="qualified"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:annox="http://annox.dev.java.net"
jaxb:version="2.1" jaxb:extensionBindingPrefixes="annox">
<xs:element name="Doc">
    <xs:complexType>
        <xs:annotation>
            <xs:appinfo>
                <annox:annotate>@java.lang.SuppressWarnings("all")</annox:annotate>
                <annox:annotateClass>@java.lang.SuppressWarnings("all")
                </annox:annotateClass> 
                <annox:annotate target="class">@java.lang.SuppressWarnings("all")
                </annox:annotate>
            </xs:appinfo>
        </xs:annotation>
        <xs:sequence>
            <xs:element ref="info" />
        </xs:sequence>
    </xs:complexType> 
</xs:element>

I tryed all of the three ways to generate "@java.lang.SuppressWarnings("all")" before Doc class

Xeddon
  • 429
  • 8
  • 18

2 Answers2

5

I think the plugin is not activated. You're using jaxb2-maven-plugin and the use configuration/plugins/plugin to configure the jaxb2-annotate-plugin. The problem is, plugins/plugin is the configuration option of my maven-jaxb2-plugin, not the Codehaus jaxb2-maven-plugin.

Try to switch to maven-jaxb2-plugin or figure out how JAXB2 plugins are activated in jaxb2-maven-plugin.

Also use the newer version. You're trying to use the Java syntax, which is a quite recent development. Please use the actual version of the jaxb2-annotate-plugin (the current is 1.0.1).

Here's one of the test projects for the reference:

Your customizations look fine, any of the three should work.

Disclaimer: I'm the author of jaxb2-annotate-plugin and maven-jaxb2-plugin.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • With jaxb2-maven-plugin, you can replace -Xannotate by -Xannotate, but it says : "parameter -Xannotate not known". So is there a chance the annotate plugin can work with jaxb2-maven-plugin ? – Tristan Mar 02 '18 at 15:00
  • @Tristan You have to add `org.jvnet.jaxb2_commons:jaxb2-basics-annotate` in dependencies of the `jaxb2-maven-plugin`. Here's an [example](https://stackoverflow.com/questions/9062539/generating-hashcode-and-equals-when-creating-java-classes-using-mojo-jaxb2-m/9136644#9136644). – lexicore Mar 02 '18 at 15:25
  • @Tristan No guarantees, however as I don't use `jaxb2-maven-plugin` myself. – lexicore Mar 02 '18 at 15:25
0

Try this XSD ..

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="..." xmlns="..."
    elementFormDefault="qualified" attributeFormDefault="unqualified" version="0.0.1"
    xmlns:annox="http://annox.dev.java.net" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
    xmlns:jl="http://annox.dev.java.net/java.lang" jaxb:extensionBindingPrefixes="annox">

    <xs:element name="Doc">
        <xs:complexType>
            <xs:annotation>
                <xs:appinfo>
                    <annox:annotate>
                        <annox:annotate annox:class="java.lang.SuppressWarnings">all</annox:annotate>
                    </annox:annotate>
                </xs:appinfo>
            </xs:annotation>
            <xs:sequence>
                <xs:element ref="info" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

But i prefer external binding like this (an example)

<bindings version="2.0" xmlns="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:annox="http://annox.dev.java.net"
    xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:extensionBindingPrefixes="annox">

    <bindings schemaLocation="../Binding.xsd">

        <bindings node="//xs:complexType[@name='Bindings']">
            <annox:annotate>
                <annox:annotate annox:class="java.lang.SuppressWarnings" >all</annox:annotate>
            </annox:annotate>
        </bindings>

    </bindings>
</bindings>

maven plugin used (an example)

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.11.0</version>
    <executions>
        <execution>
            <id>commun-generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-Xannotate</arg>
                    <arg>-nv</arg>
                    <arg>-Xnamespace-prefix</arg>
                </args>
                <bindingDirectory>${basedir}/src/main/resources/META-INF/schema/binding</bindingDirectory>
                <bindingIncludes>
                    <bindingInclude>global-binding.xjb</bindingInclude>
                    <bindingInclude>Binding.xjb</bindingInclude>
                </bindingIncludes>
                <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                <schemas>
                    <schema>
                        <fileset>
                            <directory>${basedir}/src/main/resources/META-INF/schema</directory>
                            <includes>
                                <include>Binding.xsd</include>
                            </includes>
                        </fileset>
                    </schema>
                </schemas>
                <strict>false</strict>
                <extension>true</extension>
                <verbose>true</verbose>
                <forceRegenerate>true</forceRegenerate>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.0</version>
                    </plugin>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                        <version>0.6.0</version>
                    </plugin>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-namespace-prefix</artifactId>
                        <version>1.1</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
</plugin>
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • I agree that external bindings are the way to go. But you're proposing the "old" XML syntax for annotations, and in newer versions you can use Java syntax which I recommend. I don't think the problem is in bindings, I think it is either in the Maven plugin configuration or the version of the `jaxb2-annotate-plugin`. – lexicore Feb 27 '15 at 15:45