我有一个带有自定义defind xjc任务的多项目Gradle构建,用于构建jaxb生成的对象,而按正确的顺序构建步骤时遇到问题。

我有3个项目,common,ref和product。 ref取决于common,而product取决于ref和common。命名对我的问题很重要,因为gradle似乎按照字母顺序进行操作,并且我删除了一些其他依赖项,因为它们不会影响问题。

在每个项目中,顺序应为jaxb,java编译,然后是scala编译。

在顶层build.gradle中,我将jaxb任务指定为:

task jaxb() {
    description 'Converts xsds to classes'
    def jaxbTargetFile = file( generatedSources )
    def jaxbSourceFile =  file ( jaxbSourceDir )
    def jaxbEpisodesFile =  file ( jaxbEpisodeDir )
    def bindingRootDir =  file ( rootDir.getPath() + '/')

    inputs.dir jaxbSourceDir
    outputs.dir jaxbTargetFile
    doLast {
        ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.jaxb.asPath)

        jaxbTargetFile.mkdirs()
        jaxbEpisodesFile.mkdirs()

        for ( xsd in bindingsMap) {
            if (!episodeMap.containsKey(xsd.key)) {
                ant.fail( "Entry no found in the episodeMap for xsd $xsd.key" )
            }
            def episodeFile = projectDir.getPath() + '/' + jaxbEpisodeDir + '/' + episodeMap.get(xsd.key)
            println( "Processing xsd $xsd.key with binding $xsd.value producing $episodeFile" )
            ant.xjc(destdir: "$jaxbTargetFile", extension: true, removeOldOutput: true) {
                    schema(dir:"$jaxbSourceFile", includes: "$xsd.key")
                    binding(dir:"$bindingRootDir" , includes: "$xsd.value")
                    arg(value: '-npa')
                    arg(value: '-verbose')
                    arg(value: '-episode')
                    arg(value: episodeFile)
            }
        }
    }
}

在我指定的产品的单独build.gradle文件中(在ref中类似)
dependencies {
     compile project(':common')
     compile project(':ref')
}

在所有三个项目中
compileJava.dependsOn(jaxb)

当我在产品项目中运行发布(或jar)时,可以看到以下输出:
common:jaxb
common:compileJava
common:compileScala
common:jar
product:jaxb
ref:jaxb
refcompileJava
ref:compileScala
ref:jar
product:compileJava
product:compileScala

这给我一个错误,因为产品中的xsd引用了ref,并且由于ref尚未运行jaxb,但没有用于ref的情节绑定(bind)文件,并且product用错误的包名称重新生成了导入的类。

如何确保ref jaxb在产品jaxb之前运行?

最佳答案

如果您产品的jaxb任务依赖于ref和common的jaxb任务,则应定义此依赖关系:

(在product build.gradle中)

task jaxb(dependsOn: [':common:jaxb', ':ref:jaxb']) {
...
}

ref中设置同类依赖(在commmon上)

10-04 23:17
查看更多