Should 'gradle build' invoke an overridden Jar task?


(James Reynolds) #1

I’m making the initial leap from Maven to Gradle. Our project is a large, multi-module project, and I created the initial build.gradle files using gradle init. No, I’m going through each module and adding code to replace maven plugins. In one project, we’re using Apache XMLBeans to generate java classes from a schema file. I have this working correctly, but I have a question on how to invoke it. Here is the code in question:

description = 'SPI Generated Code for AMP'
  configurations {
    xmlbeans
}
  dependencies {
    xmlbeans 'org.apache.xmlbeans:xmlbeans:2.6.0'
  // todo: update with version property
}
  task compileBeans {
    ant.taskdef(name:'xmlbean', classname: 'org.apache.xmlbeans.impl.tool.XMLBean', classpath: configurations.xmlbeans.asPath)
    ant.xmlbean(
            schema: 'src/main/resources/xsd/schema.xsd',
            classgendir: "${buildDir}/generated-classes/xmlbeans",
            srcgendir: "${buildDir}/generated-sources/xmlbeans",
//
          destfile: "${buildDir}/libs/${project.name}-${project.version}.jar",
     // this is replace in the jar task with the 'from' line
            classpath: configurations.xmlbeans.asPath,
            memoryInitialSize: '50m',
            memoryMaximumSize: '512m')
}
  task jar(dependsOn: compileBeans, type: Jar, overwrite: true) {
    println 'running custom jar task'
    from "${buildDir}/generated-classes/xmlbeans"
}

When I run gradle jar, things work as I expect. However, if i run gradle build, the standard Jar task runs and creates empty jars for me. My assumption is that I will be running gradle build on the root project to build from the groud up - and my project will be missing this dependency. How do I ensure that my overwritten task is run?


(Peter Niederwieser) #2

‘overwrite: true’ has known issues and is best avoided. In any case, I can’t think of a good reason to overwrite and then use the same task type as the original task had (‘Jar’).

PS: ‘taskBeans’ is missing a ‘doLast { … }’.


(James Reynolds) #3

Thanks for the help. After re-reading Chapter 23 of the User Guide, I think I have a better handle on the conventions. I moved the dependsOn to the compileBeans task and stopped trying to override the jar task. Now, I’m just configuring it to pull from the generated classes that I know are there. I get the jar file I’m expecting when I run gradle build.

Now if I could just get the sources jar to build too…

...
task compileBeans(dependsOn: classes) << {
        println 'running compileBeans task'
        ant.taskdef(name:'xmlbean', classname: 'org.apache.xmlbeans.impl.tool.XMLBean', classpath: configurations.xmlbeans.asPath)
        ant.xmlbean(
                schema: 'src/main/resources/xsd/schema.xsd',
                classgendir: "${buildDir}/generated-classes/xmlbeans",
                srcgendir: "${buildDir}/generated-sources/xmlbeans",
                classpath: configurations.xmlbeans.asPath,
                memoryInitialSize: '50m',
                memoryMaximumSize: '512m')
}
jar {
    println 'packing jar directory'
    from "${buildDir}/generated-classes/xmlbeans"
}