0

There's a lot of other related questions on this, but I haven't been able to get anything working here.

I'm using shadowjar in a subset of my subprojects to product a far jar. One of the subprojects produces two jars (different main class). I'm trying to eliminate the boilerplate in each subproject.

I've tried a few things, but using "extra" seems like the most canonical approach I've tried so far. But, it doesn't work.

In the root, I have:

subprojects {

    afterEvaluate {

        val jarFiles by project.extra(listOf<String>())
        val mainClasses by project.extra(listOf<String>())

        if (jarFiles != null && jarFiles.isNotEmpty()) {
            jarFiles.forEachIndexed { idx, jarName ->
                tasks.create<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>(jarName) {
                    isZip64 = true
                    archiveBaseName.set(jarName)
                    mergeServiceFiles()
                    manifest {
                        attributes(mapOf("Main-Class" to mainClasses!![idx]))
                    }
                    minimize()
                }

                tasks.build {
                    dependsOn(jarName)
                }
            }
        }
    }

And then in the subprojects I've tried:

extra["jarFiles"] = listOf("myproject")
extra["mainClasses"] = listOf("com.foo.Application")

or

val jarFiles by extra(listOf("internal", "external"))
val mainClasses by extra(listOf("com.fooInternalApplication", "com.foo.ExternalApplication"))

However, it's not getting fired. Am I close or should I take an entirely different approach?

fridgepolice
  • 349
  • 2
  • 12

1 Answers1

0

Working solution:

subprojects { 

    // this is needed so that the subproject can )optionally) set it
    extra["jars"] = null

    afterEvaluate {
        // this may have been set by the subproject
        val jars = extra["jars"] as Map<String, String>?

        jars?.forEach { jar, className ->
            tasks.create<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>(jar) {
                isZip64 = true
                archiveBaseName.set(jar)
                mergeServiceFiles()
                manifest {
                    attributes(mapOf("Main-Class" to className))
                }
                exclude("META-INF/INDEX.LIST", "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA")
                minimize()
                configurations = listOf(project.configurations.default.get())
                // TODO see https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy#L38
                // should be able to refer to something along the lines of "conventions"
                from("./build/classes/kotlin/main")
            }

            tasks.build {
                dependsOn(jar)
            }
        }
    }
}

Then, in each subproject that needs one or more jars built:

extra["jars"] = mapOf(project.name to "com.foo.MainApplication")

or

extra["jars"] = mapOf("internal" to "com.foo.InternalApplication",
                      "external" to "com.foo.ExternalApplication")
fridgepolice
  • 349
  • 2
  • 12