David R. Heffelfinger

  Ensode Technology, LLC

 
Bookmark and Share

Groovy Script to find Java classes in JAR files



We've all been there at some point or another. We just inherited a big pile of legacy Java code that we need to maintain. This code likely is using some big convoluted ANT script to be built. This being legacy code, it does not use a dependency manager like Ivy or the one included with Maven.

We, of course, want to be able to build using our favorite Java IDE, be it Eclipse, NetBeans or IntelliJ IDEA. Our Java IDE, of course, has no idea of what our project dependencies are, therefore our code is riddled with squiggly red lines due to missing dependencies all over. We are going to manually add dependencies to our project, oh joy. In many cases, these dependencies are scattered across multiple directories, we may have to inspect the contents of most of our JAR files to find many of our dependencies, especially those built in-house.

I was recently in this situation myself, to ease my pain, I came up with a Groovy script that recursively inspects every JAR file in the current directory and every subdirectory. It takes a single parameter, the name of the class to look for (case sensitive), then recursively checks every JAR file in the current directory all subdirectories. It's only requirement is that the "jar" executable be in the PATH, which should be the case for most Java programmers anyway.

Without further ado, here is the script. Enjoy

#!/usr/bin/env groovy
def cDir = new File(".");
def jarContents;
cDir.eachFileRecurse{file ->
if (file.name =~ /.*\.jar$/)
{
jarContents = "jar tvf ${file}".execute().text;
jarContents.eachLine{line ->
if (line.contains(args[0])){
print "*** found in ${file.canonicalPath}:";
println line;
}
}
}
}

 
 
 
 
Comments:

You're currently using the current directory as the "root" of your search. Wouldn't it be better to pass that as an argument, and then the search could be done on a custom folder?

Posted by Geo on February 15, 2010 at 04:10 AM EST #

Geo,

I originally implemented the script the way you are suggesting. However I found myself forgetting to pass the directory name as an argument all too often.

In most cases I was already "standing" at the directory I wanted to search anyway so I decided to drop the directory name argument. My script, by the way, is in my $PATH.

The script is simple enough, it can be easily modified to take the directory name as an argument anyway.

David

Posted by David R. Heffelfinger on February 15, 2010 at 08:50 AM EST #

I don't remember if Groovy has this or not, but in Perl & Ruby, you can do this:

directory ||= "."

and this will assign "." to the directory variable unless it already has a value.

Posted by Geo on February 15, 2010 at 04:43 PM EST #

Would there be any advantage to using Groovy's FileNameFinder class and doing something like the following?

def cDir = ".";
def jarContents;
def jarFiles = new FileNameFinder().getFileNames(cDir, '**/*.jar');
jarFiles.each{file ->
jarContents = "jar tvf ${file}".execute().text;
jarContents.eachLine{line ->
if (line.contains(args[0])){
print "*** found in ${new File(file).canonicalPath}:";
println line;
}
}
}

Posted by 208.50.255.30 on March 09, 2010 at 06:10 AM EST #

Post a Comment:
Comments are closed for this entry.
 

« December 2018
SunMonTueWedThuFriSat
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
     
Today

 
© David R. Heffelfinger