Repository contains architectural, framework / lib specific ArchUnit rules that can be reused across different projects. Rules are grouped in artifacts based on their specialization.
For more info how to write rules in ArchUnit, please see User Guide
Rules can be integrated into project by next plugins:
Using these plugins bring a way to manage the rules through build configuration and to easily share and enforce them across projects.
To use the plugin, build.gradle require next changes:
-
Declare the dependency to the plugin
-
Configure GitHubPackages repository to have access to artifact contained needed rules, add it to plugin classpath
buildscript { repositories { maven { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/vitech-team/arch-tests") credentials { username = '<username>' password = '<access_key>' } } } dependencies { classpath "com.societegenerale.commons:arch-unit-gradle-plugin:2.6.1" classpath 'com.vitech.archtests:spring:0.4' } }
-
Configure the plugin, add needed rules to configurableRules property
allprojects {
apply plugin: 'java'
apply plugin: 'com.societegenerale.commons.plugin.gradle.ArchUnitGradlePlugin'
archUnit {
configurableRules=[
configurableRule("com.vitech.archtests.spring.rules.TransactionalRules", applyOn("com.example.project","main"))
]
}
}
- After plugin setup, task
checkRules
will be created to run all available ArchUnit tests. Build your project withgradlew clean build
(pipeline will contain taskcheckRules
): if some of your code is not compliant with the rules defined, the build will fail, pointing you to the rule(s) and the class(es) that are violating it.
To add new rule(s) for verification, you have to add new configurableRule to configurableRules. Arguments required for construction:
- path to class with rules
- applyOn, arguments:
- packageName - package, that will be analyzed
- scope - available options: main, test
allprojects {
archUnit {
configurableRules=[
configurableRule("com.vitech.archtests.spring.rules.TransactionalRules", applyOn("com.example.project","main"))
]
}
}
Third optional param is List checks - list of concrete rules in class to be applied.
For example, we have class with two rules NO_TRANSACTIONAL_METHODS_CALL_FROM_THE_SAME_INSTANCE, TRANSACTIONAL_METHODS_MUST_BE_PUBLIC,
public class TransactionalRules {
@ArchTest
public static final ArchRule NO_TRANSACTIONAL_METHODS_CALL_FROM_THE_SAME_INSTANCE; // Implementation of the rule is skipped
@ArchTest
public static final ArchRule TRANSACTIONAL_METHODS_MUST_BE_PUBLIC; // Implementation of the rule is skipped
}
If we want to apply second one in our project, next configuration should be added:
allprojects {
archUnit {
configurableRules=[
configurableRule("com.vitech.archtests.spring.rules.TransactionalRules", applyOn("com.example.archunitplugintest","main"), ["TRANSACTIONAL_METHODS_MUST_BE_PUBLIC"])
]
}
}
If no rules are added to configurableRules, check will fail with message like:
'Arch unit Gradle Plugin should have at least one preconfigured/configurable rule!'
You can exclude classes that have a path containing any of the specified paths (pay attention that you have to use slash instead of dot)
allprojects {
archUnit {
excludedPaths=["com/example/excludePackage"]
}
}