How do you add JAR Libraries to your build?


(Michael Hoff) #1

Sincerely Gradle Community, I have a Problem concerning Dependency Management. In my Project ‘A’ I have two Java Classes, one of them is a GUI library with the name “StandardDraw.java” and the other one is a simple object called “Enemy.java”

I uploaded the JAR Gradle produces to an Artifactory and wanted to use the two Java Classes for my Project B

In my Project B I have the file “Main.java” where I wanted to use this library and the java Object via the Artifactory.

Project B’s Build Script looks like this:

apply plugin: ‘java’ apply plugin: ‘eclipse’

repositories {

mavenRepo urls: “http://localHost.com/artifactory/experimental” }

dependencies {

compile ‘A:A:1.0.0’

runtime “A:A:1.0.0@jar”

runtime group: ‘A’, name: ‘A’, version: ‘1.0.0’, ext: ‘jar’ }

But each time I try to build I get the error:

:compileJava /Users/Mike/Workspace/B/src/main/java/projectB/Main.java:7: cannot find symbol symbol : class Enemy location: class projectB.Main

static Enemy enemy;

^ /Users/Mike/Workspace/B/src/main/java/projectB/Main.java:11: cannot find symbol symbol : variable StdDraw location: class projectB.Main

StandardDraw.setCanvasSize( WIDTH, HEIGHT);

^ …

I am still practicing with Gradle and tried the code you use in your Documentation, but I think I 'm still mixing something up.

The directory of the JAR looks like this:

A-1.0.0.jar

  • META-INF

    • MANIFEST.MF
  • projectA

    • Enemy.class
    • StandardDraw.class

The Manifest of the JAR includes just this one line:

“Manifest-Version: 1.0”

Maybe I have to add something there?

I would appreciate any help!


(Szczepan Faber) #2

Diagnostic stuff you can do:

  1. Run gradle dependencies for the project you have compilation errors. See if the required jars are resolved. 2. Make sure that the jar contains the class your project needs 3. You can print the classpath used by compilation, println sourceSets.main.compileClasspath in your build.gradle.

Hope that helps!


(Luke Daley) #3

One thing that strikes me is this part:

dependencies {
   compile 'A:A:1.0.0'
   runtime "A:A:1.0.0@jar"
   runtime group: 'A', name: 'A', version: '1.0.0', ext: 'jar'
 }

It should just be:

dependencies {
  compile 'A:A:1.0.0'
}

Is there any reason why you have declared the dependency three times?


(Michael Hoff) #4

Sorry, I already tried the three liner:

dependencies {

compile ‘A:A:1.0.0’ }

but it didn’t work so I just threw in all the stuff I found in your documentation, that’s the only reason why I declared it three times, but it was not intentionally. I already changed it like you recommended.

I tried your ListJars Task and modified it a bit:

task listJars << {

configurations.compile.each { File file -> println file.name }

println sourceSets.main.compileClasspath } So strangely the output is:

Mike$ gradle list :listJars A-1.0.0.jar configuration ‘:compile’

BUILD SUCCESSFUL

Could something be wrong with the MetaInf of the Jar?

( I’m still using Milestone 3 (installed it with Homebrew))


(Szczepan Faber) #5

Do you want to include project dependencies in the meta file? See the ‘Customization of MANIFEST.MF’ section in the user guide: http://gradle.org/current/docs/userguide/userguide_single.html


(Michael Hoff) #6

Thank you guys, there was something wrong with the Jar file! It’s working just fine now. Thank you very much for your help, much appreciated!