如何使用gradle运行jaxb xjc任务?

问题描述 投票:0回答:1

我为 jaxb/xjc 选择了 3 个 gradle 插件,但没有一个对我有用。

对于每种情况,我都将 Maven 模型 xsd

maven-4.0.0.xsd
https://maven.apache.org/xsd/maven-4.0.0.xsd 放在
src/main/xsd
中。 (这只是一个例子,任何其他xsd都可以)

  • no.entitas.gradle.jaxb:gradle-jaxb-插件:2.0

http://github.com/stianh/gradle-jaxb-plugin

此插件在

src/<source set>/xsd/**/*.xsd
中查找 xsd 文件。

这是

build.gradle

apply plugin: 'jaxb'

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

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0'
  }
}

错误:

* What went wrong:
A problem occurred evaluating root project 'test-gradle'.
> Failed to apply plugin [id 'jaxb']
   > Could not find method add() for arguments [jaxb, no.entitas.gradle.jaxb.plugin.JaxbPlugin$_apply_closure1@62c72501] on configuration container.
  • org.hibernate.build.gradle:gradle-xjc-plugin:1.0.2.Final

https://github.com/hibernate/gradle-xjc-plugin

这是

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.hibernate.build.gradle:gradle-xjc-plugin:1.0.2.Final'
    }
}

apply plugin: 'org.hibernate.build.gradle.xjc'

dependencies {
    xjc 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
    xjc 'org.jvnet.jaxb2_commons:jaxb2-basics:0.9.3'
    xjc 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.9.3'
}

xjc {
    xjcTaskName = 'org.jvnet.jaxb2_commons.xjc.XJC2Task'
    // access the schemas NamedDomainObjectContainer
    schemas {

        // and add a new SchemaDescriptor to it under the name 'cfg'
        cfg {
            // and now, configure the SchemaDescriptor
            xsd = file( 'src/main/xsd/maven-4.0.0.xsd')
        }
    }
}

错误:

* What went wrong:
Neither path nor baseDir may be null or empty string. path='null' basedir='C:\Users\xxxxx\eclipse-workspace\test-gradle'
  • com.intershop.gradle.jaxb:jaxb-gradle-插件:5.1.0

https://github.com/IntershopCommunicationsAG/jaxb-gradle-plugin

这是

build.gradle

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

jaxb {
    javaGen {
        posConfig {
            packageName = 'org.apache.maven.model'
            schema = file('src/main/xsd/maven-4.0.0.xsd')
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    jaxb 'com.sun.xml.bind:jaxb-xjc:3.0.1'
    jaxb 'com.sun.xml.bind:jaxb-jxc:3.0.1'
    jaxb 'com.sun.xml.bind:jaxb-impl:3.0.1'
    jaxb 'com.sun.xml.bind:jaxb-core:3.0.1'
    jaxb 'org.glassfish.jaxb:jaxb-runtime:3.0.1'
    jaxb 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.1'
    jaxb 'jakarta.activation:jakarta.activation-api:2.0.1'
}

错误:

* What went wrong:
An exception occurred applying plugin request [id: 'com.intershop.gradle.jaxb', version: '5.1.0']
> Failed to apply plugin [id 'com.intershop.gradle.jaxb']
   > Could not generate a proxy class for class com.intershop.gradle.jaxb.extension.JaxbExtension.

jacobono/gradle-jaxb-plugin 使用相对目录并创建目标目录:

    buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath "com.github.jacobono:gradle-jaxb-plugin:1.3.5"
      }
    }
    
    apply plugin: "com.github.jacobono.jaxb"
    
    repositories {
      mavenCentral()
    }
    
    sourceSets {
        main {
            java {
                srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java']
            }
            resources {
                srcDirs = ['src/main/resources']
            }
        }
    }
    
    dependencies {
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1'
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0'
      jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
      jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
      jaxb 'javax.xml.bind:jaxb-api:2.2.11'
      jaxb 'org.slf4j:slf4j-nop:1.7.30'
    }
    
    jaxb {
      xsdDir = "src/main/xsd"
      xjc {
         taskClassname        = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
         generatePackage      = "org.apache.maven.model"
         destinationDir       = "build/generated-sources/xjc"
         args                 = ["-Xinheritance", "-Xannotate"]
      }
    }
    
    compileJava.dependsOn 'xjc'

rackerlabs/gradle-jaxb-plugin使用绝对目录,并且仅当目标目录是默认目录时才创建目标目录${project.buildDir}/generated-sources/xjc

buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath 'gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.5.0' } } apply plugin: "org.openrepose.gradle.plugins.jaxb" repositories { mavenCentral() } sourceSets { main { java { srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java'] } resources { srcDirs = ['src/main/resources'] } } } dependencies { jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1' jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1' jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0' jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11' jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11' jaxb 'javax.xml.bind:jaxb-api:2.2.11' jaxb 'org.slf4j:slf4j-nop:1.7.30' } jaxb { xsdDir = "${project.projectDir}/src/main/xsd" xjc { generateEpisodeFiles = false taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task" generatePackage = "org.apache.maven.model" args = ["-Xinheritance", "-Xannotate"] } } compileJava.dependsOn 'xjc'

    Gradle版本:2.3(这是我必须使用的版本)
  • JDK版本:1.8.0_311
命令:gradle构建

gradle jaxb xjc
1个回答
13
投票
这是一个不使用特定插件的示例(仅

java

),以及jaxb-xjc的
v4

(配置预计

maven-4.0.0.xsd

 位于 src/main/resources

plugins { id 'java' } group 'com.example.xjc-demo' archivesBaseName = "xjcdemo" version '1.0.0-SNAPSHOT' repositories { mavenCentral() } def generated_dir = "src/main/java" sourceSets { generated { java.srcDir "$generated_dir" } } // JAXB configuration holds classpath for running the JAXB XJC compiler configurations { jaxb } dependencies { jaxb "org.glassfish.jaxb:jaxb-xjc:4.0.1" jaxb "org.glassfish.jaxb:jaxb-runtime:4.0.1" // The next two dependencies are not necessary for generating code, only when running the code: // Generated code depends on the JAXB API, which is removed from base Java in JDK 11, and therefore needs to be added implementation "org.glassfish.jaxb:jaxb-runtime:4.0.1" implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0' // In JAXB v3, 'javax.xml.bind' was moved to 'jakarta.xml.bind' } // Cookie cutter function for defining multiple XJC tasks def addXjcTask(taskName, schema, pkg, dest) { // If you haven't already, create the generated output dir before running XJC or it will fail file(dest).mkdirs() // The main XJC task, calls XJCFacade which is the entry point of the XJC JAR tasks.create(name: taskName, type: JavaExec) { classpath configurations.jaxb mainClass = 'com.sun.tools.xjc.XJCFacade' // To explore available args, download the XJC JAR and run java -jar jaxb-xjc.jar --help args schema, "-p", pkg, "-d", dest, "-no-header" } } // Add all the XJC tasks you need addXjcTask("maven-xsd", "src/main/resources/maven-4.0.0.xsd", "${archivesBaseName}.my_maven_package_name", "$generated_dir") tasks.register("generateSources") { description "Generate Java sources fram XSD files" dependsOn tasks.getByName("maven-xsd") }
使用 

./gradlew generateSources

 运行(已使用 Gradle v7.4.2 验证)。

© www.soinside.com 2019 - 2024. All rights reserved.