我有两个项目,项目 A 和项目 B。两者都是用 groovy 编写的,并使用 gradle 作为构建系统。
项目 A 需要项目 B。这适用于编译和测试代码。
如何配置项目 A 的测试类可以访问项目 B 的测试类?
我有两个项目,项目 A 和项目 B。两者都是用 groovy 编写的,并使用 gradle 作为构建系统。
项目 A 需要项目 B。这适用于编译和测试代码。
如何配置项目 A 的测试类可以访问项目 B 的测试类?
您可以通过“测试”配置公开测试类,然后在该配置上定义 testCompile 依赖项。
我对所有 java 项目都有这个块,它包含所有测试代码:
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
from sourceSets.test.output
}
configurations {
tests
}
artifacts {
tests testJar
}
然后,当我有测试代码时,我想在我使用的项目之间访问
dependencies {
testCompile project(path: ':aProject', configuration: 'tests')
}
这是针对 Java 的;我假设它也应该适用于 groovy。
这是一个更简单的解决方案,不需要中间 jar 文件:
dependencies {
...
testCompile project(':aProject').sourceSets.test.output
}
这个问题有更多的讨论:Multi-project test dependencies with gradle
这对我有用(Java)
// use test classes from spring-common as dependency to tests of current module
testCompile files(this.project(':spring-common').sourceSets.test.output)
testCompile files(this.project(':spring-common').sourceSets.test.runtimeClasspath)
// filter dublicated dependency for IDEA export
def isClassesDependency(module) {
(module instanceof org.gradle.plugins.ide.idea.model.ModuleLibrary) && module.classes.iterator()[0].url.toString().contains(rootProject.name)
}
idea {
module {
iml.whenMerged { module ->
module.dependencies.removeAll(module.dependencies.grep{isClassesDependency(it)})
module.dependencies*.exported = true
}
}
}
.....
// and somewhere to include test classes
testRuntime project(":spring-common")
现在支持作为 Gradle 中的一流功能(自 5.6 起)
带有java
或java-library
插件的模块还可以包含一个java-test-fixtures
插件,该插件公开了要与 testFixtures 助手一起使用的助手类和资源。这种方法对工件和分类器的好处是:
例子:
:模块:一个
模块/一个/build.gradle
plugins {
id "java-library" // or "java"
id "java-test-fixtures"
}
dependencies {
testFixturesImplementation("your.jar:dependency:0.0.1")
}
或者lazyly
只是添加主配置的所有依赖项:implementation
val testFixturesImplementation by configurations.existing
val implementation by configurations.existing
testFixturesImplementation.get().extendsFrom(implementation.get())
modul/one/src/ testFixtures /java/com/example/Helper.java
package com.example;
public class Helper {}
:模块:其他
模块/其他/build.gradle
plugins {
id "java" // or "java-library"
}
dependencies {
testImplementation(testFixtures(project(":modul:one")))
}
模块/其他/src/test/java/com/example/other/SomeTest.java
package com.example.other;
import com.example.Helper;
public class SomeTest {
@Test void f() {
new Helper(); // used from :modul:one's testFixtures
}
}
有关更多信息,请参阅文档: https ://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures
上述解决方案有效,但不适用于最新版本1.0-rc3
的 Gradle。
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
// in the latest version of Gradle 1.0-rc3
// sourceSets.test.classes no longer works
// It has been replaced with
// sourceSets.test.output
from sourceSets.test.output
}
如果 ProjectA 包含您希望在 ProjectB 中使用的测试代码,并且 ProjectB 想要使用工件来包含测试代码,那么 ProjectB 的build.gradle将如下所示:
dependencies {
testCompile("com.example:projecta:1.0.0-SNAPSHOT:tests")
}
然后你需要在 ProjectA 的 build.gradle 部分中添加一个archives
命令:artifacts
task testsJar(type: Jar, dependsOn: testClasses) {
classifier = 'tests'
from sourceSets.test.output
}
configurations {
tests
}
artifacts {
tests testsJar
archives testsJar
}
jar.finalizedBy(testsJar)
现在,当 ProjectA 的工件发布到您的工件时,它们将包含一个-tests jar。然后可以将此-tests jar 添加为 ProjectB 的 testCompile 依赖项(如上所示)。
对于 Gradle1.5
task testJar(type: Jar, dependsOn: testClasses) {
from sourceSets.test.java
classifier "tests"
}
对于最新 gradle 版本(我目前在 2.14.1)上的 Android,您只需在项目 B 中添加以下内容即可从项目 A 中获取所有测试依赖项。
dependencies {
androidTestComplie project(path: ':ProjectA')
}
dependencies {
testImplementation project(':project_name')
}