1

I needed to wrap a maven build with gradle. Bad idea, right, got it, understood. However, I have a large number of code bases that use gradle already, and some that still use maven. I have a set of gradle tools that do all my branching, deployments, and a lot of cool stuff. I want to leverage this for my maven projects too.

My top level task is "deb", as I'm building debian packages. My boilerplate infrastructure tasks depend on "deb" so regardless of how each project implements it, it works, regardless of the underlying build infrastructure, maven, make, ant, whatever. Each project just needs to define a "deb" task and make it dependent on whatever is needed to build the output debian packages. That way the infrastructure gradle scripts don't care about the specific project implementation.

My main question is, how to generate the dependencies within gradle such that I wasn't building unnecessarily.

Bruce Edge
  • 1,975
  • 1
  • 23
  • 31

1 Answers1

0

Here are the gradle tasks I ended up with.

The gist of it is that I needed to come up with some rules to calculate the input and output files generated by the project's build system. In this example, it's simple because java depends on *.java, *.xml, and **/pom.xml and that's it. My outputs are all debian packages so a *.deb filespec is all that's needed for this. The setNewVersion task allows me to keep my base version string in gradle.properties. I use the gmaven plugin to read that into the maven newVersion string. (See Maven2 property that indicates the parent directory)

def env = System.getenv()    // get env to pass in to child build
def sourceBaseDir = "source" // Source dir for the wrapped project

// Set the maven newVersion using gradle.properties.
task setNewVersion(type:Exec) {
    group = "build"
    description = "(nim) Copy newVersion string into maven variable"
    commandLine "bash", "-c", "sed -e \"s/version/newVersion/\" gradle.properties > source/build.properties"
}

// Build the maven targets
task mvnBuild(type:Exec, dependsOn: setNewVersion) {
    group = "build"
    description = "(nim) Build store with maven - TODO: uses -DskipTests because mvn build from gradle fails with junit running."
    environment = System.getenv()
    workingDir = sourceBaseDir
    inputs.files fileTree(sourceBaseDir) {
         include '**/src/**/*.java', '**/src/**/*.xml', '**/pom.xml'
    }
    outputs.files fileTree(sourceBaseDir) {
        include '**/target/*.deb'
    }
    println "inputs=${inputs.getFiles() .getFiles()}"
    println "outputs=${outputs.getFiles().getFiles()}"
    commandLine "mvn", "-DskipTests", "package"
}

// Maven clean target
task mvnClean(type:Exec) {
    group = "build"
    description = "(nim) Clean store with maven, depends on local and remote repe cleanup as well"
    workingDir = sourceBaseDir
    commandLine "mvn", "clean"
}

task deb(dependsOn: "mvnBuild")
Community
  • 1
  • 1
Bruce Edge
  • 1,975
  • 1
  • 23
  • 31
  • The -DskipTests is there because for this specific project, they fail when run under gradle. This is a problem I still need to solve, but doesn't prevent it from being usable as-is. – Bruce Edge Sep 26 '15 at 23:09