我有一个Gradle整体项目,大约有50个子项目。我想确保每个子项目中的代码都符合特定规则,例如,每个名为的类*Foo
都用注释Bar
。
我已经编写了一个测试,可以成功扫描类路径并断言我感兴趣的属性。
我在哪里放置此代码,以便对整个项目执行该代码,或者对每个子项目分别执行,或者对整个代码库一次执行?
我了解测试装置,因此可以轻松地在所有子项目都可以访问的类中提供逻辑。但是,我仍然需要编写一个运行共享代码的测试类,这对于每个子项目都必须执行。
如何避免这种重复?我正在寻找这样的东西:
subprojects {
additionalTestsFrom project(':architecture-tests')
}
编辑:使用Conventional插件的干净方法,请参见下面的旧(不完善的)方法。
主要思想:https : //docs.gradle.org/current/userguide/sharing_build_logic_between_subprojects.html#sec : convention_plugins。
在buildSrc/build.gradle
:
plugins {
id 'groovy-gradle-plugin'
}
创建一个插件,定义我们进行任何测试所需的依赖项。在buildSrc/src/main/groovy/playground.tests.gradle
:
plugins {
id 'java'
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:latest.release'
testImplementation 'org.assertj:assertj-core:latest.release'
testImplementation 'nl.jqno.equalsverifier:equalsverifier:latest.release'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:latest.release'
}
tasks.withType(Test) {
useJUnitPlatform()
}
利用此插件,并公开添加到中的测试类:architecture-tests
。在architecture-tests/build.gradle
:
plugins {
id 'playground.tests' // so that we have access to JUnit, AssertJ, ...
}
dependencies {
// add whatever you need to write your architecture tests
}
// provide configurations that can be consumed by the other subprojects
configurations {
exposedTestClasses {
canBeConsumed = true
canBeResolved = false
}
exposedTestRuntimeClasspath {
canBeConsumed = true
canBeResolved = false
}
}
// wire the required data to the exposed configurations
artifacts {
exposedTestClasses(sourceSets.test.output.classesDirs.files) {
// make sure the compilation task is run first
builtBy(compileTestJava)
}
exposedTestRuntimeClasspath sourceSets.test.runtimeClasspath.files
}
在中提供测试类architecture-tests/src/test/java/
。
现在定义一个可用于导入体系结构测试的插件。在buildSrc/src/main/groovy/playground.architecture-tests.gradle
:
plugins {
id 'playground.tests' // inherit the basic test dependencies
}
// define the configurations that connect the task (defined below) to the exposed configurations of `:architecture-tests`
configurations {
sharedTestClasses {
canBeConsumed = false
canBeResolved = true
}
sharedTestRuntimeClasspath {
canBeConsumed = false
canBeResolved = true
extendsFrom testRuntimeClasspath
}
}
dependencies {
sharedTestClasses project(path: ':architecture-tests', configuration: 'exposedTestClasses')
sharedTestRuntimeClasspath project(path: ':architecture-tests', configuration: 'exposedTestRuntimeClasspath')
}
task sharedTest(type: Test) {
description = 'Runs the tests shared by all subprojects.'
group = 'verification'
// use the configurations defined above, which depends on the configurations in `:architecture-tests`
testClassesDirs = configurations.sharedTestClasses
classpath = configurations.sharedTestRuntimeClasspath
// optional - extend the classpath with the subproject's test and integration test classpath, so that I'm able to access these tests from my shared test
classpath += sourceSets.test.runtimeClasspath
classpath += sourceSets.integrationTest.runtimeClasspath
}
check.dependsOn sharedTest
要将架构测试作为子项目的一部分运行,请subproject/build.gradle
添加:
plugins {
id 'playground.architecture-tests'
}
编辑:较老,灰心,解决方案:
我想我知道了。我很高兴收到改进意见和建议!
我有一个子项目:architecture-tests
,其中包含我想包含在其他所有子项目中的测试。为此,我需要一个专用的子项目,因为我正在更改常规子项目的subproject-xxx/build.gradle
文件(子项目配置时间?)和subprojects {}
根build.gradle
文件的块内(项目配置时间?)的配置。到目前为止,这还不是问题,但是在任何公开/消耗性配置被另一个子项目解决之后,我无法重新配置子项目。这样,我需要:architecture-tests
从该subprojects
块和配置中排除该子项目,并将必要的配置复制到中architecture-tests/build.gradle
。如果我不这样做,则会得到:
解决后,无法更改依赖项配置':architecture-tests:testSharedRuntimeClasspath'的角色。”
这是我的解决方案:
将测试放在中architecture-tests/src/testShared/java/
。
architecture-tests/build.gradle
:
plugins {
id 'java-library'
}
// I don't think it is necessary to have a special source set, though
sourceSets {
testShared {
}
}
// provide configurations that can be consumed by the other subprojects
configurations {
exposedTestSharedClasses {
canBeConsumed = true
canBeResolved = false
}
exposedTestSharedRuntimeClasspath {
canBeConsumed = true
canBeResolved = false
}
}
// just add some regular dependences for my shared tests
dependencies {
// I use a platform which defines the versions/constraints. You can just add the versions below without using a platform.
testSharedImplementation platform(project(':some-platform'))
testSharedImplementation 'org.testng:testng'
testSharedImplementation 'org.assertj:assertj-core'
testSharedImplementation 'io.github.classgraph:classgraph'
}
// wire the required data to the exposed configurations
artifacts {
// give access to the compiled (test) classes
exposedTestSharedClasses(sourceSets.testShared.output.classesDirs.files) {
// make sure the compilation task is run first
builtBy(compileTestSharedJava)
}
// give access to the runtime classpath, containing the libraries defined above
exposedTestSharedRuntimeClasspath sourceSets.testShared.runtimeClasspath.files
}
对于每个消耗(其他)子项目(我build.gradle
在一个subprojects{}
块的根目录中执行此操作:
// define the configurations that connect the task (defined below) to the exposed configurations of `:architecture-tests`
configurations {
testSharedClasses {
canBeConsumed = false
canBeResolved = true
}
testSharedRuntimeClasspath {
canBeConsumed = false
canBeResolved = true
extendsFrom testRuntimeClasspath
}
}
dependencies {
testSharedClasses project(path: ':architecture-tests', configuration: 'exposedTestSharedClasses')
testSharedRuntimeClasspath project(path: ':architecture-tests', configuration: 'exposedTestSharedRuntimeClasspath')
}
task testShared(type: Test) {
useTestNG()
description = 'Runs the tests shared by all subprojects.'
group = 'verification'
// use the configurations defined above, which depends on the configurations in `:architecture-tests`
testClassesDirs = configurations.testSharedClasses
classpath = configurations.testSharedRuntimeClasspath
// optional - extend the classpath with the subproject's test and integration test classpath, so that I'm able to access these tests from my shared test
classpath += sourceSets.test.runtimeClasspath
classpath += sourceSets.integrationTest.runtimeClasspath
}
// run the shared tests as part of the regular `build` (which depends on `check`)
check.dependsOn testShared
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句