Java plugin: ClassNotFoundException

I’ve a demo Lucene project that builds a Jar using the Java plugin. MANIFEST.MF does not define a Main class and when I try to run a class from the Jar using

java -cp build/libs/lucene-demo-1.0.jar org.apache.lucene.demo.IndexFiles

it can’t find the dependencies and fails with the following:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/analysis/Analyzer
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.analysis.Analyzer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

I see that the assemble task copies the dependencies to a directory called ‘lib’ in the Jar but I don’t see those added to MANIFEST.MF classpath. That, anyway, doesn’t work in this case because no Main class is defined; there’re multiple classes that can act as entry point to the Jar. Maven solves this problem by providing an option to extract the dependencies in the jar. Is there something similar in Gradle?

This does the trick

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

This will work in simple cases. A more reliable solution is to use the (third-party) gradle-onejar-plugin.

I’m curious, how does Gradle finds these plugins? For example, Maven treats plugins like dependencies and downloads from the repo. Does Gradle treat plugins similarly?

An external plugin is declared as a regular dependency in a 'buildscript {} ’ block.

So Gradle will download from Maven repos that are defined? What I’m trying to understand is how do I find out, before using a plugin, if it is present in my corporate repo. For Maven, it’s as simple as opening a Browser and searching or navigating the repo.

Same for Gradle. Plugins will be downloaded from whatever Maven, Ivy, or flatdir repos are specified in the ‘buildscript’ block.