Gradle: Hello world Java with a fat jar

Create your build.gradle with your dependencies and a jar section that collects all your libraries into the jar and sets the Main class file.

apply plugin: 'java'

repositories {

dependencies {
    compile 'org.mindrot:jbcrypt:0.3m'

jar {
    from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
    manifest {
        attributes("Main-Class": "Main" )

Now create a basic hello world, using the library we imported:

import org.mindrot.jbcrypt.BCrypt;

public class Main {
        public static void main(String[] args) {
                String password = BCrypt.hashpw("password", BCrypt.gensalt(10));

Now build and run your jar:

$ gradle clean build && java -jar build/libs/THE_NAME_OF_YOUR_JAR.jar
java gradle gradle-jar java-jar

Edit on github
comments powered by Disqus
Click me