/arch-unit-gradle-plugin

A Gradle wrapper around ArchUnit, to easily share and enforce architecture rules across projects

Primary LanguageJavaApache License 2.0Apache-2.0

ArchUnit Gradle plugin

Build Status Gradle plugin

Context

In our dev team at Gefa Bank GmbH, we really liked how ArchUnit Maven plugin was enabling teams to distribute rules across projects. Only "problem" was that we're using Gradle, not Maven. So we decided to write an equivalent plugin for Gradle.

What is it, and how does it work ?

ArchUnit-Gradle-plugin is a wrapper around Arch-Unit-Build-Plugin-Core, which itself is a wrapper around ArchUnit, that enables you to easily make sure all your projects follow the same architecture rules.

Using a plugin brings a way to manage the rules through build configuration and to easily share and enforce architecture rules across projects.

Latest version of the plugin is available on https://plugins.gradle.org/plugin/com.societegenerale.commons.arch-unit-gradle-plugin

To use the plugin, your build.gradle require these changes:

  1. Declare the dependency to the plugin :
   buildscript {
       dependencies {
           classpath "com.societegenerale.commons:arch-unit-gradle-plugin:2.9.5"
       }
       repositories {
           mavenCentral()
       }
   }
  1. Apply the java plugin and the ArchUnitGradlePlugin, then configure it:
    allprojects {
    
        apply plugin: 'java'
        apply plugin: 'com.societegenerale.commons.plugin.gradle.ArchUnitGradlePlugin'
    
        archUnit{
        
                # defaults to "/classes/java/main" / "/classes/java/test"
                # you probably don't need to set anything if you have a standard java project
                # for a kotlin project, you would need this :
                mainScopePath="/classes/kotlin/main"
                testScopePath="/classes/kotlin/test"
        
                 excludedPaths=["some/package"]

                  preConfiguredRules=["com.societegenerale.commons.plugin.rules.NoInjectedFieldTest",
                            "com.societegenerale.commons.plugin.rules.NoAutowiredFieldTest",
                            "com.societegenerale.commons.plugin.rules.NoTestIgnoreWithoutCommentRuleTest",
                            "com.societegenerale.commons.plugin.rules.NoPrefixForInterfacesRuleTest",
                            "com.societegenerale.commons.plugin.rules.NoPowerMockRuleTest",
                            "com.societegenerale.commons.plugin.rules.NoJodaTimeRuleTest",
                            "com.societegenerale.commons.plugin.rules.NoJunitAssertRuleTest",
                            "com.societegenerale.commons.plugin.rules.HexagonalArchitectureTest",
                            "com.societegenerale.commons.plugin.rules.DontReturnNullCollectionTest",
                            "com.societegenerale.commons.plugin.rules.StringFieldsThatAreActuallyDatesRuleTest"
                                    ]

                  configurableRules=[configurableRule("com.tngtech.archunit.library.GeneralCodingRules", applyOn("com.my.project","main") )]
  
        }
    }
    
  1. Build your project with gradlew clean build : 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.

Adding new rules

All rules referenced in the configuration have to be available in the classpath. Therefore, you have 2 solutions :

  • package your rule into a custom jar, add a dependency to this jar with the dedicated configuration :
dependencies {
    ...
    archUnitExtraLib('org.example:archunit-custom-rules:1.0.0')
    ...
}

caveat : when packaging your custom rules, if you're using arch-unit-build-plugin-core, make sure it's the same version that is used in this plugin. If it's not the same version and there are breaking changes, it may not work : the plugin may take some classes from "your" jar instead of its own jar, and it may fail if some classes are missing or if some method signatures have changed

  • Propose your rule through a PullRequest to Arch-Unit-Build-Plugin-Core : if it's accepted, it will be part of the next release and usable by everyone.

Releasing a new version of the plugin

to publish in local repo during tests, use gradlew -Dmaven.repo.local=.m2/repository publishToMavenLocal