Quantcast
Channel: Michael Vorburger's Blog v2
Viewing all articles
Browse latest Browse all 64

How-to clean up and enforce open-source license headers in Maven-based build

$
0
0
The other day I had to make sure that anopen source project had clean and proper copyright and license headers in all its files. For Apache Maven-based builds, here is a quick write up illustrating how you can automate doing this. The main steps are:

1. strip existing old license headers (if needed), as the automated tool requires a particular format
2. automatically apply new standard header to all files
3. ensure that new files added in the future which don't include the new standard header fail your build
4. using your source control tool to compare before/after, and manually fix-up any lost names etc.

For some probably historical reason there appear to be two very similar Maven plug-in for this, namely the Mycila Maven License Plug-in and the Codehouse Mojo License Maven Plugin (confusingly both named "license-maven-plugin", but with a different groupId). We'll be using Mycila's for the stripping (as Codehouse' doesn't seem to include that feature), and Codehouse for the re-adding and future enforcement.

So to strip existing old license headers, add this to your (parent) pom.xml:

<plugin>
   <groupId>com.mycila</groupId>
   <artifactId>license-maven-plugin</artifactId>
   <version>2.6</version>
   <configuration>
       <header>com/mycila/maven/plugin/license/templates/APACHE-2.txt</header>
   </configuration>
   <executions>
       <execution>
           <goals>
               <goal>check</goal>
           </goals>
       </execution>
   </executions>
</plugin>

Then run this:

$ mvn com.mycila:license-maven-plugin:remove

Doing e.g. a git diff now will show you that existing license headers have been removed. As this was a one-time operation, you can now remove the com.mycila:license-maven-plugin from your pom.xml again, and add this instead:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>1.7</version>
                <configuration>
                    <licenseName>apache_v2</licenseName>
                   <addJavaLicenseAfterPackage>false</addJavaLicenseAfterPackage>
                    <failOnMissingHeader>true</failOnMissingHeader>
                    <failOnNotUptodateHeader>true</failOnNotUptodateHeader>
                    <roots>
                        <root>src/main/java</root>
                        <root>src/test/java</root>
                    </roots>
                </configuration>
<executions>
   <execution>
       <phase>validate</phase>
       <goals>
           <goal>check-file-header</goal>
       </goals>
   </execution>
</executions>
</plugin>

You can change the licenseName to any of those listed by mvn license:license-list. Now run:

$ mvn license:update-file-header

and do e.g. a git diff now to see that you that new license headers, in the format suitable for automate tooling. If you've previously removed old license headers, then at this point you might want to do a before/after comparison, and manually fix-up any lost names etc.

Note how the pom.xml configuration above binds mvn license:check-file-header to the Maven lifecycle validate phase. This means that your build (incl. e.g. your TravisCI or Jenkins-based GitHub pull request) now automatically enforces presence of these headers. A new file added missing them will cause any future mvn verify / test / package etc. to fail with:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:license-maven-plugin:1.7:check-file-header (default-cli) on project org.eclipse.emf.eson: There are .. file(s) with no header :
[ERROR] /home/vorburger/dev/ngMUI/com.temenos.ds.op/SUBS/eson/org.eclipse.emf.eson/src/org/eclipse/emf/eson/EFactoryRuntimeModule.java

If using Eclipse, you may also would like to set up  menu Project > Properties (or Window > Preferences) > Java > Code Style > Code Templates, Comments > Files for new Java files to get your header by default.

If your build is using Gradle instead of Maven, something very similar can be done using the license-gradle-plugin from nl.javadude.gradle.plugins; you can see how to configure it e.g. in the build.gradle of the open source microfinance platform Mifos X.

Viewing all articles
Browse latest Browse all 64

Trending Articles