Class CheckUnusedPropertiesTask

java.lang.Object
org.gradle.api.internal.AbstractTask
org.gradle.api.DefaultTask
io.github.timoIpeters.custompropertyanalyzer.CheckUnusedPropertiesTask
All Implemented Interfaces:
Comparable<org.gradle.api.Task>, org.gradle.api.internal.DynamicObjectAware, org.gradle.api.internal.TaskInternal, org.gradle.api.Named, org.gradle.api.plugins.ExtensionAware, org.gradle.api.Task, org.gradle.util.Configurable<org.gradle.api.Task>

public class CheckUnusedPropertiesTask extends org.gradle.api.DefaultTask
Checks for properties that are defined in configuration files but never referenced in the project's Java source code.

This task reads the report produced by analyzeCustomProperties to obtain the set of all properties referenced in code, then compares it against the keys found in application.properties, application.yml, or application.yaml (+ additional files based on a glob pattern). Any key present in a configuration file but absent from the code is flagged as unused.

By default, the build fails if unused properties are found, making this task suitable as a CI gate. Pass --ignoreUnused to report without failing.

This task depends on analyzeCustomProperties and runs it automatically.

Usage:

   ./gradlew checkUnusedProperties
   ./gradlew checkUnusedProperties --verbose
   ./gradlew checkUnusedProperties --outputFile=my-unused-report.json
   ./gradlew checkUnusedProperties --ignoreUnused
   ./gradlew checkUnusedProperties --additionalPropertiesPattern="application-*.yml"
 
The report is written to build/reports/unused-properties-report.json by default.
  • Nested Class Summary

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

    org.gradle.api.Task.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
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Task action that compares defined configuration keys against code-referenced keys and flags any that are unused.
    Glob pattern for additional property files to include when scanning for unused keys, beyond the baseline application.properties, application.yml, and application.yaml.
    The name of the analysis report file produced by analyzeCustomProperties to read referenced property keys from.
    org.gradle.api.provider.Provider<org.gradle.api.file.RegularFile>
    The analysis report file produced by analyzeCustomProperties, used as input to determine which property keys are referenced in code.
    boolean
    Whether to suppress build failure when unused properties are found.
    The name of the generated JSON report file listing unused properties.
    org.gradle.api.provider.Provider<org.gradle.api.file.RegularFile>
    The output file to which the unused properties JSON report is written.
    boolean
    Whether to print the full unused properties results to the console in addition to the JSON report.
    void
    setAdditionalPropertiesPattern(String additionalPropertiesPattern)
    Option to set an additional properties glob pattern
    void
    setAnalysisReportFile(String analysisReportFile)
    Option to set a custom analysis report file name
    void
    setIgnoreUnused(boolean ignoreUnused)
    Option to enable/disable build failure when unused properties are detected
    void
    setOutputFile(String outputFile)
    Option to set a custom output file name
    void
    setVerboseMode(boolean verboseMode)
    Option to enable/disable verbose console outputs

    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, getAsDynamicObject, getConvention, getIdentityPath, getImpliesSubProjects, getLifecycleDependencies, getOnlyIf, getReasonNotToTrackState, getReasonTaskIsIncompatibleWithConfigurationCache, getRequiredServices, getServices, getSharedResources, getStandardOutputCapture, getTaskActions, getTaskIdentity, getTemporaryDirFactory, hasTaskActions, injectIntoNewInstance, isCompatibleWithConfigurationCache, isEnabled, isHasCustomActions, notCompatibleWithConfigurationCache, prependParallelSafeAction, 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, getConvention, notCompatibleWithConfigurationCache
  • Constructor Details

    • CheckUnusedPropertiesTask

      public CheckUnusedPropertiesTask()
  • Method Details

    • getAnalysisReportFile

      @Input @Optional public String getAnalysisReportFile()
      The name of the analysis report file produced by analyzeCustomProperties to read referenced property keys from. Defaults to custom-properties-analysis.json.
      Returns:
      The analysis report file name
    • getOutputFile

      @Input @Optional public String getOutputFile()
      The name of the generated JSON report file listing unused properties.

      The file is written to build/reports/<outputFile>. Defaults to unused-properties-report.json.

      Returns:
      The unused properties output file name
    • getAdditionalPropertiesPattern

      @Input @Optional public String getAdditionalPropertiesPattern()
      Glob pattern for additional property files to include when scanning for unused keys, beyond the baseline application.properties, application.yml, and application.yaml.

      Use this to include profile-specific files such as application-prod.yml in the unused property check. This option is independent of the same option on analyzeCustomProperties — set it based on which files you want to audit for orphaned keys.

      Returns:
      The additional properties glob pattern
    • getIgnoreUnused

      @Input public boolean getIgnoreUnused()
      Whether to suppress build failure when unused properties are found.

      When false (the default), the build fails if any unused properties are detected. When true, unused properties are reported in the JSON output and console but the build continues.

      Returns:
      True if ignore unused flag is set, otherwise false
    • getVerboseMode

      @Input public boolean getVerboseMode()
      Whether to print the full unused properties results to the console in addition to the JSON report. Defaults to false.
      Returns:
      True if verbose mode is enabled, otherwise false
    • setAnalysisReportFile

      public void setAnalysisReportFile(String analysisReportFile)
      Option to set a custom analysis report file name
      Parameters:
      analysisReportFile - Name of the analysis report produced by analyzeCustomProperties. Default: custom-properties-analysis.json
    • setOutputFile

      public void setOutputFile(String outputFile)
      Option to set a custom output file name
      Parameters:
      outputFile - Name of the generated unused-properties report file. Default: unused-properties-report.json
    • setAdditionalPropertiesPattern

      public void setAdditionalPropertiesPattern(String additionalPropertiesPattern)
      Option to set an additional properties glob pattern
      Parameters:
      additionalPropertiesPattern - Glob pattern for additional property files to include (e.g. 'application-*.yml'). Should match what was passed to analyzeCustomProperties.
    • setIgnoreUnused

      public void setIgnoreUnused(boolean ignoreUnused)
      Option to enable/disable build failure when unused properties are detected
      Parameters:
      ignoreUnused - If set, unused properties are reported but the build does not fail.
    • setVerboseMode

      public void setVerboseMode(boolean verboseMode)
      Option to enable/disable verbose console outputs
      Parameters:
      verboseMode - Enable verbose console output.
    • getAnalysisReportFileProvider

      @InputFile public org.gradle.api.provider.Provider<org.gradle.api.file.RegularFile> getAnalysisReportFileProvider()
      The analysis report file produced by analyzeCustomProperties, used as input to determine which property keys are referenced in code. Resolved to build/reports/<analysisReportFile>.
      Returns:
      The analysis report file provider
    • getReportFile

      @OutputFile public org.gradle.api.provider.Provider<org.gradle.api.file.RegularFile> getReportFile()
      The output file to which the unused properties JSON report is written. Resolved to build/reports/<outputFile>.
      Returns:
      The report file provider
    • check

      public void check()
      Task action that compares defined configuration keys against code-referenced keys and flags any that are unused.