Create a Groovy executable JAR with Gradle

I would throw in a vote for the shadow gradle plugin. It is capable of building uber jars and is quite versatile and capable of things like class relocation to prevent dependency hell.

I will not get into comparing the two plugins, but I will go as far as saying that I have gravitated towards using shadow from having used application in the past because of the added features.

When I get tired of the startup times of @Grab based groovy scripts, I tend to write a gradle build file using the shadow plugin even for single file groovy scripts. An example gradle build file capable of building an uber jar of a groovy script file in the current directory. The main class name needs to correspond to the script file name:

repositories { 
  jcenter()
  mavenCentral()
}

defaultTasks = ['shadowJar']
version      = "1.0"

dependencies {
  compile "org.codehaus.groovy:groovy:2.4.7", 
          "commons-cli:commons-cli:1.2"
}

sourceSets {
  main {
    groovy {
        srcDirs = [rootDir]
    }
  }
}

project.tasks.remove jar

shadowJar {
  manifest {
    attributes 'Main-Class': 'MyGroovyScriptName'
  }

  classifier = ""
}

the uber jar will be generated in the build/libs directory.


What you are looking for is the application plugin which allows you build a standalone JVM application including all dependencies and run scripts.

apply plugin:'application'
mainClassName = 'test.tree.App'

EDIT:

This should create the uberjar you want:

task uberjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from configurations.runtime.asFileTree.files.collect { zipTree(it) }

    manifest {
        attributes 'Main-Class': 'test.tree.App'
    }
}

Tags:

Groovy

Gradle