Gradle xjc ant task always executed

Hi everyone,

I have a Gradle task in which I have declared and invoked an Ant task.
If I launch the gradle task(named “jaxb”) from command line it works correctly, but it is always executed, even if I launch another task such as “gradle jar”,“gradle compileJava” or “gradle build”.
Is there any way to avoid this?

My build.gradle file following,

Thanks in advance
Paolo

apply plugin: 'java'

group = 'it.ipzs.ws'
version = '2.1.1'
targetCompatibility = '1.5'

repositories {
    jcenter()
    mavenLocal()
}

configurations{
    xjcConf
}
  
dependencies {
   xjcConf 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
   xjcConf 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
   xjcConf 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.6.5'
   xjcConf 'org.jvnet.jaxb2_commons:jaxb2-basics:0.6.4'
   xjcConf 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:0.6.4'
}

task jaxb {
    ant.taskdef(
        name: 'antXjc',
        classname: 'org.jvnet.jaxb2_commons.xjc.XJC2Task',
        classpath: configurations.xjcConf.asPath
    )
    File schemaDir = file('resources/it/ipzs/ws/xml/schema')
    File bindingDir = file('resources/it/ipzs/ws/xml/binding')
    FileCollection schemaFiles = files { schemaDir.listFiles() }
    FileCollection bindingFiles = files { bindingDir.listFiles() }
    schemaFiles.each {
        String schemaName = it.name
        String schemaNamePrefix = (schemaName =~ /^(\w+)\./)[0][1]
        File bFile = bindingFiles.find{it.name.startsWith(schemaNamePrefix)}
        String bindingName = bFile.name
        print "Processing file $schemaName..."
        copy {
                from "resources/it/ipzs/ws/xml/schema/$schemaName"
                into "resources/it/ipzs/ws/temp/schema"
        }
        copy {
                from "resources/it/ipzs/ws/xml/binding/$bindingName"
                into "resources/it/ipzs/ws/temp/binding"
        }
        ant.antXjc(
                destdir: "src",
                package: 'it.ipzs.ws.generated',
                schema: "resources/it/ipzs/ws/temp/schema/$schemaName",
                binding: "resources/it/ipzs/ws/temp/binding/$bindingName",
                extension: 'true'){
            arg(value: "-Xinheritance")
        }
        delete("resources/it/ipzs/ws/temp/schema/$schemaName")
        delete("resources/it/ipzs/ws/temp/binding/$bindingName")
        println "...Done!"
    }
    delete("resources/it/ipzs/ws/temp")
}

jar {
    manifest {
            attributes 'Implementation-Title' : 'gestionelotti-ws',
                       'Implementation-Version' : version
        }
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
        resources {
            srcDir 'resources'
        }
    }
}

compileJava {
    targetCompatibility = '1.5'
}

You are making the common mistake of executing in the configuration phase rather than the execution phase. You should declare your logic in a doLast closure (you can use the << short-hand). Eg:

task jaxb << {
   ... 
} 
1 Like

The << shorthand is discouraged, as it is so easy to miss the difference. We will probably deprecate it at some point. In the interest of future readers of your code, always use doLast {} :slight_smile:

Could you give an example of using doLast to create a stand-alone task? Thanks

task jaxb {
   doLast {
      ...
   }
}
1 Like