We have a gradle 4.10.2 + kotlin 1.9.0 (openjdk 11) + spring boot 2.6.1 project with many services in the repo. I want to introduce ArchUnit 1.4.0 checks - so that even the new services introduced into the repo fall under them immediately. To enforce onion architecture, forbid some annotations etc. Which means that manually listing all modules as test module's dependencies won't work.
I placed the tests in core module, package com.example.core, which most of other modules depend on. When I tried to ClassFileImporter().importPackages("com.example")
, I got only classes from packages from core module.
I tried ClassFileImporter().importClasspath()
and ran out of memory. Probably won't go this way.
I tried Reflections("com.example", Scanners.SubType).getSubTypesOf(Any::class.java)
and got an empty result.
My next step would be, just leave these tests in core and import them into new modules manually... but if there is a way to feed all classes to ArchUnit automatically, I would try still.
In the end that's what I came up with. This is root build.gradle.kts:
subprojects {
plugins.apply("java")
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.matching { it.project.name != "arch-test" && it.name == "build" }.configureEach {
finalizedBy(project(":arch-test").tasks.named("build"))
}
}
gradle.taskGraph.whenReady {
val relevantProjects = allTasks.filter { it.name == "build" || it.name == "jar" }.map { it.project }
project(":arch-test").dependencies {
relevantProjects.filter { it.project.name != "arch-test" }.forEach {
add("testImplementation", project(it.path))
}
}
relevantProjects.forEach { p ->
project(":arch-test").tasks.named("build").configure {
dependsOn(p.tasks.findByName("build"))
mustRunAfter(p.tasks.findByName("build"))
}
}
}
and then the tests just import all packages they have access to:
val ALL_CLASSES: JavaClasses = ClassFileImporter().importPackages("com.example..")
and whatever the build task was - build a separate subproject or the whole repository - :arch-test:test gets all modules involved as dependencies and has access to all their classes.