How to include a Zip file inside a ShadowJar during Gradle build

2.1k Views Asked by At

I am providing below what I have at the moment.

In the example below, the Jar task produces a Jar with a Zip file ( artifact from another project ) inside it.

But, My ultimate aim is to produce an uber jar that will self contain it's dependencies. I came by Shadow plugin and it seems a clean solution.

I tried to tell my ShadowJar task to include the Zip file - but it doesn't work. See commented out ShadowJar section.

So, what I have now is to create the shadow jar but then producing another jar that includes the contents of shadow jar and the zip. I can see that this path is full of gotchas (I had to enforce the Manifest again) ....

Ideally I would think that there is an way to include a artifact from different configuration in Shadow Jar and it is my limitation of Gradle knowledge that is failing here.


    buildscript {
      repositories { jcenter() }
      dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.1.1'
      }
    }

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'com.github.johnrengelman.shadow'

    project.version = rootProject.ext.deployerVersion


    // In this section you declare where to find the dependencies of your project
    repositories {
        // Use 'maven central' for resolving your dependencies.
        // You can declare any Maven/Ivy/file repository here.
        mavenCentral()
    }

    configurations {
         pkg
    }

    // In this section you declare the dependencies for your production and test code
    dependencies {
        compile project(':Concenter.Foundation') 
        pkg project(path: ':Concenter.Platform', configuration: 'pkg')

        // Declare the dependency for your favourite test framework you want to use in your tests.
        // TestNG is also supported by the Gradle Test task. Just change the
        // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
        // 'test.useTestNG()' to your build script.
        testCompile 'junit:junit:4.11'
    }

    jar {
        dependsOn ':Concenter.Platform:distZip'
        manifest {
             attributes(
                'Main-Class': 'aqilco.concenter.deployer.Deployer',
             )
        }
        from configurations.pkg
    }

    /*
    shadowJar {
        dependsOn ':Concenter.Platform:distZip'
        manifest {
             attributes(
                'Main-Class': 'aqilco.concenter.deployer.Deployer',
             )
        }
        from configurations.pkg
    }
    */

    task pkg(type: Jar) {
        dependsOn ':Concenter.Platform:distZip'
        dependsOn 'shadowJar'
        archiveName = jar.baseName + "-" + jar.version + "-pkg." + jar.extension
        from zipTree(shadowJar.archivePath)
        from configurations.pkg
        manifest {
             attributes(
                'Main-Class': 'aqilco.concenter.deployer.Deployer',
             )
        }
    }

0

There are 0 best solutions below