Class AntTask

java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.Task
de.thetaphi.forbiddenapis.ant.AntTask
All Implemented Interfaces:
Constants, Cloneable

public class AntTask extends org.apache.tools.ant.Task implements Constants
Task to check if a set of class files contains calls to forbidden APIs from a given classpath and list of API signatures (either inline or as pointer to files). In contrast to other ANT tasks, this tool does only visit the given classpath and the system classloader, not ANT's class loader.
  • Field Details

    • classFiles

      private final org.apache.tools.ant.types.resources.Union classFiles
    • apiSignatures

      private final org.apache.tools.ant.types.resources.Union apiSignatures
    • bundledSignatures

      private final Collection<BundledSignaturesType> bundledSignatures
    • suppressAnnotations

      private final Collection<SuppressAnnotationType> suppressAnnotations
    • severityOverrides

      private final Collection<SeverityOverrideType> severityOverrides
    • classpath

      private org.apache.tools.ant.types.Path classpath
    • failOnUnsupportedJava

      private boolean failOnUnsupportedJava
    • restrictClassFilename

      private boolean restrictClassFilename
    • failOnMissingClasses

      private boolean failOnMissingClasses
    • failOnUnresolvableSignatures

      private boolean failOnUnresolvableSignatures
    • ignoreSignaturesOfMissingClasses

      private boolean ignoreSignaturesOfMissingClasses
    • failOnViolation

      private boolean failOnViolation
    • ignoreEmptyFileset

      private boolean ignoreEmptyFileset
    • targetVersion

      private String targetVersion
    • disableClassloadingCache

      private boolean disableClassloadingCache
  • Constructor Details

    • AntTask

      public AntTask()
  • Method Details

    • execute

      public void execute() throws org.apache.tools.ant.BuildException
      Overrides:
      execute in class org.apache.tools.ant.Task
      Throws:
      org.apache.tools.ant.BuildException
    • add

      public void add(org.apache.tools.ant.types.ResourceCollection rc)
      Set of class files to check
    • setDir

      public void setDir(File dir)
      Sets a directory as base for class files. The implicit pattern '**/*.class' is used to only scan class files.
    • addSignaturesResource

      private <T extends org.apache.tools.ant.ProjectComponent & org.apache.tools.ant.types.ResourceCollection> T addSignaturesResource(T res)
    • createSignaturesFileSet

      public org.apache.tools.ant.types.FileSet createSignaturesFileSet()
      Set of files with API signatures as nested element
    • createSignaturesFileList

      public org.apache.tools.ant.types.FileList createSignaturesFileList()
      List of files with API signatures as nested element
    • createSignaturesFile

      public org.apache.tools.ant.types.resources.FileResource createSignaturesFile()
      Single file with API signatures as nested element
    • createSignatures

      public SignaturesResources createSignatures()
      Collection of arbitrary Ant resources or <bundled/> elements.
    • setSignaturesFile

      public void setSignaturesFile(File file)
      A file with API signatures signaturesFile= attribute
    • addText

      public void addText(String text)
      Support for API signatures list as nested text
    • createBundledSignatures

      public BundledSignaturesType createBundledSignatures()
      Creates a bundled signatures instance
    • setBundledSignatures

      public void setBundledSignatures(String name)
      A bundled signatures name
    • createSeverityOverride

      public SeverityOverrideType createSeverityOverride()
      Adds an override for forbidden API signatures for which violations should not be reported at all (i.e. neither fail the build nor appear in the logs). Each signature must be listed in a separate XML element (in text) and a priority attribute. This takes precedence over failOnViolation. In order to be effective the signature must be given in one of the signatures elements (e.g., it can be used to disable a bundled signature which is not yet ready to be enforced).
      Since:
      3.9
    • createSuppressAnnotation

      public SuppressAnnotationType createSuppressAnnotation()
      Creates a instance of an annotation class name that suppresses error reporting in classes/methods/fields.
    • setSuppressAnnotation

      public void setSuppressAnnotation(String classname)
      Class name of annotation that suppresses error reporting in classes/methods/fields.
    • setClasspath

      public void setClasspath(org.apache.tools.ant.types.Path classpath)
      Classpath as classpath= attribute
    • setClasspathRef

      public void setClasspathRef(org.apache.tools.ant.types.Reference r)
      Classpath as classpathRef= attribute
    • createClasspath

      public org.apache.tools.ant.types.Path createClasspath()
      Classpath as nested element
    • setFailOnUnsupportedJava

      public void setFailOnUnsupportedJava(boolean failOnUnsupportedJava)
      Fail the build, if the bundled ASM library cannot read the class file format of the runtime library or the runtime library cannot be discovered. Defaults to false.
    • setFailOnMissingClasses

      public void setFailOnMissingClasses(boolean failOnMissingClasses)
      Fail the build, if a referenced class is missing. This requires that you pass the whole classpath including all dependencies. If you don't have all classes in the filesets, the application classes must be reachable through this classpath, too. Defaults to true.
    • setFailOnUnresolvableSignatures

      @Deprecated public void setFailOnUnresolvableSignatures(boolean failOnUnresolvableSignatures)
      Fail the build if a signature is not resolving. If this parameter is set to to false, then such signatures are ignored. Defaults to true.

      When disabling this setting, the task still prints a warning to inform the user about broken signatures. This cannot be disabled. There is a second setting #setIgnoreMissingSignaturesClasses() that can be used to silently ignore signatures that refer to methods or field in classes that are not on classpath, e.g. This is useful in multi-module builds where a common set of signatures is used, that are not part of every sub-modules dependencies.

      See Also:
      • #setIgnoreMissingSignaturesClasses()
    • setIgnoreSignaturesOfMissingClasses

      public void setIgnoreSignaturesOfMissingClasses(boolean ignoreSignaturesOfMissingClasses)
      If a class is missing while parsing signatures files, all methods and fields from this class are silently ignored. This is useful in multi-module projects where only some modules have the dependency to which the signature file(s) apply. This settings prints no warning at all, so verify the signatures at least once with full dependencies. Defaults to false.
      Since:
      3.0
    • setRestrictClassFilename

      public void setRestrictClassFilename(boolean restrictClassFilename)
      Automatically restrict resource names included to files with a name ending in '.class'. This makes filesets easier, as the includes="**/*.class" is not needed. Defaults to true.
    • setIgnoreEmptyFileSet

      public void setIgnoreEmptyFileSet(boolean ignoreEmptyFileset)
      Ignore empty fileset/resource collection. Defaults to false.
    • setFailOnViolation

      public void setFailOnViolation(boolean failOnViolation)
      Fail the build if violations have been found. If this parameter is set to false, then the build will continue even if violations have been found. Defaults to true.
    • setTargetVersion

      public void setTargetVersion(String targetVersion)
      The default compiler target version used to expand references to bundled JDK signatures. E.g., if you use "jdk-deprecated", it will expand to this version. This setting should be identical to the target version used in the compiler task. Defaults to null.
      Since:
      2.1
    • setDisableClassloadingCache

      public void setDisableClassloadingCache(boolean disableClassloadingCache)
      Disable the internal JVM classloading cache when getting bytecode from the classpath. This setting slows down checks, but may work around issues with other tasks, that do not close their class loaders. If you get FileNotFoundExceptions related to non-existent JAR entries you can try to work around using this setting. The default is false.
      Since:
      2.2