Skip to content

feat(spotless/gradle-plugin): Allow custom string format for license header copyright year/year range#2965

Merged
nedtwigg merged 14 commits into
diffplug:mainfrom
Maciej-Sitarz-IBM:msitarz/feature/copyright_year_format
Jun 16, 2026
Merged

feat(spotless/gradle-plugin): Allow custom string format for license header copyright year/year range#2965
nedtwigg merged 14 commits into
diffplug:mainfrom
Maciej-Sitarz-IBM:msitarz/feature/copyright_year_format

Conversation

@Maciej-Sitarz-IBM

@Maciej-Sitarz-IBM Maciej-Sitarz-IBM commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

TL;DR:

Initial feature implementation allowing users to specify string format for year and year range during license header copyright year update and formatting. Allows left/right padding of the year/year range string.

Usage:

setLicenseStep("licenseHeader('/** $YEAR */').yearStringFormat('%-12s')");

Feature status

  • The feature requires some additional work as one of the implemented tests fails: LicenseHeaderTest.withYearStringFormat_spacesBefore()
    (I didn't spend much time on it, as first I wanted to get confirmation if this feature could be merged.)
  • I intentionally left some debug println() in the code, should be removed after the above issue is fixed and before PR merge

Questions

  1. Is it the proper approach to implement such functionality? Does it have the potential to be merged?
  2. If the answer to the above is "Yes". I would work on fixing the failing test and improving the code if needed.

Problem Statement

This PR implements an initial version of a new feature that allows providing a string format for the year/year range that is inserted in the license header copyright ($YEAR placeholder in license file template).

Currently if the license template file is formatted for year range (9 characters) formatting of a license header containing single year is missing 5 spaces (the closing bracket isn't aligned).

Current:

Single year:

/* ***************************************************************** */
/*                                                                   */
/* (C) Copyright IBM Corp. 2023                                 */
/*                                                                   */
/* ***************************************************************** */

Year range:

/* ***************************************************************** */
/*                                                                   */
/* (C) Copyright IBM Corp. 2023-2026                                 */
/*                                                                   */
/* ***************************************************************** */

Expected:

Single year:

/* ***************************************************************** */
/*                                                                   */
/* (C) Copyright IBM Corp. 2023                                      */
/*                                                                   */
/* ***************************************************************** */

Year range:

/* ***************************************************************** */
/*                                                                   */
/* (C) Copyright IBM Corp. 2023-2026                                 */
/*                                                                   */
/* ***************************************************************** */

How to test the new feature?

./gradlew :plugin-gradle:test --tests com.diffplug.gradle.spotless.LicenseHeaderTest

Currently used workaround

def singleYearLicenseRegex = "^/\\* \\(C\\) Copyright IBM Corp\\. ([0-9]{4})                                \\*/"
def singleYearLicenseAlignedFmt = '/* (C) Copyright IBM Corp. $1                                      */'

replaceRegex('Fix misaligned license header closing bracket.',
  singleYearLicenseRegex,
  singleYearLicenseAlignedFmt)

@nedtwigg

nedtwigg commented Jun 9, 2026

Copy link
Copy Markdown
Member

Happy to merge something like this, the approach looks good, obviously the System.out.printlns gotta go

@nedtwigg

nedtwigg commented Jun 9, 2026

Copy link
Copy Markdown
Member

@Maciej-Sitarz-IBM

Copy link
Copy Markdown
Contributor Author

Changes:

  • merged latest changes from main
  • cleaned the code
  • simplified and improved the implementation of the feature
  • added info to plugin-maven/CHANGES.md (as the new feature is also available there)

@nedtwigg I think the PR is ready to be reviewed.

The 8.7.0/3.7.0 releases were published after this branch added its
changelog entries, sweeping them into the now-versioned sections. Move
them back into [Unreleased] since diffplug#2965 is not yet released.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@nedtwigg nedtwigg merged commit 6d9c1b9 into diffplug:main Jun 16, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants