Gradle 'unable to resolve class' error from build script compile-dependency that works fine in Eclipse and from java command line


(neale) #1

I’m having trouble loading a custom jar dependency from my local maven repository. References to classes in the jar always cause the build to fail with an ‘unable to resolve class’ error, but non-custom dependencies(apache commons, etc) work fine. Equivalent java code that references the same dependency compiles and runs without error both in Eclipse and on the command line.

In trying to track down the problem I created a couple of bare-bones projects as follows. The first project (proj1), from which the jar is produced, is basically the quickstart Gradle project in Eclipse, with a Person class that references Apache commons:

package org.gradle;
  import org.apache.commons.collections.list.GrowthList;
  public class Person {
    private final String name;
      public Person(String name) {
        this.name = name;
        new GrowthList();
    }
      public String getName() {
        return name;
    }
}

Here’s the build script:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
  sourceCompatibility = 1.7
archivesBaseName = 'proj1'
group = 'org.gradle'
version = '1.0'
  repositories {
 mavenLocal()
}
  dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

The jar is added to the local maven repository via ‘gradle install’, which appears to work fine, and is referenced in the second project (proj2) as follows:

apply plugin: 'java'
apply plugin: 'eclipse'
  sourceCompatibility = 1.7
  repositories {
 mavenLocal()
}
  dependencies {
 compile 'org.gradle:proj1:1.0'
}
  task blah << {
 def p = new org.gradle.Person("asdf")
}

Attempts to build this fail with the error ‘unable to resolve class org.gradle.Person’.

Commenting out the Person reference and running ‘gradle dependencies’ produces

compile - Compile classpath for source set 'main'.
\--- org.gradle:proj1:1.0
     \--- commons-collections:commons-collections:3.2

Adding ‘configurations.compile.files.each{println it.name}’ to the ‘blah’ task gives:

$ gradle blah
:blah
proj1-1.0.jar
commons-collections-3.2.jar
  BUILD SUCCESSFUL

Have I misunderstood something about publishing jars to Maven repositories? Does Gradle expect certain values to be present in the jar manifest? The above test used the basic ‘Manifest-Version: 1.0’ manifest, but I’ve tried adding other tags, such as the implementation name and version, without success.

I’m using Gradle 1.11, Java 1.7.0_51, Spring Tool Suite 3.4 (Eclipse 4.3.1) on Win7. I’ve also tried opening proj2 in IDEA and got the same error.


(Jan Sorensen) #2

You must declare the dependencies needed to execute the build in a buildscript block. Something like:

buildscript {
  repositories {
      mavenLocal()
  }
    dependencies {
      classpath 'org.gradle:proj1:1.0'
  }
}

(neale) #3

Wow, that must be a common gotcha. Thanks.