Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile multiple jars from single source project using Gradle

I am using Gradle to build a library that is meant to be used in Spark applications. Such libraries are typically made available in multiple combinations of Scala version and Spark version. For example, for spark-testing-base - you can see that multiple artifacts are availble on mvn cntrl, often for each Scala/Spark combination.

I am looking for an elegant way to do this build.

I saw in this question a solution for building multiple jars from the same source, which is what I want, but it will involve manually writing subprojects for each combination. (in the linked question there are only two artifacts built, and I want to build at least nine, maybe more).

So my question - is there a better way to do this, or is the solution described above the only way?

UPDATE: I saw two Gradle plugins that might be useful, but haven't had time to check them out yet - links below for anyone who's interested:

https://github.com/ADTRAN/gradle-scala-multiversion-plugin

https://github.com/uklance/gradle-java-flavours

like image 545
Eyal Avatar asked Nov 07 '22 01:11

Eyal


1 Answers

You might be able to accomplish this using configurations.

Try to declare configurations in nested loops over the version combinations of dependencies you want to build with.
Then, assign the various combinations of dependencies to the configurations.
Create archiving tasks, for all the configurations.
Finally, declare artifacts for all the configurations.

E.g.:

scalaDependencies.each { scalaDep ->
    sparkDependencies.each { sparkDep ->

        def configurationName = buildConfigurationName(scalaDep, sparkDep)

        configurations.create(configurationName)
        configurations."$configurationName" {
            extendsFrom compile // this would be the simplest case
        }

        dependencies {
            "$configurationName"(scalaDep)
            "$configurationName"(sparkDep)
        }

        task("${configurationName}Jar", type: Jar) {
            from "$configurationName"
        }

        artifacts {
            "$configurationName"("${configurationName}Jar")
        }
    }
}

The scalaDependencies and sparkDepdendencies would just be the strings you normally declare as dependencies, including the respective version numbers.
The method/closure buildConfigurationName you will have to figure out yourself ;)
I am coding this from the hip right now, but I am fairly sure this will work if you fill out the remaining bits.
You might also need another block to actually create the mavenPublications, but if it works to the point of creating the Jars, I think you can solve that.

I have a working example that is somewhat similar, declaring multiple sourceSets and artifacts from a list of names, that might also be of help.
Find it here: https://github.com/thokari/gradle-workshop/blob/master/examples/09-multiple-artifacts/build.gradle

like image 77
Thomas Hirsch Avatar answered Nov 13 '22 06:11

Thomas Hirsch