135

I have determined that two JAXB plugins for Maven 2 exist, with some different configurations.

The one is from Sun: http://jaxb.dev.java.net/jaxb-maven2-plugin/, the other from Mojohaus: http://mojohaus.org/jaxb2-maven-plugin/

Which of these two plugins can be recommended?


Thanks Matt. On my little research project, I found that there's quite another plugin comming from the sunners:

<groupId>com.sun.tools.xjc.maven2</groupId>  
<artifactId>maven-jaxb-plugin</artifactId>  

and that one:

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>

and still the one from Codehouse.

Efthymis
  • 1,326
  • 11
  • 13
cuh
  • 3,723
  • 4
  • 30
  • 47

6 Answers6

112

Let's summarize. We have/had:

  1. the maven-jaxb2-plugin (https://github.com/highsource/maven-jaxb2-plugin)
  2. the maven-jaxb-plugin (https://jaxb.dev.java.net/jaxb-maven2-plugin/)
  3. the jaxb2-maven-plugin (https://github.com/mojohaus/jaxb2-maven-plugin)

Based on the comments of this thread, I've always used the maven-jaxb2-plugin (i.e. plugin #1):

Concerning the org.jvnet.jaxb2.maven2:maven-jaxb2-plugin versus com.sun.tools.xjc.maven2:maven-jaxb-plugin, from my point of view it's definitely the first one (http://maven-jaxb2-plugin.java.net/).

This plugin has much more features than com.sun.tools.xjc.maven2:maven-jaxb-plugin, the development is active. Finally, I'm one of the authors :) and I'd say we keep in touch with JAXB developers and users and react to the latests features/requests.

And indeed, the plugin #2 is dead. And because I've always been happy with #1, I've never used plugin #3 so can't really say anything about it. Just in case, here is a working configuration for plugin #1:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <inherited>true</inherited>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>



    
Lonzak
  • 9,334
  • 5
  • 57
  • 88
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Thx for the config. In the end i use the codehouse plugin, which gives me the same capabilities for my needs. Only the syntax differs as far as i have seen. – cuh Mar 16 '10 at 16:14
  • What about http://ws.apache.org/jaxme/mp/ which is mentioned directly on Maven's site? – rcl Jul 19 '10 at 19:18
  • 1
    @rcl: Can't say much about it but since it hasn't been updated for more than 4 years, I'm not sure I would use it. I'm just happy with the [maven-jaxb2-plugin](https://maven-jaxb2-plugin.dev.java.net/). – Pascal Thivent Jul 19 '10 at 19:29
  • 1
    As for #1, the site hosting the documentation seems to be down today. http://confluence.highsource.org/display/MJIIP/User+Guide Is this project this active? – rds Oct 15 '12 at 14:57
  • Just found this terrific answer. The first plugin seems to be the most completely. The rest isn't worth mentioning anymore. – Michael-O Nov 22 '13 at 09:39
  • 2
    @Gregor `maven-jaxb2-plugin` is now hosted on [GitHub](https://github.com/highsource/maven-jaxb2-plugin). The documentation is in the [wiki](https://github.com/highsource/maven-jaxb2-plugin/wiki). – lexicore Nov 03 '14 at 15:02
  • `maven-jaxb2-plugin` is not compatible with java 11, while `jaxb2-maven-plugin` yes. – Marco Sulla May 02 '23 at 15:14
  • The [HiSrc HigherJAXB](https://github.com/patrodyne/hisrc-higherjaxb#readme) project is a fork of `maven-jaxb2-plugin`. Its source/target (release) compatibility is at Java 11 and JDK 17 is used for the build. JAXB dependencies are at version 4.x for [Jakarta EE 10](https://jakarta.ee/specifications/xml-binding). – Rick O'Sullivan Jul 29 '23 at 14:41
50

I have recently tried the three plug-ins mentioned above (included here as well):

  1. the maven-jaxb2-plugin (http://maven-jaxb2-plugin.java.net/)
  2. the maven-jaxb-plugin (https://jaxb.dev.java.net/jaxb-maven2-plugin/)
  3. the jaxb2-maven-plugin (http://mojo.codehaus.org/jaxb2-maven-plugin/)

I ended up using a fourth option: The CXF XJC Maven Plugin http://cxf.apache.org/cxf-xjc-plugin.html

If I am missing something I would like to know, but the configuration seemed more straightforward for what I was trying to do and more easily allowed me to to deal with duplicate class generation within the same namespace -- similar to this question: Is there a way to deal with duplicate element definitions across multiple .xsd files in JAXB?.

I now have granular control over each incoming XSD and corresponding java package; here is a sample configuration close to the one I am using.

 <plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-xjc-plugin</artifactId>
    <version>2.3.0</version>
    <configuration>
        <extensions>
            <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
        </extensions>
    </configuration>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>xsdtojava</goal>
            </goals>
            <configuration>
                <sourceRoot>${basedir}/target/generated-sources/src/main/java</sourceRoot>
                <xsdOptions>
                    <xsdOption>
                        <xsd>src/main/resources/schema/commands.xsd</xsd> <!--shares a common.xsd file causing the conflicts-->
                        <packagename>com.foo.bar.commands</packagename>
                    </xsdOption>
                    <xsdOption>
                        <xsd>src/main/resources/schema/responses.xsd</xsd>
                        <packagename>com.foo.bar.responses</packagename>
                    </xsdOption>
                </xsdOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
Community
  • 1
  • 1
bn.
  • 7,739
  • 7
  • 39
  • 54
  • 1
    Excellent suggestion. I just tried this and it worked like a charm- thank you. – SGB May 06 '13 at 22:09
  • Does anyone know if there is an Eclipse m2e connector for this? I did some googling and didn't find one. Hoping I just missed it because I like this option. – user944849 May 14 '13 at 14:54
  • cxf-xjc-plugin has Eclipse connector as of 2017. But I do not see the problem of generating the code manually running maven and adding the source directory. In fact, my experience is that projects with generated classes (JAXB, XMLBeans) are better left closed and depended on in a binary form (jar). Then IDE works faster and there are no classpath issues (especially with XMLBeans). – Vytenis Bivainis Apr 14 '17 at 22:10
  • Looks like CXF XJC Maven Plugin only supports generating Java sources from schema, and is not capable to generate schema from Jaxb annotations. – alebu Jan 02 '20 at 08:46
21

I am the author of maven-jaxb2-plugin.

The maven-jaxb2-plugin currently uses JAXB 2.1. In the next versions we'll also provide JAXB 2.0 and JAXB 2.2 versions.

As for "which plugin is better" discussion, check the features, decide yourself. Let me know if you miss some functionality.

Coderino Javarino
  • 2,819
  • 4
  • 21
  • 43
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Is it possible to generate schemas from classes using the maven-jaxb2-plugin? Or does the plugin only support xsd->Java? – Jörg May 21 '13 at 03:47
  • Currently it's xsd->java only. – lexicore May 22 '13 at 06:25
  • Does the name 'maven-jaxb2-plugin' not violate the [Maven plugin naming conventions](https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#Plugin_Naming_Convention_and_Apache_Maven_Trademark) or is it an official maven plugin? I don't mind the name but as it never was updated I thought the plugin would not be maintained anymore. – FrVaBe Mar 20 '14 at 09:18
  • The plugin is quite old, it was incepted before this became an established convention. And jaxb2-maven-plugin was already taken back then. I think it is important to avoid name collision here. However nowadays plugin build even enforce these naming conventions so probably I'll have to migrate anyway. – lexicore Mar 20 '14 at 10:08
  • Ah, I see. But `org.jvnet.jaxb2.maven2:jaxb2-maven-plugin` seems to be still available ;-) If you put still effort in the plugin it would be nice if it get a standard maven plugin documentation page. Or do you plan to drop support? Would be too bad because it works great. Thanks! – FrVaBe Mar 20 '14 at 13:17
  • Well, yes, fully qualified name is available but people will always mistaken one plugin for another. Yes, I still support the project. – lexicore Mar 20 '14 at 15:16
  • Is it possible in this plugin to specify NTLM credentials for schema urls? – lanoxx Jul 24 '14 at 15:05
  • 3
    @lanoxx No, it is not. It is generally not recommended to compile schemas from URLs. Make local copies of schemas and use catalog files to rewrite links. – lexicore Jul 24 '14 at 15:44
  • Pretty sure highsource[dot]org is no longer a legitimate site S/O users would want to visit.. May wish to revise the question/post/comments that reference it. – Ian W Jul 11 '20 at 10:22
  • 2
    Actual site: https://github.com/highsource/maven-jaxb2-plugin – lexicore Jul 12 '20 at 07:53
  • I know i am probably late into the game - but i am seeing behavior which i can't explain for the plugin - it seems that the plugin is not generating classes for non complex types. I am trying to use the plugin along with Spring Boot. Any help on that one? – alext Jan 27 '23 at 09:27
5
  • maven-jaxb2-plugin uses the JAXB reference implementation by Oracle/Sun
  • cxf and jaxb2-maven-plugin use Apache Xerces
rds
  • 26,253
  • 19
  • 107
  • 134
2

On a slight tangent: there was a problem with use of maven-jaxb2-plugin with Eclipse Indigo that I posted here. A fix (extension) has recently become available.

This is not meant to disagree, at all, with the recommendation of maven-jaxb2-plugin over maven2-jaxb-plugin. I don't know, but I expect that maven2-jaxb-plugin has the same problem, probably unresolved.

Community
  • 1
  • 1
Ed Staub
  • 15,480
  • 3
  • 61
  • 91
0

I would guess that one is for the original JAXB specification and the codehaus one is for the JAXB 2.1 specification (and if the dev.java.net would load some time this century, I'd be able to say for sure).

matt b
  • 138,234
  • 66
  • 282
  • 345
  • Yeah, and if it would be possible to dowload the **org.jvnet.jaxb2.maven2 plugin** i would compare them. Since the repo of java doesn't provide the plugin it's out of the game, although the configuration-syntax seemed a little more convenient. – cuh Mar 12 '10 at 14:29