skuzzle / restrict-imports-enforcer-rule

Gradle plugin & Maven Enforcer rule that restricts usage of unwanted imports in Java, Kotlin and Groovy source files.
MIT License
75 stars 12 forks source link

Feature request: exclude generate-sources and/or generate-test-sources #47

Closed honnix closed 3 years ago

honnix commented 3 years ago

A maven plugin or annotation processor may generate code using imports that we normally ban, and it is not possible to control the code generation.

It would be nice we can ignore a whole directory such as target/generated-sources. I understand this can be done on package level, but sometimes we have generated code and human written code in the same package but different directories.

honnix commented 3 years ago

I took a look at the code and I think if we could do something around the following lines to filter out excluded folders, it might work.

final Collection<Path> srcDirectories = this.includeCompileCode
                ? listSourceRoots(mavenProject.getCompileSourceRoots())
                : Collections.emptyList();
final Collection<Path> testDirectories = this.includeTestCode
                ? listSourceRoots(mavenProject.getTestCompileSourceRoots())
                : Collections.emptyList();
skuzzle commented 3 years ago

That's actually a very valid request and as you already found out it should be pretty straight forward to implement. I think that target/generated-sources is a pretty standard location for generated files so it might be a sensible default exclusion. Making the exclusions configurable would be the icing on the cake.

I'll happily accept a pull request with the feature, but I'd also take care of it myself if I find the time.

honnix commented 3 years ago

Cool, thanks for confirming the validity of it. I will give it a try.

skuzzle commented 3 years ago

Will be released as 1.3.0