Skip to content

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

Open
Maciej-Sitarz-IBM wants to merge 6 commits into
diffplug:mainfrom
Maciej-Sitarz-IBM:msitarz/feature/copyright_year_format
Open

feat(spotless/gradle-plugin): Allow custom string format for license header copyright year/year range#2965
Maciej-Sitarz-IBM wants to merge 6 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

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

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