Skip to content

ChristianSchulte/jaxb-gradle-plugin

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JAXB Gradle Plugin

Summary

This plugin generates Java code from schema files (see com.sun.tools.xjc.XJCTask) or schema files from existing Java code (see com.sun.tools.jxc.SchemaGenTask).

Usage

To apply the JAXB Gradle Plugin to your projects, add the following in your build script:

build.gradle
plugins {
    id 'com.intershop.gradle.jaxb' version '2.0.0'
}

jaxb {
     // generate java code from schema
     javaGen {
        //generates a 'project' schema file from existing java code
        name {
            schema = file('schema.xsd')
            binding = file('binding.xjb')
        }
    }

    //generates schema from java code
    schemaGen {
        //generates java code for project from project schema
        name {
            javaFiles = fileTree(dir: 'src/main/java', include: 'com/corporate/annotated/**/binding/**/*.java')
            namespaceconfigs = [ 'http://www.corporate.com/xml/ns/corporate/feature/1.0' : 'feature.xsd' ]
        }
    }
}

If the JavaBasePlugin is applied to the project, generated java sources will be added to the specified source set. Per default the main source set is used.

Important
This plugin uses the version 2.2.11. It is possible to change this to 2.2.7 or 2.2.10. It will not work with older versions of JAXB. It is possible to overwrite the project dependency configuration 'jaxb', if you want use an other version of JAXB. The classpath of the xjc can be extended with a project configuration 'jaxb' and dependencies.

Tasks

The JAXB Gradle plugin adds one task jaxb to the project. This task depends on all other plugin task. It is a task added for each configuration in schemaGen and javaGen.

Task name

Type

Description

jaxb

Task

Overall jaxb code generation task of a project. This task depends on all jaxbJavaGen and jaxbSchemaGen tasks.

jaxbJavaGen<configuration name>

com.intershop.build.jaxb.task.SchemaToJavaTask

This task generates Java code for the specified schema and binding configuration.

jaxbSchemaGen<configuration name>

com.intershop.build.jaxb.task.JavaToSchemaTask

This task creates schema files for the specified java code.

Project Extension 'jaxb'

This plugin adds an extension jaxb to the project.

Properties

Property

Type

Default value

Description

xjcVersion

String

'2.2.11'

The version of JAXB.

Methods

Method Values Description

javaGen

SchemaToJava

This contains all Java code generation configurations.

schemaGen

JavaToSchema

This contains all schema code generation configurations.

Object 'javaGen' (SchemaToJava)

Properties
Property Type Default value Description

outputDir

File

<project.buildDir>/generated/jaxb/java/<config name>

Generated code will be written under this directory.

args

List<String>

Additional command line arguments passed to the XJC (-use-runtime, -schema, -dtd, -relaxng, -Xlocator, -Xsync-methods)

sourceSetName

String

'main'

Generated source code will be added to the source set.

encoding

String

'UTF-8'

specify character encoding for generated source files

strictValidation

boolean

true

perform strict validation of the input schema

extension

boolean

false

allow vendor extensions - do not strictly follow the
compatibility rules and app E.2 from the JAXB Spec

header

boolean

true

Generate a header in each generated file indicating that this file is generated by such and such version of JAXB RI when.
If this value is false '-nv' will be added to the command line parameter list.

packageName

String

'generated'

If specified, generated code will be placed under this Java package.

schema

File

null

A schema file to be compiled.

binding

File

null

An external binding file that will be applied to the schema file.

catalog

File

null

Specify the catalog file to resolve external entity references. Support TR9401, XCatalog, and OASIS XML Catalog format. See the catalog-resolver sample for details.

schemas

FileCollection

null

To compile more than one schema at the same time, use this configuration.

bindings

FileCollection

null

To specify more than one external binding file at the same time, use this configuration.

targetVersion

String

'2.2'

Specifies the runtime environment in which the generated code is supposed to run. Expects also 2.0 or 2.1 values. This allows more up-to-date versions of XJC to be used for developing applications that run on earlier JAXB versions.

language

String

'XMLSCHEMA'

Specifies the schema language to compile. Supported values are "WSDL", "XMLSCHEMA", and "WSDL." Case insensitive.

antTaskClassName

String

'com.sun.tools.xjc.XJCTask'

The JAXB tools (e.g. XJC) bundled with the JDK are relocated to a package not matching the JAXB-RI. There are a lot of XJC plugins around compiled against the JAXB-RI which cannot be used with the JAXB tools bundled with the JDK due to this. When configuring the plugin to use the JAXB-RI Ant task instead of the Ant task bundled with the JDK (e.g. com.sun.tools.xjc.XJC2Task), those plugins can be used.

Method

Method

Parameter

Description

arg

String

Add an additional command line arguments passed to the XJC

Object 'schemaGen' (JavaToSchema)

Property Type Default value Description

outputDir

File

<project.buildDir>/generated/jaxb/schema/<config name>

Base directory to place the generated schema files

javaFiles

FileCollection

null

Source files configuration.

namespaceconfigs

Map<String,String>

null

Control the file name of the generated schema. The entry key is the namespace attribute and the value is the file name. When this element is present, the schema document generated for the specified namespace will be placed in the specified file name.

episode

String

null

If specified, generate an episode file in the specified name.

Example Configurations

Simple configuration for java code generation

plugins {
    id 'java'
    id 'com.intershop.gradle.jaxb' version '2.0.0'
}

jaxb {
    javaGen {
        posConfig {
            packageName = ''
            schema = file('pos.xsd')
        }
    }
}

Configuration with binding file for java code generation

plugins {
    id 'java'
    id 'com.intershop.gradle.jaxb' version '2.0.0'
}

jaxb {
    javaGen {
        posConfig {
            binding = file('binding.xjb')
            schema = file('pos.xsd')
        }
    }
}

Configuration with more than one schema file for java code generation

plugins {
    id 'java'
    id 'com.intershop.gradle.jaxb' version '2.0.0'
}

jaxb {
    javaGen {
        ublConfig {
            binding = file('ubl.xjb')
            schemas = fileTree(dir: 'cd-UBL-1.0/xsd', include: '**/*.xsd')
        }
    }
}

Configuration with xew plugin for java code generation

plugins {
    id 'java'
    id 'com.intershop.gradle.jaxb' version '2.0.0'
}

    def xewFile = file('staticfiles/definition/xew-includes.txt')
    def args = ['-Xxew', '-Xxew:instantiate early', '-Xxew:delete', "-Xxew:includeFile \${xewFile.absolutePath.replace('\\','/')}"]

    configurations {
        xjc
    }

    jaxb {
        javaGen {
            gcdm {
                schemas = fileTree(dir: 'staticfiles/definition', include: '**/**/*.xsd')
                binding = file('staticfiles/definition/gcdm-customer-binding.xjb')
                strictValidation = false
                extension = true
                arg.addAll(args)
            }
        }
    }

    dependencies {
        xjc 'com.github.jaxb-xew-plugin:jaxb-xew-plugin:1.1'
    }

Configuration for schema generation

plugins {
    id 'java'
    id 'com.intershop.gradle.jaxb' version '2.0.0'
}

jaxb {
    schemaGen {
        orderstatusimport {
            javaFiles = fileTree(dir: 'javasource', include: 'com/corporate/annotated/**/binding/**/*.java')
            namespaceconfigs = ['http://com.corporate.com/xml/ns/corporate/feature/status/1.0' : 'feature_xml.xsd' ]
        }
    }
}

License

Copyright 2014-2016 Intershop Communications.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Gradle JAXB code generation plugin

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 41.8%
  • HTML 37.9%
  • Groovy 19.9%
  • Shell 0.4%