Copyrights Conundrum

Copyrights Conundrum

Micah Martin
Micah Martin

March 12, 2009

Here’s the situation. You’ve spent days working on an open source project. Your sweat, blood, and tears have been poured into thousands of lines of code spread through countless file in a sprawling tree of directories. The final step is to release your masterpiece into the wild.

So you choose your open source license and then it hits you; you realize that need to add a copyright header to each and every source code file.

copyright-free image

How tedious! You google for a tool that will automatically add the headers for you, and you find nothing satisfactory. In the end, you write your own script to add the headers for you.

I’ve been in this exact situation and written such a script too many times. My most recent experience and script implementation will be the last.

I’ve dubbed this last imeplementation MM Copyrights and you’re welcome to use it.

MM Copyrights is a simple Ruby gem that will search a directory for source code files, inserting or updating a header comment in each file.


It’s just a matter of installing the gem hosted on Github.

sudo gem install mmcopyrights


Let’s say you want to add the following copyright header to all .rb files in the lib directory…

require 'mmcopyrights'
MM::Copyrights.process("lib", "rb", "#-", "©2009 Micah Martin\nAll rights reserved")

And all the ruby files will look like this:

#- ©2009 Micah Martin
#- All rights reserved

... ruby code ...

Typically I keep the copyright text in a separate file and write the following Rake task:

task :copyrights do
		require 'mmcopyrights'
		MM::Copyrights.process('lib', "rb", "#-",'copyrights.txt'))

Or I write the following ant task if need be:

<target name="copyrights">
		<exec command="ruby">
				<arg value="-rrubygems" />
				<arg value="-e" />
				<arg value="require 'mmcopyrights'; MM::Copyrights.process('src', 'java', '//-','copyright.txt'))" />


If you haven’t guessed, MM::Copyrights::process takes 4 parameters.

  1. The path of the directory containing source code.

  2. The extension of source file to be processed.

  3. The comment prefix. Note that there’s a unique prefix the tool will use to find existing copyright headers.

  4. The copyright text, without the comment prefix.

That’s about it. Feel free to run MM::Copyrights::process multiple times on the same code base. It’s harmless. And when the new year rolls around, just change the copyright text and rerun the tool. It will update existing headers.