user1464251
user1464251

Reputation: 333

Specifying Gradle dependencies for *.jar files

This must be straight forward but I fail to see where group: name: and version: come from for a particular jar file. For example from Gradle's documentation.

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    testRuntime "org.hamcrest:SelfDescribing:1.+"
}

I'm trying to make use of a hamcrest jar for my junit testing. I need hamcrest for runtime testing but I don't know how to correctly specify this. Below is my build.gradle file. This test project will build successfully but will not run the unit tests due to unresolved dependencies.

apply plugin: 'java'
apply plugin: 'eclipse-wtp'

version = '1.0'

compileJava.destinationDir = file("$buildDir/classes/test")

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

repositories {
flatDir dirs: "C:/eclipse/plugins/org.junit_4.11.0.v201303080030"
flatDir dirs: "C:/eclipse/plugins"
}

dependencies {
compile "junit:junit:4"
testCompile "junit:junit:4"
testRuntime "org.hamcrest:SelfDescribing:1.+"
}

test {
testLogging.showStandardStreams = true

testLogging {
    events 'started', 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
    exceptionFormat 'short'
}
}

jar {
manifest.attributes provider: 'gradle'
}

Upvotes: 0

Views: 1797

Answers (2)

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

When using a flatDir repository, you'll have to make sure that:

  • Artifact files are contained directly in the specified directory (not in a subdirectory).
  • Artifact filenames match the pattern module-version.extension (e.g. junit-4.jar) or module.extension (e.g. junit.jar).

Alternatively, you could use file dependencies as described in Scott's answer.

PS: I'm not sure if version ranges work with flatDir repositories.

Upvotes: 1

Scott Barta
Scott Barta

Reputation: 80010

To add a dependency on a jar file, add a statement like this to your dependencies block:

compile files('path/to/archive.jar')

A popular approach is this, which will automatically pick up all jar files in a libs directory:

compile fileTree(dir: 'path/to/libs', include: '*.jar')

Upvotes: 2

Related Questions