当前位置:  开发笔记 > 编程语言 > 正文

如何使用gradle,jaxb和xjc从xsd生成jaxb类,类应该有XmlRootElement

如何解决《如何使用gradle,jaxb和xjc从xsd生成jaxb类,类应该有XmlRootElement》经验,为你挑选了2个好方法。

我们尝试使用gradle,xsd和xjc生成JAXB类,而JAXB类应该具有XmlRootElement注释,因此它可以用于公开为Web服务响应.我们正在关注此链接http://azagorneanu.blogspot.com/2011/09/configure-maven-to-generate-classes.html,它有很大的帮助,但我们无法找到一个只有gradle的特定示例.所以我们想出了一些我们将分享的答案.



1> aamir..:

build.gradle应如下所示

    buildscript {
    repositories {
    mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "net.saliman:gradle-cobertura-plugin:2.2.4"
        classpath 'com.github.jacobono:gradle-jaxb-plugin:1.3.5'

    }
}
apply plugin: 'com.github.jacobono.jaxb'
dependencies {
    jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7'
    jaxb "org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.6.5"
    jaxb "org.jvnet.jaxb2_commons:jaxb2-basics:0.6.4"
    jaxb "org.jvnet.jaxb2_commons:jaxb2-basics-annotate:0.6.4"
}
configurations {
    jaxb
}
task jaxb(){
    description 'Converts xsds to classes'
    def jaxbTargetDir = file("generated")
    doLast {
    jaxbTargetDir.mkdirs()
    ant.taskdef(name: 'xjc', classname: 'org.jvnet.jaxb2_commons.xjc.XJC2Task', classpath: configurations.jaxb.asPath)
    ant.jaxbTargetDir = jaxbTargetDir 
    ant.xjc(destdir: '${jaxbTargetDir}', package: 'com.sample.jaxbclasses', schema:'generated/schema.xsd', binding:'generated/binding.xjb', extension:'true'){
        arg(value: "-Xannotate")
        }
    }
}

schema.xsd

    
    

    
        
            
            
            
        
    

    
        
            
        
    


binding.xjb



    
        
        

        
        
    

    
    
        
            
                
            
        
        
            
                
            
        
    

下面的binding.xjb也可以使用



  
    
      
    
  

现在你可以运行任务' jaxb ',All set.干杯!

User.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7 
// See http://java.sun.com/xml/jaxb 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2017.01.26 at 11:59:18 AM EST 
//


package com.sample.jaxbclasses;

import java.io.Serializable;
import java.util.Calendar;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


/**
* 

Java class for user complex type. * *

The following schema fragment specifies the expected content contained within this class. * *

* <complexType name="user">
*   <complexContent>
*     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
*       <all>
*         <element name="id" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
*         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
*         <element name="registrationDate" type="{http://www.w3.org/2001/XMLSchema}dateTime"/>
*       </all>
*     </restriction>
*   </complexContent>
* </complexType>
* 
* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "user", propOrder = { }) @XmlRootElement(name = "user") public class User implements Serializable { private final static long serialVersionUID = 1L; protected Long id; @XmlElement(required = true) protected String name; @XmlElement(required = true, type = String.class) @XmlJavaTypeAdapter(Adapter1 .class) @XmlSchemaType(name = "dateTime") protected Calendar registrationDate; /** * Gets the value of the id property. * * @return * possible object is * {@link Long } * */ public Long getId() { return id; } /** * Sets the value of the id property. * * @param value * allowed object is * {@link Long } * */ public void setId(Long value) { this.id = value; } /** * Gets the value of the name property. * * @return * possible object is * {@link String } * */ public String getName() { return name; } /** * Sets the value of the name property. * * @param value * allowed object is * {@link String } * */ public void setName(String value) { this.name = value; } /** * Gets the value of the registrationDate property. * * @return * possible object is * {@link String } * */ public Calendar getRegistrationDate() { return registrationDate; } /** * Sets the value of the registrationDate property. * * @param value * allowed object is * {@link String } * */ public void setRegistrationDate(Calendar value) { this.registrationDate = value; } }



2> grep..:
group 'com.example'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8
targetCompatibility = 1.8


repositories {
    mavenCentral()
}

project.ext {
    jaxbTargetDir = file("src/generated/java")

}


configurations {
    xsd2java
}

dependencies {
    xsd2java "com.sun.xml.bind:jaxb-xjc:2.2.6"
    xsd2java "com.sun.xml.bind:jaxb-impl:2.2.6"
}

task xsd2java() {

    doLast {
        jaxbTargetDir.mkdirs()

        ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.xsd2java.asPath)
        ant.jaxbTargetDir = jaxbTargetDir


        ant.xjc(
                destdir: '${jaxbTargetDir}',
                package: 'com.example.request',
                schema: 'src/main/resources/XMLreq.xsd'
        )

        ant.xjc(
                destdir: '${jaxbTargetDir}',
                package: 'com.example.response',
                schema: 'src/main/resources/XMLres.xsd'
        )

    }
}
compileJava.dependsOn xsd2java

推荐阅读
手机用户2402851155
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有