Saturday, August 24, 2013

Computing path to maven module base directory

Having spent a bunch of time writing functional tests for maven projects with many modules, I tend to end up needing access to the project base path (typically the path with the pom.xml file in it).  This isn't much of a problem when running a single module - new File(".") pretty much does it.  On the other hand when attempting to access modules outside of the jvm base path, things start to get tricky.

There's a couple ways to skin this cat - probably one of the more portable ones is a utility method that accepts a Class and a path and returns a File by checking the supplied Class' classloader:


        public static File getPathInModule(Class<?> classInModule,String ... items){
                String tcPath = classInModule.getProtectionDomain().getCodeSource().getLocation().getFile();
                String[] tcPathParts = StringUtils.split(tcPath,File.separator);
                StringBuffer pomPath = new StringBuffer();
                for(String part:tcPathParts){
                        pomPath.append(File.separator);
                        if("target".equals(part)){
                                break;
                        }
                        pomPath.append(part);
                }
                pomPath.append(StringUtils.join(items,File.separator));
                return new File(pomPath.toString());
        }
This works fairly well as long as there is a target directory present, which is for my purposes - always. It could be modified to look for a pom.xml file instead though.

Wednesday, August 21, 2013

Hadoop 3.0.0 maven repository

Recent maven hadoop artifacts are pretty hard to come by.  The central repository contains stable and 2.0.0-alpha releases, CDH repositories have 2.0.X releases, and the apache repository has 3.X snapshots.  That means if you want 2.3.X or 3.X functionality, you're either stuck pulling in snapshots or building/hosting your own releases.

The apache snapshots are problematic because individual build snapshots don't appear to be retained for very long.  Building/hosting your own artifacts seems like a pain.  Hopefully, this is a temporary issue until hadoop 3.X is released.  Until then, here's a maven repository that has (unofficial) 3.0.0 release artifacts with source jars.

It is pretty easy to put in pretty much any hadoop version into the repository.  Visit the project page or shoot me a email to request one!