Skip to content

Commit

Permalink
Fix #379 allow users to customize the copyright string format (#380)
Browse files Browse the repository at this point in the history
Co-authored-by: Dave Nice <dave.nice@uk.ibm.com>
Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
  • Loading branch information
3 people committed Jun 6, 2023
1 parent 040993a commit 901476b
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/it/override-copyright-format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Integration test for issue #379

## description

Verify that the copyright statement format can be overriden.
23 changes: 23 additions & 0 deletions src/it/override-copyright-format/invoker.properties
Original file line number Diff line number Diff line change
@@ -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
# <http://www.gnu.org/licenses/lgpl-3.0.html>.
# #L%
###
invoker.goals=clean package
invoker.failureBehavior=fail-fast
117 changes: 117 additions & 0 deletions src/it/override-copyright-format/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>


<groupId>org.codehaus.mojo.license.test</groupId>
<artifactId>test-override-copyright-format</artifactId>
<version>1.0</version>
<name>License Test :: override-copyright-format</name>
<packaging>jar</packaging>

<organization>
<name>org.example</name>
</organization>

<inceptionYear>2018</inceptionYear>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@maven-compiler-plugin.version@</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>

<plugins>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>@build-helper-maven-plugin.version@</version>
<executions>
<execution>
<id>add-generated-source-directory</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>@maven-resources-plugin.version@</version>

<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>

<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>

<resources>
<resource>
<directory>src/main/original/java</directory>
<filtering>false</filtering>
<include>**/*.java</include>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>@project.version@</version>

<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>update-file-header</goal>
</goals>
<configuration>

<encoding>UTF-8</encoding>
<roots>
<root>${project.build.directory}/generated-sources</root>
</roots>

<copyrightStringFormat>(c) doo bee doo %2$s %1$s</copyrightStringFormat>

<licenseName>lgpl_v3</licenseName>

</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
29 changes: 29 additions & 0 deletions src/it/override-copyright-format/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -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
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #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;
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@Deprecated
package org.example.test;
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public abstract class AbstractLicenseNameMojo
/**
* Name of project's organization.
* <p>
* Will be used as copyrigth's holder in new header.
* Will be used as copyright's holder in new header.
*
* @since 1.0
*/
Expand All @@ -148,6 +148,17 @@ public abstract class AbstractLicenseNameMojo
@Parameter( property = "license.copyrightOwners" )
protected String copyrightOwners;

/**
* optional copyright string format
* <p>
* 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.
* <p>
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
47 changes: 41 additions & 6 deletions src/main/java/org/codehaus/mojo/license/model/Copyright.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 )
{

Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 901476b

Please sign in to comment.