Thursday 2 August 2012

How to Add License Headers to Code Files via Maven?

This post explains how to automate the inclusion of headers in code files via Maven. We will add the following header (the code sample is available on GitHub) :
/**
 * Copyright (C) 2010-2012 MyCompany - All rights reserved.
 */
This header is based on a template located in src/main/resources/fileHeader.txt:
Copyright (C) ${h_inceptionYear}-${h_currentYear} ${h_copyrightOwner} - All rights reserved.
It contains three ${parameters} which are configured in the pom.xml. Two are defined in the properties:
<properties>
  ...
  <inceptionYear>2010</inceptionYear>
  <copyrightOwner>MyCompany</copyrightOwner>
</properties>
One is computed via a plugin (the current year):
<!-- Creating current year element -->
<plugin>
  <groupId>org.codehaus.groovy.maven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          import java.util.Date
          import java.text.MessageFormat
          def vartimestamp = MessageFormat.format("{0,date,yyyy}", new Date())
          project.properties['currentYear'] = vartimestamp
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>
The template location, together with the parameters mapping and the types of files to ignore, is configured here:
  <!-- Adding header to file -->
  <plugin>
    <groupId>com.mycila.maven-license-plugin</groupId>
    <artifactId>maven-license-plugin</artifactId>
    <version>1.8.0</version>
    <configuration>
      <!-- Template location -->
      <header>src/main/resources/fileHeader.txt</header>
      <properties>
        <!-- Values to be substituted in template -->
        <h_inceptionYear>${inceptionYear}</h_inceptionYear>
        <h_currentYear>${currentYear}</h_currentYear>
        <h_copyrightOwner>${copyrightOwner}</h_copyrightOwner>
      </properties>
      <strictCheck>true</strictCheck>
      <excludes>
        <exclude>**/*.html</exclude>
        <exclude>**/*.xml</exclude>
        <exclude>**/*.txt</exclude>
        <exclude>**/*.ec</exclude>
        <exclude>**/*.log</exclude>
        <exclude>**/*.css</exclude>
        <exclude>**/*.js</exclude>
        <exclude>**/*.jsp</exclude>
        <exclude>**/*.md</exclude>
      </excludes>
    </configuration>
    <executions>
      <execution>
        <phase>validate</phase>
        <goals>
          <goal>format</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

During the build process, proper headers are added (or updated) in code files:
/**
 * Copyright (C) 2010-2012 MyCompany - All rights reserved.
 */

package com.mypackage;

public class MyClass {

  // ...

}

2 comments:

  1. And if I need to put this comment after the fist line!?

    ReplyDelete
    Replies
    1. I am not sure it is possible with this pluggin...

      Delete