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

excludedClass does not take wildcard patterns #10

Closed bertramn closed 5 years ago

bertramn commented 7 years ago

I tried to use a patterns such as **.ObjectFactory in the excludedClass but the enforcer rule fails.

Input:

<rules>
  <restrictImports implementation="de.skuzzle.enforcer.restrictimports.RestrictImports">
    <basePackage>**</basePackage>
    <includeTestCode>false</includeTestCode>
    <bannedImports>
      <bannedImport>javax.xml.bind.JAXBElement</bannedImport>
    </bannedImports>
    <excludedClasses>
      <excludedClass>**.ObjectFactory</excludedClass>
    </excludedClasses>
  </restrictImports>
</rules>

Expected:

Rule fails on all classes importing the banned class except the ones called ObjectFactory at any package level.

Actual:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (check-jaxb-generated-imports) on project xxx: A type incompatibility occurred while executing org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce: java.lang.String cannot be cast to de.skuzzle.enforcer.restrictimports.PackagePattern
skuzzle commented 7 years ago

Could you re-run the maven build with -e flag to get the full stack trace of the exception?

Also, using ** at the beginning of a package pattern is currently not supported. It may only occur as last part of the pattern (see the README). I'm considering to allow the Ant-Style patterns or RegEx patterns but this is not yet implemented (see #5)

bertramn commented 5 years ago

Can confirm that is is now allowing a user to exclude classes with a specific name in multiple packages. Example check that no JAXBElement is imported except for the JAXB generated ObjectFactory:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
  <execution>
    <id>check-jaxb-generated-imports</id>
    <goals>
      <goal>enforce</goal>
    </goals>
    <phase>compile</phase>
    <configuration>
      <rules>
        <restrictImports implementation="de.skuzzle.enforcer.restrictimports.rule.RestrictImports">
          <reason>JAXBElement is banned from the domain model, see design principles.</reason>
          <basePackage>**</basePackage>
          <bannedImport>javax.xml.bind.JAXBElement</bannedImport>
          <exclusion>**.ObjectFactory</exclusion>
        </restrictImports>
      </rules>
    </configuration>
  </execution>
</executions>
<dependencies>
  <dependency>
    <groupId>de.skuzzle.enforcer</groupId>
    <artifactId>restrict-imports-enforcer-rule</artifactId>
    <version>0.14.0</version>
  </dependency>
</dependencies>
</plugin>
skuzzle commented 5 years ago

thanks for reporting back. I'll investigate. Your above example clearly looks like it should work with the current plugin version I misread your comment, thinking something is still wrong. Great to hear that it is working as expected!