Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven jaxb2 plugin in Gradle

Tags:

I need to migrate a maven project to gradle. The maven project uses the maven-jaxb2-plugin like this (version for the plugin is set in a root pom.xml):

<plugin>
  <groupId>...</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <generatePackage>package for generated classes</generatePackage>
    <schemaDirectory>directory containing XSD files</schemaDirectory>
    <includeSchemas>
        <includeSchema>XSD file name</includeSchema>
        <includeSchema>XSD file name</includeSchema>
        ...
    </includeSchemas>
    <strict>true</strict>
    <verbose>true</verbose>
    <extension>true</extension>
  </configuration>
<plugin>

So, I wanted to achieve the same functionality in gradle, and this is what I have:

plugins {
  id "com.github.jacobono.jaxb" version "1.3.5"
}
dependencies {
  jaxb "org.glassfish.jaxb:jaxb-runtime:2.2.11"
  jaxb "org.glassfish.jaxb:jaxb-xjc:2.2.11"
}
jaxb {
  xsdDir = "directory containing XSD files"
  xjc {
    taskClassname = "com.sun.tools.xjc.XJC2Task"
    generatedPackage = "package for generated classes"
  }
}
compileJava.dependsOn xjc

This project is part of a multi-project build with dependencies on other projects etc., but I don't think those are relevant.

Am I on the right track?? I'm asking because the behavior doesn't seem to be the same when I do mvn clean install and gradle clean build

Question:

  • Is there a way to specify the XSD file names we want to use in gradle (as we do using includeSchema in maven)?

My problem:

like image 694
Farzad Avatar asked May 09 '16 13:05

Farzad


People also ask

Can Maven plugins be used in gradle?

You can't use a Maven plugin as-is in Gradle; you'll have to port it to a Gradle plugin. How difficult this is depends on how many Maven APIs the plugin is using. Another strategy might be to call into Maven via Gradle's Exec task.

What is Maven jaxb2 plugin?

This plugin uses the Java API for XML Binding (JAXB), version 2+, to generate Java classes from XML Schemas (and optionally binding files) and to create XML Schemas from annotated Java classes.


1 Answers

This is how I achieved this task after doing the research. This worked as expected. Add followings to your build file.

buildscript {

    dependencies {
        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-b41'
    jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7-b41'
    jaxb 'javax.xml.bind:jaxb-api: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"
    jaxb "org.jvnet.jaxb2_commons:jaxb2-value-constructor:3.0"
}


jaxb {

    System.setProperty('javax.xml.accessExternalSchema', 'all') //To solve external schema dependencies
    xsdDir = "src/main/resources/schema/" //xsd directory
    xjc {
        taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task" // This is for setter plugin
        args = ["-Xsetters","-Xsetters-mode=direct"]
    }
}

you should run 'gradle xjc' to generate related java files from xsd files.

like image 91
user3011958 Avatar answered Sep 28 '22 02:09

user3011958