Run gradle shadowjar task twice in the same build file

I am trying to create two "fatJars" using the ShadowJar plugin as part of the same build file. I am trying to run the shadowJar task twice inside the assembly by declaring two tasks like ShadowJar

So far I have defined two tasks:

task shadowjar_one (type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) task shadowjar_two (type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) 

and now trying to create my banks as follows:

 shadowjar_one { mergeServiceFiles() exclude 'somefile.txt' archiveName = 'jar1.jar' appendManifest { attributes 'Main-Class': 'some.package.someClass' } } shadowjar_two { mergeServiceFiles() exclude 'someOtherfile.txt' archiveName = 'jar2.jar' appendManifest { attributes 'Main-Class': 'some.package.someOtherClass' } } 

The problem I encountered is that the banks are created, but they do not contain any other dependencies (packages, files, etc.) on the "other" banks. The jars contain only META-INF and the current catalogs of the project packages.

Any idea what could be the problem?

Note: I expect two slightly different jar files to be released. Both should have the same project code base with differences in the Main-Class attribute of the manifest (and a few more minor differences)

Many thanks!

+5
source share
3 answers

The creator of the shadow plugin is here - I was just aware of this question here. You are faced with the fact that the Shadow plugin creates and configures the shadowJar task, using a set of specific conventions for this task.

When you create your own tasks using this type, you need to manually define several of these configuration parameters, since the plugin will not be able to understand what your intentions are with these tasks.

Here you can refer to the configuration that applies to the built-in task: https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy # L38-L63

+4
source

A workaround that I used is to have one shadowJar task but pass parameters. In your case, something like:

 shadowJar { mergeServiceFiles() exclude System.properties.getProperty('exclude') archiveName = System.properties.getProperty('archiveName') appendManifest { attributes 'Main-Class': System.properties.getProperty('mainClass') } } 

Then, when starting the application:

 gradlew shadowJar -Dexclude=... -DarchiveName=... -DmainClass=... 
+2
source

The author gave a very good solution (in that it is both short and working):

https://github.com/johnrengelman/shadow/issues/108

I really use the setting for this solution that appears at the bottom of this page (I added comments to explain a little):

 task bootstrapNodeJar(type: ShadowJar) { group = "shadow" // Not a must have, but it always good to have a group, you can chose whichever - this is the one shadowJar belongs to description = "Builds a Bitsquare bootstrap node executable jar" // Same as the above manifest.attributes 'Main-Class': 'io.bitsquare.app.cli.BootstrapNodeMain' // The main attraction! Be sure to update this line classifier = 'bootstrapNode' // General jar task property - see more about it in the Gradle manual from(project.convention.getPlugin(JavaPluginConvention).sourceSets.main.output) // Leave as is configurations = [project.configurations.runtime] // Same as the above exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA') // This one is actually really important! // Here you can add other Jar properties like destinationDir, for example } 
+2
source

Source: https://habr.com/ru/post/1200324/


All Articles