Gradle is a Java build system.

Can not run a built jar (through gradlew desktop:dist) from ClassNotFoundException

I had the following in my build.gradle:

 task dist(type: Jar) {
     duplicatesStrategy = 'include' // Overwrites successive files; not ideal, should really be 'fail'
     manifest {
         attributes 'Main-Class': project.mainClassName
     }
     dependsOn configurations.runtimeClasspath
     from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
     }
     with jar
 }

The underlying problem is that the .jar file had META-INF/*.SF which are licensing files, and these were invalid. With an invalid license file (or something?), Java’s classloader silently fails.

The solution is to remove the META-INF/* files from the .jar.

Task ‘X’ uses this output of task ‘Y’ without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.

task distDependencies(type: Copy) {
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    into "build/dependencies"
}

The problem is that configurations.runtimeClasspath was being used without being dependsOn. Solution is to add dependsOn:

task distDependencies(type: Copy) {
    dependsOn configurations.runtimeClasspath   // added!
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    into "build/dependencies"
}

Including META-INF/services/... in a multi-project Gradle project

If you have a core, and you’ve put your META-INF files into src/META-INF, you can put this into core/build.gradle (for example):

sourceCompatibility = 1.17

sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.test.java.srcDirs = [ "test/" ]

sourceSets.main.resources.srcDirs = [ "src/" ]  // <-- 

eclipse.project.name = appName + "-core"

This should then include all your META-INF files in the built core-version.jar. See also https://discuss.gradle.org/t/copying-files-from-meta-inf-to-jar-using-gradle-jar-plugin/11253