为* .jar文件指定Gradle依赖项

时间:2014-02-04 21:14:59

标签: junit jar dependencies gradle

这必须是直截了当但我无法看到group:name:和version:来自某个特定的jar文件。例如,来自Gradle的文档。

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.+"
}

我正在尝试使用hamcrest jar进行junit测试。我需要hamcrest进行运行时测试,但我不知道如何正确指定它。下面是我的build.gradle文件。此测试项目将成功构建,但由于未解析的依赖关系而无法运行单元测试。

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'
}

2 个答案:

答案 0 :(得分:2)

要在jar文件中添加依赖项,请在dependencies块中添加如下语句:

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

这是一种流行的方法,它会自动获取 libs 目录中的所有jar文件:

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

答案 1 :(得分:1)

使用flatDir存储库时,您必须确保:

  • 工件文件直接包含在指定的目录中(不在子目录中)。
  • 工件文件名与模式module-version.extension(例如junit-4.jar)或module.extension(例如junit.jar)相匹配。

或者,您可以使用file依赖关系,如Scott的回答中所述。

PS:我不确定版本范围是否适用于flatDir存储库。

相关问题