|
| 1 | +# Nebula ArchRules |
| 2 | + |
| 3 | +[ArchUnit](https://www.archunit.org/) a popular OSS library used to enforce “architectural” code rules as part of a |
| 4 | +JUnit suite. However, it is limited by its design to be used as part of a JUnit suite in a single repository. Nebula |
| 5 | +ArchRules is a toolkit which gives organizations the ability to share and apply rules across any number of repositories. |
| 6 | +Rules can be sourced from OSS libraries or private internal libraries. |
| 7 | + |
| 8 | +### Authoring Rules |
| 9 | + |
| 10 | +To author rules, apply the ArchRules Library plugin to a project: |
| 11 | + |
| 12 | +```kotlin |
| 13 | +plugins { |
| 14 | + id("com.netflix.nebula.archrules.library") version ("latest.release") |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +This plugin will create a source set called `archRules`. Create classes in that source set which implement the |
| 19 | +`com.netflix.nebula.archrules.core.ArchRulesService` interface. |
| 20 | + |
| 21 | +#### Example |
| 22 | + |
| 23 | +```java |
| 24 | +package com.example.library; |
| 25 | + |
| 26 | +import com.netflix.nebula.archrules.core.ArchRulesService; |
| 27 | +import com.tngtech.archunit.lang.ArchRule; |
| 28 | +import com.tngtech.archunit.lang.Priority; |
| 29 | +import com.tngtech.archunit.lang.syntax.ArchRuleDefinition; |
| 30 | + |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static com.tngtech.archunit.core.domain.JavaAccess.Predicates.target; |
| 34 | +import static com.tngtech.archunit.core.domain.JavaAccess.Predicates.targetOwner; |
| 35 | +import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.annotatedWith; |
| 36 | + |
| 37 | +public class LibraryArchRules implements ArchRulesService { |
| 38 | + private final ArchRule noDeprecated = ArchRuleDefinition.priority(Priority.LOW).noClasses() |
| 39 | + .should().accessTargetWhere(targetOwner(annotatedWith(Deprecated.class))) |
| 40 | + .orShould().accessTargetWhere(target(annotatedWith(Deprecated.class))) |
| 41 | + .orShould().dependOnClassesThat().areAnnotatedWith(Deprecated.class) |
| 42 | + .allowEmptyShould(true) |
| 43 | + .as("No code should reference deprecated APIs") |
| 44 | + .because("usage of deprecated APIs introduces risk that future upgrades and migrations will be blocked"); |
| 45 | + |
| 46 | + @Override |
| 47 | + public Map<String, ArchRule> getRules() { |
| 48 | + return Map.of("deprecated", noDeprecated); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +When authoring rules about the usage of your own library code, it is recommended to colocate your rules library in the |
| 54 | +same project as the library code. The ArchRules plugin will publish the rules in a separate Jar, and the Runner plugin |
| 55 | +will select that jar for running rules, but these rule classes will not end up up in the runtime classpath. |
| 56 | + |
1 | 57 | ## LICENSE |
| 58 | + |
2 | 59 | Copyright 2025 Netflix, Inc. |
3 | 60 |
|
4 | | -Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at |
| 61 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the |
| 62 | +License. You may obtain a copy of the License at |
5 | 63 |
|
6 | 64 | http://www.apache.org/licenses/LICENSE-2.0 |
7 | 65 |
|
8 | | -Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 66 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an " |
| 67 | +AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific |
| 68 | +language governing permissions and limitations under the License. |
0 commit comments