How to patch open source software
As a developer, I very often use open source software, but sometimes you run into a situation where the binary (that’s that package you just download and stick in your classpath) needs to be patched. Maybe it’s an obscure bug fix that you need that hasn’t been released yet, or maybe it’s a feature that’s still on the trunk (that’s the “main” stream of code).
Very often when you read about these situations, the author will just say, “apply this patch” or “this has been fixed in this Jira”, which is great, but unless you work with a team or on a major project, it’s not something you do every day.
Fortunately, it’s not as difficult as it would seem to be. Here’s how you do it.
In my case, I ran into a situation where I needed to apply a patch to Apache Nutch to add parameter support to its integration with Solr. Now, this is a Java project, but I suspect the steps are pretty much the same (at least concept-wise) for other languages. So here are the steps:
1) Download the source. Typically, you do this in the same location where you got the binaries. In my case, I downloaded http://www.alliedquotes.com/mirrors/apache/nutch/apache-nutch-1.4-src.zip.
2) Uncompress the source to an accessible directory.
3) Download the relevant patch. In this case, it was https://issues.apache.org/jira/secure/attachment/12505095/NUTCH-1213.diff. You’ll want it in the same directory as the
4) If you’re on Windows, you’re going to need Cygwin; make sure that you install “patch”, which is under “Utils”.
5) Run the patch like so:
patch < NUTCH-1213.diff
In my case, the utility wasn't finding the source files, so I had to type the location, but that's (usually) not a huge deal.
6) At this point you have patched the source, but you still need to create the binaries you'll need to actually run the software. This may sound intimidating, but it's not. Most open source software includes some easy way to build it because the developers need to do that all the time. In the case of Java projects it's usually in the form of a configuration file for Maven (
pom.xml) or Ant (
Nutch has both, and I already have Ant on my system, so the next step is to make sure that
ANT_HOME are set to their proper locations (use your actual locations):
export JAVA_HOME=/cygdrive/c/sw/jdk1.7.0_02 export ANT_HOME=/cygdrive/c/sw/apache-ant-1.8.3
Then you can run the build:
cd <directory with build.xml< $ANT_HOME/bin/ant
You should see a long string of messages that tell you what happened. Hopefully at the end you'll see this message:
If not, take a look at the actual errors and see what's going on; usually it's something simple, like a missing system variable.
If everything works, you're done. You've now got a patched version of that open source software that's so useful!
Not so tough, right?
Now, I confess, I've only done this for Java projects. Anybody have a good resource for other languages?