依赖关系不会被复制到罐子(在摇篮) [英] Dependencies not copied into jar (in Gradle)
问题描述
TL; DR我期待一个模块的依赖关系必须建立到其的.jar,但他们似乎并没有成为
我用摇篮建设项目。我有两个子项目,应用
(一个Android模块)和测试
(一个Java模块),通过的IntelliJ所创建。
I'm using Gradle to build a project. I have two subprojects, app
(an Android module) and tests
(a Java module), as created by IntelliJ.
由于我想使用应用
定义的类中的测试
的内容,我已经添加了适当的依赖。通过这个答案,我有以下设置:
Since I want to use the classes defined in app
in the contents of tests
, I've added the appropriate dependency. Inspired by this answer, I've got the following setup:
应用程序/ build.gradle:
app/build.gradle:
...
task jarTask(type: Jar) {
baseName ="${project.archivesBaseName}"
from android.sourceSets.main.java
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration jarTask
}
测试/ build.gradle
tests/build.gradle
...
dependencies {
...
testCompile project(path: ':app', configuration: 'jarConfiguration')
}
不过,这些类的一个应用程序
进口 org.json.JSONObject
。由于这是由Android SDK中提供的应用
可以找到很好 - 但是当我尝试运行测试,我得到(在:测试:compileTestJava
)包org.json不存在。但是,如果我更新的测试/ build.gradle为
However, one of the classes in app
imports org.json.JSONObject
. Since this is provided by the Android SDK, app
can find it fine - but when I try to run tests, I get (in :tests:compileTestJava
) "package org.json does not exist". However, if I update tests/build.gradle to be
...
dependencies {
...
testCompile project(path: ':app', configuration: 'jarConfiguration')
testCompile 'org.json:json:20090211'
}
(见这里),然后测试正常运行。
(See here) then the tests run correctly.
这不是我所期待的 - 如果一个的.jar不打包的相关性,则取决于该的.jar任何项目还必须明确依赖于传递依赖 - 这是不是造成依赖的定义迅速增加?
This wasn't what I expected - if a .jar doesn't package up its dependencies, then any project which depends on that .jar must also explicitly depend on the transitive dependencies - wouldn't this cause dependency definitions to rapidly increase?
推荐答案
我碰到一个fatJar的概念,在这里一>和此处,这似乎是等同于一个罐依赖 - 但改变我的应用程序/ build.gradle读
I came across the concept of a "fatJar" here and here, which seems to be synonymous with a "jar with dependencies" - but changing my app/build.gradle to read
...
task jarTask(type: Jar) {
baseName ="test-${project.archivesBaseName}"
from android.sourceSets.main.java
}
task fatJar(type: Jar) {
baseName = project.name + "-all"
from {configurations.compile.collect {it.isDirectory() ? it : zipTree(it)}}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration fatJar
}
仍然给出了错误包org.json不存在
。这似乎是因为 org.json
没有显式调用了在应用程序/ build.gradle文件的依赖。用下面的程序/ build.gradle,我的项目建成并运行测试,预计:
still gives an error that package org.json does not exist
. This appears to be because org.json
is not explicitly called out as a dependency in the app/build.gradle file. With the following app/build.gradle, my project built and ran tests as expected:
...
dependencies {
...
compile 'org.json:json:20090211'
}
task jarTask(type: Jar) {
baseName ="${project.archivesBaseName}"
from android.sourceSets.main.java
}
task fatJar(type: Jar) {
baseName = project.name + "-all"
from {configurations.compile.collect {it.isDirectory() ? it : zipTree(it)}}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration fatJar
}
(注意,如果你只是想fatJar任务,你可以简单地设置从文件({{configurations.compile.collect it.isDirector()是:zipTree(它)}},机器人.sourceSets.main.java)
这篇关于依赖关系不会被复制到罐子(在摇篮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!