代码之家  ›  专栏  ›  技术社区  ›  arseniyandru

在gradle中使用ant XJC从JAR编译多个XSD

  •  1
  • arseniyandru  · 技术社区  · 8 年前

    因此,我可以使用AntXJC从契约jar中的一个xsd生成类。

     ant.xjc(package: packageName, destdir: project.ext.generatedSrcDir,    
     extension: 'true',
     schema: "jar:file:///$pathToContractJar!/MySchema.xsd")
    
    2 回复  |  直到 8 年前
        1
  •  6
  •   lance-java    8 年前
    configurations {
        jaxb
    }
    dependencies {
        jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.6'
        jaxb 'com.sun.xml.bind:jaxb-impl:2.2.6'
        jaxb 'javax.xml.bind:jaxb-api:2.2.6'    
    }
    task xjc {
        def xsds = zipTree(pathToContractJar).matching { 
            include: '*.xsd' 
        }
        inputs.dir "src/main/resources/bindings"
        inputs.files xsds
        outputs.dir "$buildDir/xjc"
        doLast {
            System.setProperty('javax.xml.accessExternalSchema', 'all')
            mkdir "$buildDir/xjc/result"
            mkdir "$buildDir/xjc/xsd"
    
            copy {
                from xsds
                into "$buildDir/xjc/xsd"
            }
            ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.jaxb.asPath)
            ant.xjc(
                destdir: "$buildDir/xjc/result",
                package: packageName,
            ) {
                schema(dir: "$buildDir/xjc/xsd", includes: '*.xsd')
                binding(dir: "src/main/resources/bindings", includes: '*.xjb')
                arg(value: '-verbose')
            }
        }
    }
    
        2
  •  3
  •   Martin Traverse    4 年前

    我使用了很长一段时间,但我发现迁移到Java11/Gradle6时,它不再工作了。Java 11没有JAXB API,因此需要将其作为依赖项添加。此外,在Gradle内部使用Ant调用XJC的机制不想工作,可能我只是没有找到标志的神奇组合!相反,可以调用JAR清单中列出的XJC主类,完全省去了Ant。这里的配置使用Gradle6.3和Java11为我工作。

    我还没有尝试过在JAR+绑定文件中使用这种方法,但是根据使用信息,参数是可用的,所以它应该可以工作!

    this question

    sourceSets {
    
        generated {
            java.srcDir "$generated_dir"
        }
    }
    
    dependencies {
    
        compile sourceSets.generated.output
    
        // Generated code depends on the JAXB API, which is removed from base Java in JDK 11
        compile "org.glassfish.jaxb:jaxb-runtime:2.3.3"
        generatedCompile "org.glassfish.jaxb:jaxb-runtime:2.3.3"
    }
    
    
    // XJC tasks
    
    // JAXB configuration holds classpath for running the JAXB XJC compiler
    configurations {
        jaxb
    }
    
    dependencies {
    
        jaxb "org.glassfish.jaxb:jaxb-xjc:2.3.3"
    }
    
    // Cookie cutter function for defining multiple XJC tasks
    // (not necessary if you only have a single task)!
    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
            main '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
        }
    
        // Add a dependency on the new task so it gets invoked
        compileGeneratedJava.dependsOn tasks.getByName(taskName)
    }
    
    // Add all the XJC tasks you need
    
    addXjcTask("xjcSchema1",
            "path/to/schema1.xsd",
            'com.example.generated.schema1',
            "$generated_dir")
    
    addXjcTask("xjcSchema2",
            "path/to/schema2.xsd",
            'com.example.generated.schema2',
            "$generated_dir")