模块如何使用 gradle 多模块使用另一个模块的资源

ray*_*man 4 android gradle build.gradle

我有项目 A 和项目 B

项目 A 单元测试(在测试目录下)需要使用项目 B 主/资源目录下的资源文件。

项目 A 上的 gradle.build:

dependencies {
..    testCompile project(':web')
}
Run Code Online (Sandbox Code Playgroud)

项目 B 上的 gradle.build:

task testJar(type: Jar) {
    classifier 'resources'
    from sourceSets.main.resources
}
Run Code Online (Sandbox Code Playgroud)

仍然失败。我不确定我错过了什么?

谢谢你,雷。

RaG*_*aGe 5

当您像这样添加对项目的依赖时:

testCompile project(':B')
Run Code Online (Sandbox Code Playgroud)

您依赖于项目 B 生成的默认工件,这通常是默认的 jar。如果您想依赖自定义 jar,例如测试 jar、资源 jar 或胖 jar,则必须明确指定。您可以将自定义工件添加到配置中,并改为依赖于配置,如下所示:

在 B 的 build.gradle 中:

configurations {
  foo
}

task testJar(type: Jar) {
    classifier 'resources'
    from sourceSets.main.resources
}

artifacts {
  foo testJar
}
Run Code Online (Sandbox Code Playgroud)

然后在 A 中使用它作为:

dependencies{
    testCompile project(path: ':B', configuration: 'foo')
}
Run Code Online (Sandbox Code Playgroud)

要验证,您可以将此任务添加到 A:

task printClasspath()<<{
    configurations.testCompile.each{println it}
}
Run Code Online (Sandbox Code Playgroud)

打印:

${projectRoot}\B\build\libs\B-resources.jar
Run Code Online (Sandbox Code Playgroud)