Patrick Hütter

[HowTo] Build groovy project and package as jar (with all dependencies) for easy distribution with gradle

I just moved from maven to gradle within one of our groovy projects. Gradle just works fine and with it you’re able to distribute your application to other developers without the need of complicated instructions how to install the build tool.

I collected all you need in this simple build.gradle script for creating a jar with your dependencies with „gradle build“ and also the option to run your application directly by running „gradle run“ in the project directory.
In this script is a task, called „wrapper“ contained. You can run „gradle wrapper“ to run this task within your project directory and gradle will install the gradle wrapper files into it.

gradlew
gradlew.bat
gradle/wrapper/
gradle-wrapper.jar
gradle-wrapper.properties

Bundle this files with your project (for example into version control) and everyone who gets your code can build your project running the wrapper wich automatically downloads gradle if it’s missing.

At last the build.gradle file. If you want to use it, place it in your project directory root.

apply plugin:'application'
apply plugin: 'groovy'

// change to your mainClass with full package name
mainClassName = "main.groovy.MyMainClass"

repositories {
 // define your repositories here
 mavenCentral()
}

dependencies {
 // define your dependencies here
 compile 'org.codehaus.groovy:groovy-all:2.2.0'
 // for example
 // compile 'org.jsoup:jsoup:1.7.2'
}

jar {
 doFirst {
 from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
 }

 // This is optional, needed if you have signature problems while starting your created jar.
 // This often happens while including foreign depdendencies wich are signed. 
 exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'

 // change to your main class with full package name
 manifest.attributes("Main-Class":"main.groovy.MyMainClass")
}
Die mobile Version verlassen