Search code examples
javagradlediscord-jda

how to create executable java gradle project?


enter image description here this is my build.gradle

enter image description here this is what happens when i attempt to run the built jar.

 plugins {
    id 'java'
}


group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    implementation "net.dv8tion:JDA:5.0.0-alpha.6"
    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.2'
    implementation"io.github.bonigarcia:webdrivermanager:5.1.0"

}

test {
    useJUnitPlatform()
}
jar {
    manifest {
        attributes 'Main-Class': 'bullshitPackage.main'
    }

    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

Solution

  • You could use the shadowjar plugin to include all dependencies in your jar.

    To use it in your build.gradle file, try:

    buildscript {
        repositories {
            maven {
                url "https://plugins.gradle.org/m2/"
            }
        }
        dependencies {
            classpath "gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.0.0"
        }
    } 
     
    plugins {
        id 'java'
        id 'com.github.johnrengelman.shadow' version '7.0.0'
    }
    
    
    
    group 'org.example'
    version '1.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
        implementation "net.dv8tion:JDA:5.0.0-alpha.6"
        implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.2'
        implementation"io.github.bonigarcia:webdrivermanager:5.1.0"
    
    }
    
    test {
        useJUnitPlatform()
    }
    jar {
        manifest {
            attributes 'Main-Class': 'bullshitPackage.main'
        }
    
        exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
    }
    

    Then from your console, navigate to your project's root folder that contains the file gradlew, then run gradlew shadowjar to build the jar into the ./build/libs folder.