From 06af7eddd5b5d596c70d07722adfc8595aea4ef0 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 27 Jun 2020 23:16:09 -0700 Subject: [PATCH 1/8] Add docs for the license header. --- plugin-maven/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index bcba6f6e3b..66e3165c5c 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -526,6 +526,36 @@ Unlike Eclipse, Spotless WTP ignores per default external URIs in schema locatio external entities. To allow the access of external URIs, set the property `resolveExternalURI` to true. + + +## License header + +Spotless can inject a license header into your files, including populating an accurate copyright header from today's date or from git history. + +```xml + + + + /* Licensed under Apache-2.0 */ + ${basedir}/license-header + + + # + +``` + +If the license header (specified with `licenseHeader` or `licenseHeaderFile`) contains `$YEAR` or `$today.year`, then that token will be replaced with the current 4-digit year. For example, if Spotless is launched in 2020, then `/* Licensed under Apache-2.0 $YEAR. */` will produce `/* Licensed under Apache-2.0 2020. */` + +Once a file's license header has a valid year, whether it is a year (`2020`) or a year range (`2017-2020`), it will not be changed. If you want the date to be updated when it changes, enable the [`ratchetFrom` functionality](#ratchet), and the year will be automatically set to today's year according to the following table (assuming the current year is 2020): + +* No license header -> `2020` +* `2017` -> `2017-2020` +* `2017-2019` -> `2017-2020` + +### Retroactively populating year range from git history + +If your project has not been rigorous with copyright headers, and you'd like to use git history to repair this retroactively, you can do so with `-DspotlessSetLicenseHeaderYearsFromGitHistory=true`. When run in this mode, Spotless will do an expensive search through git history for each file, and set the copyright header based on the oldest and youngest commits for that file. This is intended to be a one-off sort of thing. + ## Line endings and encodings (invisible stuff) From 527b3273f33580abe49c166353e192c50fe97eb3 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 27 Jun 2020 23:20:51 -0700 Subject: [PATCH 2/8] Plumbing to allow the LicenseHeader step to respond to `ratchetFrom`. --- .../com/diffplug/spotless/maven/FormatterFactory.java | 2 +- .../diffplug/spotless/maven/FormatterStepConfig.java | 11 +++++++++-- .../spotless/maven/generic/LicenseHeader.java | 6 +++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java index bbed6ad1a4..f44d440c85 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java @@ -144,7 +144,7 @@ Optional ratchetFrom(FormatterConfig config) { } private FormatterStepConfig stepConfig(Charset encoding, FormatterConfig config) { - return new FormatterStepConfig(encoding, licenseHeaderDelimiter(), config.getProvisioner(), config.getFileLocator()); + return new FormatterStepConfig(encoding, licenseHeaderDelimiter(), ratchetFrom(config), config.getProvisioner(), config.getFileLocator()); } private static List gatherStepFactories(List allGlobal, List allConfigured) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java index dfe038a4c0..50bdf5219d 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2020 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.diffplug.spotless.maven; import java.nio.charset.Charset; +import java.util.Optional; import com.diffplug.spotless.Provisioner; @@ -23,12 +24,14 @@ public class FormatterStepConfig { private final Charset encoding; private final String licenseHeaderDelimiter; + private final Optional ratchetFrom; private final Provisioner provisioner; private final FileLocator fileLocator; - public FormatterStepConfig(Charset encoding, String licenseHeaderDelimiter, Provisioner provisioner, FileLocator fileLocator) { + public FormatterStepConfig(Charset encoding, String licenseHeaderDelimiter, Optional ratchetFrom, Provisioner provisioner, FileLocator fileLocator) { this.encoding = encoding; this.licenseHeaderDelimiter = licenseHeaderDelimiter; + this.ratchetFrom = ratchetFrom; this.provisioner = provisioner; this.fileLocator = fileLocator; } @@ -41,6 +44,10 @@ public String getLicenseHeaderDelimiter() { return licenseHeaderDelimiter; } + public Optional getRatchetFrom() { + return ratchetFrom; + } + public Provisioner getProvisioner() { return provisioner; } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java index 1ea925d5fe..105aba2d49 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2020 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,7 @@ public final FormatterStep newFormatterStep(FormatterStepConfig config) { if (file != null ^ content != null) { FormatterStep step = file != null ? createStepFromFile(config, delimiterString) - : createStepFromContent(delimiterString); + : createStepFromContent(config, delimiterString); return step.filterByFile(LicenseHeaderStep.unsupportedJvmFilesFilter()); } else { @@ -58,7 +58,7 @@ private FormatterStep createStepFromFile(FormatterStepConfig config, String deli return LicenseHeaderStep.createFromFile(licenseHeaderFile, config.getEncoding(), delimiterString); } - private FormatterStep createStepFromContent(String delimiterString) { + private FormatterStep createStepFromContent(FormatterStepConfig config, String delimiterString) { return LicenseHeaderStep.createFromHeader(content, delimiterString); } } From 488483a94c6b907ef0070f73828140d0e6516196 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 27 Jun 2020 23:28:27 -0700 Subject: [PATCH 3/8] LicenseHeader now updates year when `rachetFrom` is enabled. --- .../spotless/maven/generic/LicenseHeader.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java index 105aba2d49..705464fa76 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java @@ -15,7 +15,7 @@ */ package com.diffplug.spotless.maven.generic; -import java.io.File; +import java.nio.file.Files; import org.apache.maven.plugins.annotations.Parameter; @@ -41,24 +41,22 @@ public final FormatterStep newFormatterStep(FormatterStepConfig config) { if (delimiterString == null) { throw new IllegalArgumentException("You need to specify 'delimiter'."); } - if (file != null ^ content != null) { - FormatterStep step = file != null - ? createStepFromFile(config, delimiterString) - : createStepFromContent(config, delimiterString); - - return step.filterByFile(LicenseHeaderStep.unsupportedJvmFilesFilter()); + FormatterStep unfiltered = FormatterStep.createLazy(LicenseHeaderStep.name(), () -> { + // by default, we should update the year if the user is using ratchetFrom + boolean updateYear = config.getRatchetFrom().isPresent(); + String header; + if (content != null) { + header = content; + } else { + byte[] raw = Files.readAllBytes(config.getFileLocator().locateFile(file).toPath()); + header = new String(raw, config.getEncoding()); + } + return new LicenseHeaderStep(header, delimiterString, LicenseHeaderStep.defaultYearDelimiter(), updateYear); + }, step -> step::format); + return unfiltered.filterByFile(LicenseHeaderStep.unsupportedJvmFilesFilter()); } else { throw new IllegalArgumentException("Must specify exactly one of 'file' or 'content'."); } } - - private FormatterStep createStepFromFile(FormatterStepConfig config, String delimiterString) { - File licenseHeaderFile = config.getFileLocator().locateFile(file); - return LicenseHeaderStep.createFromFile(licenseHeaderFile, config.getEncoding(), delimiterString); - } - - private FormatterStep createStepFromContent(FormatterStepConfig config, String delimiterString) { - return LicenseHeaderStep.createFromHeader(content, delimiterString); - } } From ab59f3f06804b4960ce8c2fbd5bf1ca8f1f45c04 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 27 Jun 2020 23:42:08 -0700 Subject: [PATCH 4/8] Add support for spotlessSetLicenseHeaderYearsFromGitHistory --- .../spotless/maven/AbstractSpotlessMojo.java | 5 +- .../spotless/maven/FormatterConfig.java | 8 ++- .../spotless/maven/FormatterFactory.java | 2 +- .../spotless/maven/FormatterStepConfig.java | 8 ++- .../spotless/maven/generic/LicenseHeader.java | 55 +++++++++++++++---- 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index d3f410a754..be36757704 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -120,6 +120,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter(property = "spotlessFiles") private String filePatterns; + @Parameter(property = "spotlessSetLicenseHeaderYearsFromGitHistory") + private String setLicenseHeaderYearsFromGitHistory; + protected abstract void process(Iterable files, Formatter formatter) throws MojoExecutionException; @Override @@ -189,7 +192,7 @@ private FormatterConfig getFormatterConfig() { Provisioner provisioner = MavenProvisioner.create(resolver); List formatterStepFactories = getFormatterStepFactories(); FileLocator fileLocator = getFileLocator(); - return new FormatterConfig(baseDir, encoding, lineEndings, Optional.ofNullable(ratchetFrom), provisioner, fileLocator, formatterStepFactories); + return new FormatterConfig(baseDir, encoding, lineEndings, Optional.ofNullable(ratchetFrom), provisioner, fileLocator, formatterStepFactories, Optional.ofNullable(setLicenseHeaderYearsFromGitHistory)); } private FileLocator getFileLocator() { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java index 6127d93d58..52f2a871f3 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterConfig.java @@ -32,15 +32,17 @@ public class FormatterConfig { private final Provisioner provisioner; private final FileLocator fileLocator; private final List globalStepFactories; + private final Optional spotlessSetLicenseHeaderYearsFromGitHistory; public FormatterConfig(File baseDir, String encoding, LineEnding lineEndings, Optional ratchetFrom, Provisioner provisioner, - FileLocator fileLocator, List globalStepFactories) { + FileLocator fileLocator, List globalStepFactories, Optional spotlessSetLicenseHeaderYearsFromGitHistory) { this.encoding = encoding; this.lineEndings = lineEndings; this.ratchetFrom = ratchetFrom; this.provisioner = provisioner; this.fileLocator = fileLocator; this.globalStepFactories = globalStepFactories; + this.spotlessSetLicenseHeaderYearsFromGitHistory = spotlessSetLicenseHeaderYearsFromGitHistory; } public String getEncoding() { @@ -63,6 +65,10 @@ public List getGlobalStepFactories() { return unmodifiableList(globalStepFactories); } + public Optional getSpotlessSetLicenseHeaderYearsFromGitHistory() { + return spotlessSetLicenseHeaderYearsFromGitHistory; + } + public FileLocator getFileLocator() { return fileLocator; } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java index f44d440c85..d74634382c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java @@ -144,7 +144,7 @@ Optional ratchetFrom(FormatterConfig config) { } private FormatterStepConfig stepConfig(Charset encoding, FormatterConfig config) { - return new FormatterStepConfig(encoding, licenseHeaderDelimiter(), ratchetFrom(config), config.getProvisioner(), config.getFileLocator()); + return new FormatterStepConfig(encoding, licenseHeaderDelimiter(), ratchetFrom(config), config.getProvisioner(), config.getFileLocator(), config.getSpotlessSetLicenseHeaderYearsFromGitHistory()); } private static List gatherStepFactories(List allGlobal, List allConfigured) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java index 50bdf5219d..a1f2e52b41 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterStepConfig.java @@ -27,13 +27,15 @@ public class FormatterStepConfig { private final Optional ratchetFrom; private final Provisioner provisioner; private final FileLocator fileLocator; + private final Optional spotlessSetLicenseHeaderYearsFromGitHistory; - public FormatterStepConfig(Charset encoding, String licenseHeaderDelimiter, Optional ratchetFrom, Provisioner provisioner, FileLocator fileLocator) { + public FormatterStepConfig(Charset encoding, String licenseHeaderDelimiter, Optional ratchetFrom, Provisioner provisioner, FileLocator fileLocator, Optional spotlessSetLicenseHeaderYearsFromGitHistory) { this.encoding = encoding; this.licenseHeaderDelimiter = licenseHeaderDelimiter; this.ratchetFrom = ratchetFrom; this.provisioner = provisioner; this.fileLocator = fileLocator; + this.spotlessSetLicenseHeaderYearsFromGitHistory = spotlessSetLicenseHeaderYearsFromGitHistory; } public Charset getEncoding() { @@ -55,4 +57,8 @@ public Provisioner getProvisioner() { public FileLocator getFileLocator() { return fileLocator; } + + public Optional spotlessSetLicenseHeaderYearsFromGitHistory() { + return spotlessSetLicenseHeaderYearsFromGitHistory; + } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java index 705464fa76..fe71531d6a 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java @@ -15,10 +15,13 @@ */ package com.diffplug.spotless.maven.generic; +import java.io.File; +import java.io.IOException; import java.nio.file.Files; import org.apache.maven.plugins.annotations.Parameter; +import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterStepConfig; @@ -42,21 +45,49 @@ public final FormatterStep newFormatterStep(FormatterStepConfig config) { throw new IllegalArgumentException("You need to specify 'delimiter'."); } if (file != null ^ content != null) { - FormatterStep unfiltered = FormatterStep.createLazy(LicenseHeaderStep.name(), () -> { - // by default, we should update the year if the user is using ratchetFrom - boolean updateYear = config.getRatchetFrom().isPresent(); - String header; - if (content != null) { - header = content; - } else { - byte[] raw = Files.readAllBytes(config.getFileLocator().locateFile(file).toPath()); - header = new String(raw, config.getEncoding()); - } - return new LicenseHeaderStep(header, delimiterString, LicenseHeaderStep.defaultYearDelimiter(), updateYear); - }, step -> step::format); + FormatterStep unfiltered; + if ("true".equals(config.spotlessSetLicenseHeaderYearsFromGitHistory().orElse(""))) { + unfiltered = FormatterStep.createNeverUpToDateLazy(LicenseHeaderStep.name(), () -> { + boolean updateYear = false; // doesn't matter + LicenseHeaderStep step = new LicenseHeaderStep(readFileOrContent(config), delimiterString, LicenseHeaderStep.defaultYearDelimiter(), updateYear); + return new FormatterFunc() { + @Override + public String apply(String input, File source) throws Exception { + return step.setLicenseHeaderYearsFromGitHistory(input, source); + } + + @Override + public String apply(String input) throws Exception { + throw new UnsupportedOperationException(); + } + }; + }); + } else { + unfiltered = FormatterStep.createLazy(LicenseHeaderStep.name(), () -> { + // by default, we should update the year if the user is using ratchetFrom + boolean updateYear = config.getRatchetFrom().isPresent(); + String header; + if (content != null) { + header = content; + } else { + byte[] raw = Files.readAllBytes(config.getFileLocator().locateFile(file).toPath()); + header = new String(raw, config.getEncoding()); + } + return new LicenseHeaderStep(header, delimiterString, LicenseHeaderStep.defaultYearDelimiter(), updateYear); + }, step -> step::format); + } return unfiltered.filterByFile(LicenseHeaderStep.unsupportedJvmFilesFilter()); } else { throw new IllegalArgumentException("Must specify exactly one of 'file' or 'content'."); } } + + private String readFileOrContent(FormatterStepConfig config) throws IOException { + if (content != null) { + return content; + } else { + byte[] raw = Files.readAllBytes(config.getFileLocator().locateFile(file).toPath()); + return new String(raw, config.getEncoding()); + } + } } From 30aad3b74281149b19795e01b81000167c2a315b Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 29 Jun 2020 11:08:58 -0700 Subject: [PATCH 5/8] Use constant for spotlessSetLicenseHeaderYearsFromGitHistory --- .../java/com/diffplug/spotless/generic/LicenseHeaderStep.java | 2 +- .../java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index 0824046366..47d2289905 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -213,7 +213,7 @@ public String format(String raw) { } } - private static final String spotlessSetLicenseHeaderYearsFromGitHistory = "spotlessSetLicenseHeaderYearsFromGitHistory"; + public static final String spotlessSetLicenseHeaderYearsFromGitHistory = "spotlessSetLicenseHeaderYearsFromGitHistory"; public static final String FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY() { return spotlessSetLicenseHeaderYearsFromGitHistory; diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index be36757704..0ce5cbae2d 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -46,6 +46,7 @@ import com.diffplug.spotless.Formatter; import com.diffplug.spotless.LineEnding; import com.diffplug.spotless.Provisioner; +import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.cpp.Cpp; import com.diffplug.spotless.maven.generic.Format; import com.diffplug.spotless.maven.generic.LicenseHeader; @@ -120,7 +121,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter(property = "spotlessFiles") private String filePatterns; - @Parameter(property = "spotlessSetLicenseHeaderYearsFromGitHistory") + @Parameter(property = LicenseHeaderStep.spotlessSetLicenseHeaderYearsFromGitHistory) private String setLicenseHeaderYearsFromGitHistory; protected abstract void process(Iterable files, Formatter formatter) throws MojoExecutionException; From aeab7586a0047954514d43ab201f748bfb1d0f68 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 29 Jun 2020 11:18:41 -0700 Subject: [PATCH 6/8] Update changelog. --- plugin-maven/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 8e999a4897..ba4ce0c0cc 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * You can now ratchet a project's style by limiting Spotless only to files which have changed since a given [git reference](https://javadoc.io/static/org.eclipse.jgit/org.eclipse.jgit/5.6.1.202002131546-r/org/eclipse/jgit/lib/Repository.html#resolve-java.lang.String-), e.g. `ratchetFrom 'origin/main'`. ([#590](https://github.com/diffplug/spotless/pull/590)) * Huge speed improvement for multi-module projects thanks to improved cross-project classloader caching ([#571](https://github.com/diffplug/spotless/pull/571), fixes [#559](https://github.com/diffplug/spotless/issues/559)). +* If you specify `-PspotlessSetLicenseHeaderYearsFromGitHistory=true`, Spotless will perform an expensive search through git history to determine the oldest and newest commits for each file, and uses that to determine license header years. ([#626](https://github.com/diffplug/spotless/pull/626)) * `prettier` will now autodetect the parser (and formatter) to use based on the filename, unless you override this using `config` or `configFile` with the option `parser` or `filepath` ([#620](https://github.com/diffplug/spotless/pull/620)). ## [1.31.3] - 2020-06-17 From 757fcad7862da4a7eb95cb577271150c47f79dce Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 29 Jun 2020 11:20:59 -0700 Subject: [PATCH 7/8] Update readme. --- plugin-maven/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 66e3165c5c..d27620abf3 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -544,7 +544,7 @@ Spotless can inject a license header into your files, including populating an ac ``` -If the license header (specified with `licenseHeader` or `licenseHeaderFile`) contains `$YEAR` or `$today.year`, then that token will be replaced with the current 4-digit year. For example, if Spotless is launched in 2020, then `/* Licensed under Apache-2.0 $YEAR. */` will produce `/* Licensed under Apache-2.0 2020. */` +If the license header (specified with `content` or `file`) contains `$YEAR` or `$today.year`, then that token will be replaced with the current 4-digit year. For example, if Spotless is launched in 2020, then `/* Licensed under Apache-2.0 $YEAR. */` will produce `/* Licensed under Apache-2.0 2020. */` Once a file's license header has a valid year, whether it is a year (`2020`) or a year range (`2017-2020`), it will not be changed. If you want the date to be updated when it changes, enable the [`ratchetFrom` functionality](#ratchet), and the year will be automatically set to today's year according to the following table (assuming the current year is 2020): From f46feff17b40610756d0c6d3efeeef3f94a61f2d Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 29 Jun 2020 17:57:15 -0700 Subject: [PATCH 8/8] Fix issues found by @lutovich --- plugin-maven/CHANGES.md | 2 +- .../diffplug/spotless/maven/generic/LicenseHeader.java | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index ba4ce0c0cc..17bfdf80c4 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -6,7 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * You can now ratchet a project's style by limiting Spotless only to files which have changed since a given [git reference](https://javadoc.io/static/org.eclipse.jgit/org.eclipse.jgit/5.6.1.202002131546-r/org/eclipse/jgit/lib/Repository.html#resolve-java.lang.String-), e.g. `ratchetFrom 'origin/main'`. ([#590](https://github.com/diffplug/spotless/pull/590)) * Huge speed improvement for multi-module projects thanks to improved cross-project classloader caching ([#571](https://github.com/diffplug/spotless/pull/571), fixes [#559](https://github.com/diffplug/spotless/issues/559)). -* If you specify `-PspotlessSetLicenseHeaderYearsFromGitHistory=true`, Spotless will perform an expensive search through git history to determine the oldest and newest commits for each file, and uses that to determine license header years. ([#626](https://github.com/diffplug/spotless/pull/626)) +* If you specify `-DspotlessSetLicenseHeaderYearsFromGitHistory=true`, Spotless will perform an expensive search through git history to determine the oldest and newest commits for each file, and uses that to determine license header years. ([#626](https://github.com/diffplug/spotless/pull/626)) * `prettier` will now autodetect the parser (and formatter) to use based on the filename, unless you override this using `config` or `configFile` with the option `parser` or `filepath` ([#620](https://github.com/diffplug/spotless/pull/620)). ## [1.31.3] - 2020-06-17 diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java index fe71531d6a..dc7a200000 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java @@ -66,13 +66,7 @@ public String apply(String input) throws Exception { unfiltered = FormatterStep.createLazy(LicenseHeaderStep.name(), () -> { // by default, we should update the year if the user is using ratchetFrom boolean updateYear = config.getRatchetFrom().isPresent(); - String header; - if (content != null) { - header = content; - } else { - byte[] raw = Files.readAllBytes(config.getFileLocator().locateFile(file).toPath()); - header = new String(raw, config.getEncoding()); - } + String header = readFileOrContent(config); return new LicenseHeaderStep(header, delimiterString, LicenseHeaderStep.defaultYearDelimiter(), updateYear); }, step -> step::format); }