Sunday, September 30, 2012

Licensing your projects using maven

Well, I have to admit I didn't expect that.

I didn't expect to bump into a Maven plugin that really actually does all the work, so far 100% safe, generic, adaptable, and in less then a day, I've licensed over 250 projects, with different licenses groups, it was very very simple, and straight forward.

I also got something extra in addition to my licensing, implementing it forced me to define my poms better so the groups would share parents, and so on to the top most Maven parent pom, that defines the licensing.

First this is for Java source projects, using the Eclipse IDE, and building projects with Maven, you could also have Java Android projects...

This is the Maven licensing plugin we would be working with.
This is the Maven remote resources plugin we would be working with..

If you would browse a bit in the comments of the first link you would see a comment about the remote-resources plugin, so I gave it a go, worked, like a charm...

Setup:

We would need a Maven License Artifact(project), for the remote resources plugin.
It's pom should look somewhat like this:


<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>com.nu.art.software</groupId>
 <artifactId>mm-project-licenser</artifactId>
 <version>0.0.1</version>
 <packaging>jar</packaging>

 <name>_MavenMM-Project-Licenser</name>
 <url>http://maven.apache.org</url>

 <properties>

 </properties>
 
 <build>
  <plugins>
   <plugin>
    <artifactId>maven-remote-resources-plugin</artifactId>
    <version>1.3</version>
    <executions>
     <execution>
      <goals>
       <goal>bundle</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <includes>
      <include>nu-art-property-license-v1.0.txt</include>
      <include>mit-license.txt</include>
      <include>apache-license-v2.0.txt</include>
      <include>comment-style-license-header.xml</include>
     </includes>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
 </dependencies>
</project>


In the resources folder of that project I hold the licenses files... and the comment-style I wanted:


apache-license-v2.0.txt:

-----------------------------------------------------------------------------
 Copyright ${founded.year}-${current.year}, '${company}' AND/OR '${owner}' 
 
 For support you may email: <${email}>
   
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-----------------------------------------------------------------------------
 
 Last Builder: ${user.name}


comment-style-license-header.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<additionalHeaders>
    <javadoc_style>
        <firstLine>/*</firstLine>
        <beforeEachLine> * </beforeEachLine>
        <endLine> */</endLine>
        <!--skipLine></skipLine-->
        <firstLineDetectionPattern>(\s|\t)*/\*.*$</firstLineDetectionPattern>
        <lastLineDetectionPattern>.*\*/(\s|\t)*$</lastLineDetectionPattern>
        <allowBlankLines>false</allowBlankLines>
        <isMultiline>true</isMultiline>
    </javadoc_style>
</additionalHeaders>

Next is the licensed project pom, since the license plugin is defined in the top most parent pom, I would post snippets of it:
Some properties:
 <properties>
  <license.header>${license.nu-art}</license.header>
  <license.header.style>${license.header.style.comment}</license.header.style>
  <license.open-source.default>${license.open-source.apache2.0}</license.open-source.default>

  <license.header.style.comment>${project.build.directory}/maven-shared-archive-resources/comment-style-license-header.xml</license.header.style.comment>
  <license.open-source.apache2.0>${project.build.directory}/maven-shared-archive-resources/apache-license-v2.0.txt</license.open-source.apache2.0>
  <license.mit>${project.build.directory}/maven-shared-archive-resources/mit-license.txt</license.mit>
  <license.nu-art>${project.build.directory}/maven-shared-archive-resources/nu-art-property-license-v1.0.txt</license.nu-art>
 </properties>


And a licensing profile:
 <profile>
  <id>add-licenses</id>
  <build>
   <plugins>
    <plugin>
     <groupId>com.mycila.maven-license-plugin</groupId>
     <artifactId>maven-license-plugin</artifactId>
     <version>1.10.b1</version>
     <configuration>
      <basedir>${project.sourceDirectory}</basedir>
      <header>${license.header}</header>
      <failIfMissing>true</failIfMissing>
      <aggregate>true</aggregate>
      <strictCheck>true</strictCheck>
      <properties>
       <owner>${owner} AKA ${nickname}</owner>
       <company>${company.name}</company>
       <founded.year>${company.founded.year}</founded.year>
       <current.year>${current.year}</current.year>
       <email>${company.support}</email>
       <user.name>${user.name}</user.name>
       <timestamp>${maven.build.timestamp}</timestamp>
      </properties>
      <includes>
       <include>**/*.java</include>
      </includes>
      <excludes>
       <exclude>gen/**/R.java</exclude>
       <exclude>gen/**/BuildConfig.java</exclude>
      </excludes>
      <headerDefinitions>
       <headerDefinition>${license.header.style}</headerDefinition>
      </headerDefinitions>
     </configuration>
     <executions>
      <execution>
       <id>Verify-Licenses</id>
       <phase>verify</phase>
       <goals>
        <goal>check</goal>
       </goals>
      </execution>
      <execution>
       <id>Add-Licenses</id>
       <phase>generate-sources</phase>
       <goals>
        <goal>format</goal>
       </goals>
      </execution>
     </executions>
    </plugin>
   </plugins>
  </build>
 </profile>


And a Remote Resources plugin:
 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-remote-resources-plugin</artifactId>
  <version>1.3</version>
  <configuration>
   <resourceBundles>
    <resourceBundle>com.nu.art.software:mm-project-licenser:0.0.1</resourceBundle>
   </resourceBundles>
   <attached>false</attached>
  </configuration>
  <executions>
   <execution>
    <phase>generate-sources</phase>
    <goals>
     <goal>process</goal>
    </goals>
   </execution>
  </executions>
 </plugin>

I don't think that there is more to it... if you understand a bit Maven you should be fine together with the links supplied. 

No comments:

Post a Comment