Class Cpd

java.lang.Object
org.gradle.api.internal.AbstractTask
org.gradle.api.DefaultTask
org.gradle.api.internal.ConventionTask
org.gradle.api.tasks.SourceTask
de.aaschmid.gradle.plugins.cpd.Cpd
All Implemented Interfaces:
Comparable<org.gradle.api.Task>, org.gradle.api.internal.DynamicObjectAware, org.gradle.api.internal.IConventionAware, org.gradle.api.internal.TaskInternal, org.gradle.api.Named, org.gradle.api.plugins.ExtensionAware, org.gradle.api.reporting.Reporting<CpdReports>, org.gradle.api.Task, org.gradle.api.tasks.util.PatternFilterable, org.gradle.api.tasks.VerificationTask, org.gradle.util.Configurable<org.gradle.api.Task>

@CacheableTask public class Cpd extends org.gradle.api.tasks.SourceTask implements org.gradle.api.tasks.VerificationTask, org.gradle.api.reporting.Reporting<CpdReports>
Runs static code/paste (= duplication) detection on supplied source code files and generates a report of duplications found.

Sample:

 apply plugin: 'cpd'

 task cpd(type: Cpd, description: 'Copy/Paste detection for all Ruby scripts') {

     // change language of cpd to Ruby
     language = 'ruby'

     // set minimum token count causing a duplication warning
     minimumTokenCount = 10

     // enable CSV reports and customize outputLocation, disable xml report
     reports {
         csv {
             required = true
             outputLocation = file("${buildDir}/cpd.csv")
         }
         xml.required = false
     }

     // explicitly include all Ruby files and exclude tests
     include '**.rb'
     exclude '**Test*'

     // set source for running duplication check on
     source = files('src/ruby')
 }
 
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.gradle.api.Named

    org.gradle.api.Named.Namer
  • Field Summary

    Fields inherited from interface org.gradle.api.Task

    TASK_ACTION, TASK_CONSTRUCTOR_ARGS, TASK_DEPENDS_ON, TASK_DESCRIPTION, TASK_GROUP, TASK_NAME, TASK_OVERWRITE, TASK_TYPE
  • Constructor Summary

    Constructors
    Constructor
    Description
    Cpd(org.gradle.api.model.ObjectFactory objectFactory, org.gradle.api.provider.ProviderFactory providerFactory, org.gradle.workers.WorkerExecutor workerExecutor)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
     
    The character set encoding (e.g., UTF-8) to use when reading the source code files but also when producing the report; defaults to CpdExtension.getEncoding().
    boolean
    Whether CPD should exit with status 4 (the default behavior, true) if violations are found or just with 0 (to not break the build, e.g.).
    boolean
    Whether PMD should exit with status 5 (the default behavior, true) if recoverable errors occurred or just with 0 (to not break the build, e.g.
    boolean
    Ignore annotations because more and more modern frameworks use annotations on classes and methods which can be very redundant and causes false positives.
    boolean
    Whether or not to allow the build to continue if there are warnings.
    boolean
    Option if CPD should ignore identifiers differences, i.e.
    boolean
    Option if CPD should ignore literal value differences when evaluating a duplicate block.
    Flag to select the appropriate language.
    A positive integer indicating the minimum token count to trigger a CPD match; defaults to CpdExtension.getMinimumTokenCount().
    protected org.gradle.api.tasks.util.internal.PatternSetFactory
     
    org.gradle.api.file.FileCollection
    The classpath containing the PMD library which contains the CPD library to be used.
     
    boolean
    Enables or disables skipping of blocks configured by skipBlocksPattern.
    Configures the pattern, to find the blocks to skip if enabled using skipBlocks.
    boolean
    Ignore multiple copies of files of the same name and length in comparison.
    org.gradle.api.file.FileTree
     
    reports(groovy.lang.Closure closure)
     
    reports(org.gradle.api.Action<? super CpdReports> action)
     
    void
    run()
     
    void
    setEncoding(String encoding)
     
    void
    setFailOnError(boolean failOnError)
     
    void
    setFailOnViolation(boolean failOnViolation)
     
    void
    setIgnoreAnnotations(boolean ignoreAnnotations)
     
    void
    setIgnoreFailures(boolean ignoreFailures)
     
    void
    setIgnoreIdentifiers(boolean ignoreIdentifiers)
     
    void
    setIgnoreLiterals(boolean ignoreLiterals)
     
    void
    setLanguage(String language)
     
    void
    setMinimumTokenCount(Integer minimumTokenCount)
     
    void
    setPmdClasspath(org.gradle.api.file.FileCollection pmdClasspath)
     
    void
    setSkipBlocks(boolean skipBlocks)
     
    void
    setSkipBlocksPattern(String skipBlocksPattern)
     
    void
    setSkipDuplicateFiles(boolean skipDuplicateFiles)
     

    Methods inherited from class org.gradle.api.tasks.SourceTask

    exclude, exclude, exclude, exclude, getExcludes, getIncludes, getPatternSet, include, include, include, include, setExcludes, setIncludes, setSource, setSource, source

    Methods inherited from class org.gradle.api.internal.ConventionTask

    conventionMapping, conventionMapping, getConventionMapping

    Methods inherited from class org.gradle.api.DefaultTask

    compareTo, configure, dependsOn, doFirst, doFirst, doFirst, doLast, doLast, doLast, finalizedBy, getActions, getAnt, getDependsOn, getDescription, getDestroyables, getDidWork, getEnabled, getExtensions, getFinalizedBy, getGroup, getInputs, getLocalState, getLogger, getLogging, getMustRunAfter, getName, getOutputs, getPath, getProject, getShouldRunAfter, getState, getTaskDependencies, getTemporaryDir, getTimeout, hasProperty, mustRunAfter, onlyIf, onlyIf, onlyIf, property, setActions, setDependsOn, setDescription, setDidWork, setEnabled, setFinalizedBy, setGroup, setMustRunAfter, setOnlyIf, setOnlyIf, setOnlyIf, setProperty, setShouldRunAfter, shouldRunAfter, usesService

    Methods inherited from class org.gradle.api.internal.AbstractTask

    acceptServiceReferences, appendParallelSafeAction, doNotTrackState, doNotTrackStateIf, getAsDynamicObject, getIdentityPath, getImpliesSubProjects, getLifecycleDependencies, getOnlyIf, getReasonNotToTrackState, getReasonsNotToTrackState, getReasonTaskIsIncompatibleWithConfigurationCache, getRequiredServices, getServices, getSharedResources, getStandardOutputCapture, getTaskActions, getTaskIdentity, getTemporaryDirFactory, hasTaskActions, injectIntoNewInstance, isCompatibleWithConfigurationCache, isEnabled, isHasCustomActions, notCompatibleWithConfigurationCache, prependParallelSafeAction, restoreOnlyIf, restoreTaskActions, setImpliesSubProjects

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.gradle.api.Task

    doNotTrackState, notCompatibleWithConfigurationCache
  • Constructor Details

    • Cpd

      @Inject public Cpd(org.gradle.api.model.ObjectFactory objectFactory, org.gradle.api.provider.ProviderFactory providerFactory, org.gradle.workers.WorkerExecutor workerExecutor)
  • Method Details

    • run

      public void run()
    • getPatternSetFactory

      @Inject protected org.gradle.api.tasks.util.internal.PatternSetFactory getPatternSetFactory()
      Specified by:
      getPatternSetFactory in class org.gradle.api.tasks.SourceTask
    • getSource

      @Nonnull @InputFiles @SkipWhenEmpty @PathSensitive(RELATIVE) public org.gradle.api.file.FileTree getSource()
      Overrides:
      getSource in class org.gradle.api.tasks.SourceTask
    • reports

      @Nonnull public CpdReports reports(@Nonnull @DelegatesTo(value=CpdReports.class,strategy=1) groovy.lang.Closure closure)
      Specified by:
      reports in interface org.gradle.api.reporting.Reporting<CpdReports>
    • getReports

      @Nonnull public CpdReports getReports()
      Specified by:
      getReports in interface org.gradle.api.reporting.Reporting<CpdReports>
    • reports

      @Nonnull public CpdReports reports(org.gradle.api.Action<? super CpdReports> action)
      Specified by:
      reports in interface org.gradle.api.reporting.Reporting<CpdReports>
    • getEncoding

      @Input @Optional public String getEncoding()
      The character set encoding (e.g., UTF-8) to use when reading the source code files but also when producing the report; defaults to CpdExtension.getEncoding().

      Example: encoding = UTF-8

      Returns:
      the charset encoding
    • setEncoding

      public void setEncoding(String encoding)
    • getIgnoreAnnotations

      @Input public boolean getIgnoreAnnotations()
      Ignore annotations because more and more modern frameworks use annotations on classes and methods which can be very redundant and causes false positives.

      Example: ignoreAnnotations = true

      Returns:
      if annotations should be ignored
    • setIgnoreAnnotations

      public void setIgnoreAnnotations(boolean ignoreAnnotations)
    • getIgnoreFailures

      @Input public boolean getIgnoreFailures()
      Whether or not to allow the build to continue if there are warnings.

      Example: ignoreFailures = true

      Specified by:
      getIgnoreFailures in interface org.gradle.api.tasks.VerificationTask
    • setIgnoreFailures

      public void setIgnoreFailures(boolean ignoreFailures)
      Specified by:
      setIgnoreFailures in interface org.gradle.api.tasks.VerificationTask
    • getIgnoreIdentifiers

      @Input public boolean getIgnoreIdentifiers()
      Option if CPD should ignore identifiers differences, i.e. variable names, methods names, and so forth, when evaluating a duplicate block.

      Example: ignoreIdentifiers = true

      Returns:
      whether identifiers should be ignored
    • setIgnoreIdentifiers

      public void setIgnoreIdentifiers(boolean ignoreIdentifiers)
    • getIgnoreLiterals

      @Input public boolean getIgnoreLiterals()
      Option if CPD should ignore literal value differences when evaluating a duplicate block. This means e.g. that foo=42; and foo=43; will be seen as equivalent.

      Example: ignoreLiterals = true

      Returns:
      whether literals should be ignored
    • setIgnoreLiterals

      public void setIgnoreLiterals(boolean ignoreLiterals)
    • getLanguage

      @Input public String getLanguage()
      Flag to select the appropriate language.

      Example: language = 'java'

      Returns:
      used language
    • setLanguage

      public void setLanguage(String language)
    • getMinimumTokenCount

      @Input public Integer getMinimumTokenCount()
      A positive integer indicating the minimum token count to trigger a CPD match; defaults to CpdExtension.getMinimumTokenCount().

      Example: minimumTokenCount = 25

      Returns:
      the minimum token cound
    • setMinimumTokenCount

      public void setMinimumTokenCount(Integer minimumTokenCount)
    • getPmdClasspath

      @Classpath public org.gradle.api.file.FileCollection getPmdClasspath()
      The classpath containing the PMD library which contains the CPD library to be used.
      Returns:
      CPD libraries classpath
    • setPmdClasspath

      public void setPmdClasspath(org.gradle.api.file.FileCollection pmdClasspath)
    • getSkipDuplicateFiles

      @Input public boolean getSkipDuplicateFiles()
      Ignore multiple copies of files of the same name and length in comparison.

      Example: skipDuplicateFiles = true

      Returns:
      whether duplicate files should be skipped
    • setSkipDuplicateFiles

      public void setSkipDuplicateFiles(boolean skipDuplicateFiles)
    • getFailOnError

      @Input public boolean getFailOnError()
      Whether CPD should exit with status 4 (the default behavior, true) if violations are found or just with 0 (to not break the build, e.g.).

      Example: failOnError = false

      Returns:
      whether CPD should fail on error
    • setFailOnError

      public void setFailOnError(boolean failOnError)
    • getFailOnViolation

      @Input public boolean getFailOnViolation()
      Whether PMD should exit with status 5 (the default behavior, true) if recoverable errors occurred or just with 0 (to not break the build, e.g. if the validation check fails).

      Example: failOnViolation = false

      Returns:
      whether CPD should fail on violation
    • setFailOnViolation

      public void setFailOnViolation(boolean failOnViolation)
    • getSkipBlocks

      @Input public boolean getSkipBlocks()
      Enables or disables skipping of blocks configured by skipBlocksPattern.

      Example: skipBlocks = false

      Returns:
      whether blocks should be skipped by a given pattern
      See Also:
      • skipBlocksPattern
    • setSkipBlocks

      public void setSkipBlocks(boolean skipBlocks)
    • getSkipBlocksPattern

      @Input public String getSkipBlocksPattern()
      Configures the pattern, to find the blocks to skip if enabled using skipBlocks. It is a String property and contains of two parts, separated by '|'. The first part is the start pattern, the second part is the ending pattern.

      Example: skipBlocksPattern = '#include <|>'

      Returns:
      the pattern used to skip blocks
      See Also:
      • skipBlocks
    • setSkipBlocksPattern

      public void setSkipBlocksPattern(String skipBlocksPattern)
    • defaultCPDVersion

      public static String defaultCPDVersion()