From 901476b2055f38cdfb5d63b62466f6561f34751d Mon Sep 17 00:00:00 2001 From: davenice <32769325+davenice@users.noreply.github.com> Date: Tue, 6 Jun 2023 23:12:06 +0100 Subject: [PATCH] Fix #379 allow users to customize the copyright string format (#380) Co-authored-by: Dave Nice Co-authored-by: Slawomir Jaranowski --- src/it/override-copyright-format/README.md | 5 + .../invoker.properties | 23 ++++ src/it/override-copyright-format/pom.xml | 117 ++++++++++++++++++ .../postbuild.groovy | 29 +++++ .../java/org/example/test/SampleFile.java | 10 ++ .../java/org/example/test/package-info.java | 2 + .../mojo/license/AbstractFileHeaderMojo.java | 2 +- .../mojo/license/AbstractLicenseNameMojo.java | 19 ++- .../mojo/license/model/Copyright.java | 47 ++++++- 9 files changed, 243 insertions(+), 11 deletions(-) create mode 100644 src/it/override-copyright-format/README.md create mode 100644 src/it/override-copyright-format/invoker.properties create mode 100644 src/it/override-copyright-format/pom.xml create mode 100644 src/it/override-copyright-format/postbuild.groovy create mode 100644 src/it/override-copyright-format/src/main/original/java/org/example/test/SampleFile.java create mode 100644 src/it/override-copyright-format/src/main/original/java/org/example/test/package-info.java diff --git a/src/it/override-copyright-format/README.md b/src/it/override-copyright-format/README.md new file mode 100644 index 000000000..1d3535fbe --- /dev/null +++ b/src/it/override-copyright-format/README.md @@ -0,0 +1,5 @@ +# Integration test for issue #379 + +## description + +Verify that the copyright statement format can be overriden. \ No newline at end of file diff --git a/src/it/override-copyright-format/invoker.properties b/src/it/override-copyright-format/invoker.properties new file mode 100644 index 000000000..232528b32 --- /dev/null +++ b/src/it/override-copyright-format/invoker.properties @@ -0,0 +1,23 @@ +### +# #%L +# License Maven Plugin +# %% +# Copyright (C) 2008 - 2020 CodeLutin, Codehaus, Tony Chemit +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Lesser Public License for more details. +# +# You should have received a copy of the GNU General Lesser Public +# License along with this program. If not, see +# . +# #L% +### +invoker.goals=clean package +invoker.failureBehavior=fail-fast diff --git a/src/it/override-copyright-format/pom.xml b/src/it/override-copyright-format/pom.xml new file mode 100644 index 000000000..3ea049ac0 --- /dev/null +++ b/src/it/override-copyright-format/pom.xml @@ -0,0 +1,117 @@ + + + 4.0.0 + + + org.codehaus.mojo.license.test + test-override-copyright-format + 1.0 + License Test :: override-copyright-format + jar + + + org.example + + + 2018 + + + UTF-8 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + @maven-compiler-plugin.version@ + + 1.7 + 1.7 + + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + @build-helper-maven-plugin.version@ + + + add-generated-source-directory + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources + + + + + + + + maven-resources-plugin + @maven-resources-plugin.version@ + + + + generate-sources + generate-sources + + copy-resources + + + + ${project.build.directory}/generated-sources + + + + src/main/original/java + false + **/*.java + + + + + + + + + org.codehaus.mojo + license-maven-plugin + @project.version@ + + + + process-sources + + update-file-header + + + + UTF-8 + + ${project.build.directory}/generated-sources + + + (c) doo bee doo %2$s %1$s + + lgpl_v3 + + + + + + + + diff --git a/src/it/override-copyright-format/postbuild.groovy b/src/it/override-copyright-format/postbuild.groovy new file mode 100644 index 000000000..4b1aadfe1 --- /dev/null +++ b/src/it/override-copyright-format/postbuild.groovy @@ -0,0 +1,29 @@ +/* + * #%L + * License Maven Plugin + * %% + * Copyright (C) 2008 - 2020 CodeLutin, Codehaus, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +file = new File(basedir, 'target/generated-sources/org/example/test/SampleFile.java'); +assert file.exists(); +content = file.text; + +assert content.contains('(c) doo bee doo org.example 2018 - 20'); + +return true; diff --git a/src/it/override-copyright-format/src/main/original/java/org/example/test/SampleFile.java b/src/it/override-copyright-format/src/main/original/java/org/example/test/SampleFile.java new file mode 100644 index 000000000..27a7e986a --- /dev/null +++ b/src/it/override-copyright-format/src/main/original/java/org/example/test/SampleFile.java @@ -0,0 +1,10 @@ +package org.example.test; +import java.io.File; + + +public class SampleFile { + public static void main(String[] args) { + File file = new File(args[0]); + System.out.println(file.getAbsolutePath()); + } +} diff --git a/src/it/override-copyright-format/src/main/original/java/org/example/test/package-info.java b/src/it/override-copyright-format/src/main/original/java/org/example/test/package-info.java new file mode 100644 index 000000000..2a90f6d15 --- /dev/null +++ b/src/it/override-copyright-format/src/main/original/java/org/example/test/package-info.java @@ -0,0 +1,2 @@ +@Deprecated +package org.example.test; diff --git a/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java index 054005187..fb1656e07 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java @@ -701,7 +701,7 @@ private FileHeaderProcessor getFileHeaderProcessor( License license, FileHeaderT LOG.warn( "No inceptionYear defined (will use current year)" ); } - Copyright copyright = getCopyright( getCopyrightOwners() ); + Copyright copyright = getCopyright( copyrightStringFormat, getCopyrightOwners() ); header.setCopyright( copyright ); String licenseContent = license.getHeaderContent( getEncoding() ); diff --git a/src/main/java/org/codehaus/mojo/license/AbstractLicenseNameMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractLicenseNameMojo.java index 454dcc576..c8cac0653 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractLicenseNameMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractLicenseNameMojo.java @@ -121,7 +121,7 @@ public abstract class AbstractLicenseNameMojo /** * Name of project's organization. *

- * Will be used as copyrigth's holder in new header. + * Will be used as copyright's holder in new header. * * @since 1.0 */ @@ -148,6 +148,17 @@ public abstract class AbstractLicenseNameMojo @Parameter( property = "license.copyrightOwners" ) protected String copyrightOwners; + /** + * optional copyright string format + *

+ * If not set, "Copyright (C) %1$s %2$s" is used where the copyright dates are + * substituted for $1 and the copyright holder for $2. + * + * @since 2.1.0 + */ + @Parameter( property = "license.copyrightStringFormat" ) + protected String copyrightStringFormat; + /** * optional extra templates parameters. *

@@ -261,16 +272,16 @@ protected String processLicenseContext( String licenseContent ) templateParameters.put( "organizationName", organizationName ); templateParameters.put( "inceptionYear", inceptionYear ); - templateParameters.put( "copyright", getCopyright( getCopyrightOwners() ) ); + templateParameters.put( "copyright", getCopyright( copyrightStringFormat, getCopyrightOwners() ) ); templateParameters.put( "projectName", projectName ); addPropertiesToContext( extraTemplateParameters, "extra_", templateParameters ); return licenseFreeMarkerHelper.renderTemplate( FreeMarkerHelper.TEMPLATE, templateParameters ); } - Copyright getCopyright( String holder ) + Copyright getCopyright( String copyrightStringFormat, String holder ) { - return Copyright.newCopyright( inceptionYear, holder ); + return Copyright.newCopyright( copyrightStringFormat, inceptionYear, holder ); } /** diff --git a/src/main/java/org/codehaus/mojo/license/model/Copyright.java b/src/main/java/org/codehaus/mojo/license/model/Copyright.java index 55a5d74df..9f57ea188 100644 --- a/src/main/java/org/codehaus/mojo/license/model/Copyright.java +++ b/src/main/java/org/codehaus/mojo/license/model/Copyright.java @@ -36,10 +36,15 @@ public class Copyright { + /** + * Default copyright to string format. + */ + protected static final String DEFAULT_COPYRIGHT_TO_STRING_FORMAT = "Copyright (C) %1$s %2$s"; + /** * Copyright to string format. */ - protected static final String COPYRIGHT_TO_STRING_FORMAT = "Copyright (C) %1$s %2$s"; + protected String copyrightToStringFormat; /** * Copyright holder. @@ -57,17 +62,37 @@ public class Copyright */ protected Integer lastYear; - public static Copyright newCopyright( Integer inceptionYear, Integer lastYear, String holder ) + public static Copyright newCopyright( String copyrightToStringFormat, + Integer inceptionYear, Integer lastYear, String holder ) { int firstYear = inceptionYear == null ? lastYear : inceptionYear; if ( lastYear == null || firstYear >= lastYear ) { lastYear = null; } - Copyright result = new Copyright( firstYear, lastYear, holder ); + if ( copyrightToStringFormat == null ) + { + return newCopyright( DEFAULT_COPYRIGHT_TO_STRING_FORMAT, firstYear, lastYear, holder ); + } + Copyright result = new Copyright( copyrightToStringFormat, firstYear, lastYear, holder ); return result; } + public static Copyright newCopyright( Integer inceptionYear, Integer lastYear, String holder ) + { + return newCopyright( DEFAULT_COPYRIGHT_TO_STRING_FORMAT, inceptionYear, lastYear, holder ); + } + + public static Copyright newCopyright( String copyrightToStringFormat, Integer inceptionYear, String holder ) + { + + Calendar cal = Calendar.getInstance(); + cal.setTime( new Date() ); + Integer lastYear = cal.get( Calendar.YEAR ); + + return newCopyright( copyrightToStringFormat, inceptionYear, lastYear, holder ); + } + public static Copyright newCopyright( Integer inceptionYear, String holder ) { @@ -84,16 +109,26 @@ public Copyright() public Copyright( Copyright copyright ) { - this( copyright.getFirstYear(), copyright.getLastYear(), copyright.getHolder() ); + this( copyright.getCopyrightToStringFormat(), copyright.getFirstYear(), + copyright.getLastYear(), copyright.getHolder() ); } - public Copyright( int firstYear, Integer lastYear, String holder ) + public Copyright( String copyrightToStringFormat, int firstYear, Integer lastYear, String holder ) { + this.copyrightToStringFormat = copyrightToStringFormat; this.firstYear = firstYear; this.lastYear = lastYear; this.holder = holder; } + /** + * @return the copyright to string format + */ + public String getCopyrightToStringFormat() + { + return copyrightToStringFormat; + } + /** * @return the copyright holder */ @@ -169,7 +204,7 @@ public String getYears() */ public String getText() { - String copyright = String.format( COPYRIGHT_TO_STRING_FORMAT, getYears(), getHolder() ); + String copyright = String.format( getCopyrightToStringFormat(), getYears(), getHolder() ); return copyright; }