How to run xjc within gradle to generate classes from .xsd

Is there an example anywhere on how to run xjc from within Gradle to generate classes from an .xsd file ( and put the generated classes in the compiled director and .jar ? These are generated classes that I want included in the compile but do not want to check them in so do not wan them in src/main/java.

Didn’t you search for something that does this, like “https://github.com/stianh/gradle-jaxb-plugin”?

I will give this example a go.

I looked at this but do not understand the syntax; Under Usage: it has:

dependencies {

jaxb ‘com.sun.xml.bind:jaxb-xjc:2.2.4-1’ }

I thought dependencies {

=> typically starts with ‘compile’ and then lists the resource. }

what does starting with ‘jaxb’ do?

— In addition, in my existing build.gradle file, I simply have

repositories {

mavenCentral()

}

---- This example has: buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath ‘no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0’

} }

so what is the difference here? (repositories nested in buildscript with yet another dependencies section?

I get errors (and deprecation messages) when I follow the Usage section for this plugin; does anyone have a working example?

I just want to add a step that generates classes when I build (normally do this with the command line with xjc).

— When I added just this:

  apply plugin: 'jaxb'

 buildscript {



repositories {





 mavenLocal()





 mavenCentral()







maven {







  credentials {











username ""











password ""







  }







  url "https://myrepo.mycomp.com"





 }



}



dependencies {





 classpath 'no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0'



}  }

 dependencies {



compile 'org.springframework:spring-core:4.0.6.RELEASE'



compile 'org.springframework:spring-context:4.0.6.RELEASE'



compile 'org.apache.commons:commons-email:1.3.3'



// why does not begin with 'compile'?



jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.4-1'



  // QA



testCompile 'org.springframework:spring-test:4.0.6.RELEASE'



testCompile group: 'junit', name: 'junit', version: '4.+'  }

 
I get an error:
  $ gradle build  Download http://repo1.maven.org/maven2/no/entitas/gradle/jaxb/gradle-jaxb-plugin/2.0/gradle-jaxb-plugin-2.0.pom  Download http://repo1.maven.org/maven2/no/entitas/gradle/jaxb/gradle-jaxb-plugin/2.0/gradle-jaxb-plugin-2.0.jar  The ConfigurationContainer.add() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the create() method instead.  The TaskContainer.add() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the create() method instead.  :generateSchemaSource UP-TO-DATE  :compileJava

 FAILURE: Build failed with an exception.

 * What went wrong:  Could not resolve all dependencies for configuration ':compile'.  > Could not find org.springframework:spring-core:4.0.6.RELEASE.

 Required by:



  :mymodule:unspecified  > Could not find org.springframework:spring-context:4.0.6.RELEASE.

 Required by:



  :mymodule:unspecified  > Could not find cglib:cglib:2.2.2.

 Required by:



  :mymodule:unspecified  > Could not find com.force.api:force-wsc:22.0.0.

 Required by:



  :ForceOnbaseImportNB:unspecified  > Could not find org.apache.commons:commons-email:1.3.3.

 Required by:



  :mymodule:unspecified  > Could not find com.sun.xml.bind:jaxb-xjc:2.2.4-1.

 Required by:



  :mymodule:unspecified

 * Try:  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

 BUILD FAILED

 Total time: 5.852 secs  

This plugin does not work, or at least I cannot get it to work by following the Usage section and integrating that into my current build.gradle file.

Is there anyone out there that has actually done this? What I am looking for is someone who has actually been able to generate JAXB classes on a build (not someone who is going to simply google this and post a URL).

Perhaps you should read the Gradle user guide to get a better understanding of the top-level concepts.

For instance, you’ll figure out that “jaxb” is a “Source Set” which the jaxb plugin defines.

You’ll also learn that the “buildscript” section is only for dependencies of the build script itself, not for your application.

You are specifying repositories for the buildscript, but not for the application itself (I don’t know whether MavenCentral is assumed by default). Perhaps if you copied your repositories section to the “root” of the build script, it might find those dependencies.

I have more understanding of the gradle build file now; thank you for the comments.

The project mentioned is dead, not being maintained. When I followed the usage instructions, it is downloading com.sun.xml.bind.jaxb stuff and stax-api dependencies, there is no need for any of those dependencies given that JAXB support is now native to JDK 1.6 and higher and there are many other issues as you can see by the github issues ( no comments, updates in a couple of years ).

It is basically a dead or abandoned project.

If there is a maintained jaxb/gradle plugin out there, would be nice; I will search for something else or simply continue to do more things manually and hope for automation at a later date.

Running as is, the plugin will hang using JDK 1.6, gets stuck and does not return to the command prompt. The author has not been responsive on github for quite some time on many other issues ( a year ).

If there is anyone anywhere using JAXB with gradle (successfully), please post.

And you don’t get any clues when you run with “–info” or “–debug”?

Also note that there is no reason you can’t simply clone the project and copy the source into your “buildSrc” tree (look in user guide for this) and run your build in the debugger.

Hi,

I’m one of the authors of the JAXB plugin, and I’m sorry to say that this plugin has not been maintained properly for a long time, as you will have seen. I have not tried it, but this plugin: https://github.com/jacobono/gradle-jaxb-plugin, seems to be in much better shape.

Thanks for the info! I will give this a go now. We are attempting to reduce as much shell scripting as possible if it can be done in Gradle (we have been long time users of Maven where this is working well so far and do not want to have part of the build/test/deploy process in Maven or a bash script and the rest in Gradle).

I could not get this plugin to work either, I have spent way too much time on this and falling back to Maven (it works there).

My suggestion would be to delete projects from github, webpages, blog entries that are out of date, no longer work or have documentation specific to JDK 1.5 and less (or at a minimum always post a date with the date of the post and update in large flashing red letters T H I S N O L O N G E R W O R K S D O N ’ T W A S T E Y O U R P R E C I O U S T I M E H E R E G O W A L K Y O U R D O G S.

I get this error:

  $ gradle build

 FAILURE: Build failed with an exception.

 * What went wrong:  A problem occurred configuring root project 'Project'.  > Could not resolve all dependencies for configuration ':classpath'.  > Could not find com.jacobo.gradle.plugins:gradle-jaxb-plugin:1.3.1.



 Required by:





  :Project:unspecified

 * Try:  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

 BUILD FAILED

 Total time: 3.263 secs

 

Actually, I succeeded in making it work, but my only problem now is that I cannot specify the package name as I can with Maven or directly using xjc. The classes are generated with the package “generated”. Trying to use the packageName setting returns that this setting is deprecated. Does anyone has a clue on how I can specify the package name?

Here is the build.gradle file that I am using -

1 Like

ok, found the solution. Just place the xsd file under /com/xxx/yyy/xxx. The file will be located there and the package will be com.xxx.yyy.zzz. No need for packageName…

I don’t mean to bump an old thread, just wanted to let you know that this plugin is working and has updated documentation now.

Apologies for that piece of the documentation being off, I forgot to update the group name after I had changed it.

Someone raised that issue on github and I fixed it. I’ve also accepted a few pull requests and fixed some other issues.

I haven’t been lurking here as I wasn’t sure the plugin was ready to go, but some of the folks who raised issues seem to think so. Hence my finding this after a month.

By “this plugin”, I assume you mean the “jacobono/gradle-jaxb-plugin”?

I sent you a direct email on this, but perhaps I should point it out here.

When I install this, I get the following (using either snippet option described in the plugin doc):

Could not find org.gradle.jacobo:gradle-xsd-wsdl-slurping:1.1.1.

Searched in the following locations:

https://jcenter.bintray.com/org/gradle/jacobo/gradle-xsd-wsdl-slurping/1.1.1/gradle-xsd-wsdl-slurping-1.1.1.pom

https://jcenter.bintray.com/org/gradle/jacobo/gradle-xsd-wsdl-slurping/1.1.1/gradle-xsd-wsdl-slurping-1.1.1.jar

I confirmed that there is no “1.1.1” folder, so either dependencies are off, or that dependency needs to be uploaded to jcenter.

Hi there,

I could successfully run this… Make sure you have Gradle 2.1.

Full Instructions how to do this with Java 1.7/1.8 and Gradle 2.1 is at

https://gist.github.com/marcellodesales/dbb9baf0c0214a4b84e8

The GIST contains FULL Example!!! XSD, build.gradle and the Resulting classes!!!

1 Like