简体   繁体   中英

how to generate java class from xsd by using xmlbeans in gradle

I am new to gradle. In my project, i have xsd files and i would like to generate java classes from that xsd (using xmlbeans only). How to do that in gradle.

I am using axis2 1.6.1, gradle, xmlbean 2.3.0` (JAXB is not an option in my current project).

java -classpath xbean.jar org.apache.xmlbeans.impl.tool.SchemaCompiler 
     -src <OUTPUT_SOURCE_FOLDER> 
     -out <OUTPUT>.jar <SOURCE>.xsd 
     -compiler "javac.exe" 
     -javasource "1.5"

I had to modify Stanislav's answer a bit to get the plugin working. Namely setting the sourceSets in the following manner:

sourceSets {
    xmlbeans {
        java {
            srcDirs = ['dir/schema']
        }
    }
}

in case someone has the same problem.

As far as I know, you can use the "xmlbeansPlugin" for this. Can't say now, whether it's possible to change the xmlbean's version to the older one.

All you need is to apply the plugin to your build script:

apply from: 'https://raw.github.com/rartavia/gradle-plugins/master/xmlbeansPlugin/xmlbeans.gradle'

Then declare xmlbeans configuration and it's dependency

configurations {
    xmlbeans
}

dependencies {
    xmlbeans 'org.apache.xmlbeans:xmlbeans:2.5.0'
}

After that you have to specify the xsd files location

sourceSets {
    schemas {
            srcDir = 'src/main/schemas'
    }
}

And now you can call compileXmlSchemas task to generate classes

Anyone looking for an answer to this question with latest gradle build (6.x), here is how I was able to configure the project in eclipse (Version: 2020-06 (4.16.0)) and generated Java (8) classes from XSD files automatically with gradle task.

code snippet in build.gradle file

buildscript {
   repositories {
    maven {
        url "https://plugins.gradle.org/m2/"
     }
   }
   dependencies {
     classpath 'gradle.plugin.com.liferay:gradle-plugins-xsd-builder:1.0.10'
  } 
}
dependencies {
  compile "org.apache.xmlbeans:xmlbeans:4.0.0"
  ... //other dependencies
}
.... // other code
apply plugin: "com.liferay.xsd.builder"

After adding the above code, refresh eclipse and then update the gradle task view in eclipse, you should see the task: buildXSD under build group. Add all your XSD files in xsd folder (keep this folder at root of the project folder) and double-click on buildXSD task to generate all Java types from XSD, pretty clean. If you have any future XSD to be added, just drop that in xsd folder and run again the same task. This task will generate a jar file which contains all the Java Types in lib folder of your project.

gradle 项目中的 buildXSD 任务

For curious minds - currently, what we do is following:

  1. Run the jar task under build folder which actually generates a jar file of the project (this project uses the Java classes generated from the XSD for certain webservice calls)
  2. Then copy the both jar files: generated jar file from 1st step + XSD based generated jar files to the deploy folder in local machine
  3. Another gradle task is ran which calls remote machine to stop the server, deploys the jars to the desired location and then starts the server. This gradle task also uses another plugin: https://gradle-ssh-plugin.github.io/ which also works very well.

All the above steps are automated with single gradle task (clean + jar + customCopyTask + deployToServer) which works perfectly to automate this. Note: The jar task also generates XSD based jar file as well. So may be you might want to write a custom jar task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM