Output multiple jars with changed variable


is it possible to output multiple jars from the same project, each with a changed variable in the Java source code?
So in jar 1 variable a is set to 1, in jar 2 variable a is set to 2.

In general yes, but can you tell us about the use case first?

Sure. I’m writing a Minecraft mod which patches base classes via ASM. We have many different environments in our network which need different ASM transformers, so I want a jar per environment

Thanks for the example! I’m just asking because it would be much easier if you could do this with a system property instead of creating different code for each environment. Or possibly a Java agent that does the ASM manipulation at load time instead of at build time.

If that’s not possible, then here’s a relatively complete example of the tasks you’d need to enhance classes, create a jar and prepare it for publishing:

//your class enhancing task
task mySpecialClasses() {
  inputs.files fileTree(sourceSets.main.output.classesDir)
  outputs.dir "$buildDir/classes/special/main"
  doLast {
    //invoke your transformer, transforming input classes into the output dir

//build a Jar from the output of the enhancer
task mySpecialJar(type:Jar) {
  from mySpecialClasses
  classifier = 'special'

//tell Gradle that this jar is one of your published archives
artifacts {
  archives mySpecialJar

The ASM manipulation is done during load time

If it’s done during load time, why do you need multiple jars? The code can discover the environment itself at load time and decide which transformation to apply.

Minecraft is a bit different with it’s ASM handling. ASM transformers need to be registered before everything else is loaded, so I cannot identify which transformer to use

But your transformer is a plain old jar. It can contain all code for all platforms and decide what to do based on the environment.