Dependencies are not included in jar after assemble


(kmxillo) #1

My Programm built correctly.

./gradlew clean assemble

  Gradle Version        : 4.5.1
  OS Info               : Linux 4.4.114-94.11-default (amd64)
  JDK Version           : Oracle Corporation 9.0.4 [Java HotSpot(TM) 64-Bit Server VM 9.0.4+11]
  JAVA_HOME             : /usr/java/jdk-9.0.4

But when I run it I get:

Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ListMultimap

As I understood I should define in build.gradle:

   dependencies {
    api 'com.google.guava:guava:23.0' }

I understood that ‘api’ field exports the lib to consumers.


(James Justinic) #2

Declaring a dependency in the api configuration of a java library does export it to consuming projects, but that is a different concept from bundling an application with its dependencies. Fat JARs are generally bad for libraries, but good for applications. To bundle dependencies, you can use a plugin like Shadow.


(kmxillo) #3

Using Java 8 I did not have to define any guava dependency. This problem comes because now I must compile it with Java 9.

What does it explain that before It worked and now I need to bundle it with Shadow? Does Shadow bundle all jar, or can I say the missing ones?


(James Justinic) #4

If your JAR file runs with java -jar when using Java 8, your build must already contain a plugin or logic to handle building a fat JAR or the full required classpath. You would encounter java.lang.ClassNotFoundException for dependencies if you were trying to run the JAR that would be built by default.

Your later mention of Java 9 instead suggests you’re having a Java 9 module issue, but it isn’t clear what you’ve done to set up for Java 9 or if you’ve followed some steps from the Gradle guides on Java 9 modules. Please clarify what you have in your project.


(kmxillo) #5

Build.gradle is the same building it with Java 8 or Java9.

With Java8 works fine, but java 9 misses guava dependencies.

No 8 or 9 have guava in jre/lib/ext.


(kmxillo) #6

Using findJarsByGroup worked!

task copyToLib(type: Copy) {
  from findJarsByGroup(configurations.compile, 'org.apache.avro')
  into "$buildSrc/lib"
}

def findJarsByGroup(Configuration config, groupName) {
  configurations.compile.files { it.group.equals(groupName) }
}