David R. Heffelfinger
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;
}
}
}
}
Posted at 10:48AM Feb 13, 2010 by David R. Heffelfinger in Groovy | Comments[4]
Groovy Script to SFTP files using AntBuilder and Grapes
Recently I needed to write a Groovy script to SFTP a file to a server. With a little bit of googling, I came to the conclusion that the easiest way to do that was to use AntBuilder ANT's scp task.
Since this script is going to be used by several people, I wanted to use groovy Grapes to pull the dependency automatically.
Turns out that AntBuilder doesn't see dependencies pulled with the Grab annotation, after a little bit of some more googling I found the solution. Instead of using the @Grab annotation, use the static grab() method of the Grape class to pull the dependencies, this allows us to specify that we need the rootLoader as the class loader, which AntBuilder can see.
Without further ado, here is the script in it's entirety for your copying and pasting pleasure:
#!/usr/bin/env groovy
import groovy.grape.Grape;
Grape.grab(group:"ant", module:"ant-jsch", version:"1.6.5", classLoader:this.class.classLoader.rootLoader)
Grape.grab(group:"com.jcraft", module:"jsch", version:"0.1.42", classLoader:this.class.classLoader.rootLoader)
def ant = new AntBuilder();
def pw;
print "password: ";
//the following line does not work under Cygwin, System.console() returns null
pw = System.console().readPassword();
def pwStr = new String(pw);
ant.scp(trust:'true',file:"/path/to/file.txt",
todir:"user@host:/home/user",
password:"${pwStr}",verbose:"true");
Posted at 10:33PM Feb 02, 2010 by David R. Heffelfinger in Groovy |
Groovy Script to zip a directory excluding certain files and subdirectories
Like I mentioned in my last post, I've been reading about Groovy lately. I'm finding Groovy very useful to automate small tedious tasks that I have to do in a regular basis.
For example, many times I have to zip up a directory, but there are some files or subdirectories I want to leave out. What I usually did was I would zip up the whole directory, then manually delete whatever I didn't want in the zip file. Alternatively, I would try to do it from the Linux command line, but I invariably would forget the syntax to do it, this would happen so often that I wrote a blog post explaining how to do it primarily so that I could refer to it myself.
ANT has a zip core task that can easily exclude files or directories from the resulting zip file, through Groovy's AntBuilder class, it is trivial to write Groovy scripts to invoke ANT tasks. It didn't take long to put two and two together, and write a simple Groovy script that would invoke the ANT zip task, specifying any unwanted files or directories.
Without further ado, here's the script:
#!/usr/bin/env groovy
def excludes="**/*.swp, somefile.txt, somedir/**"
def ant = new AntBuilder()
println "args = ${args}"
if (args.length > 0) {
if (args[0].endsWith("/") || args[0].endsWith("\\")) {
args[0] = args[0].substring(0, args[0].length() -1)
}
ant.zip(baseDir:args[0], destFile:args[0] + ".zip", excludes:excludes,
update:true)
}
else {
println "usage: ZipDir.groovy [dirName]"
}
Simply edit the value of the excludes variable to contain any valid comma-separated ANT patterns matching the files or directories you want to exclude from the zip file. Enjoy.
Posted at 01:01PM Aug 01, 2009 by David R. Heffelfinger in Groovy |
Groovy Script to Find and Replace Text
Lately I've been reading a bit about Groovy .Today I had to do a search and replace across several files, and decided to put my newly found Groovy knowledge to use. I wrote a simple script to search all files with a given extension, and search and replace a String inside each file.
I know this can be accomplished as a one liner, but I don't want to try and remember the syntax every time I need to do it. Also, there may be a "groovier" way to do it, being primarily a Java developer I don't really know (and quite frankly, I don't care that much, it works). Without further ado, here is the script for your copying and pasting pleasure:
#!/usr/bin/env groovy
def currentDir = new File(".");
def backupFile;
def fileText;
//Replace the contents of the list below with the
//extensions to search for
def exts = [".txt", ".foo"]
//Replace the value of srcExp to a String or regular expression
//to search for.
def srcExp = "dummy"
//Replace the value of replaceText with the value new value to
//replace srcExp
def replaceText = "awesome"
currentDir.eachFileRecurse(
{file ->
for (ext in exts){
if (file.name.endsWith(ext)) {
fileText = file.text;
backupFile = new File(file.path + ".bak");
backupFile.write(fileText);
fileText = fileText.replaceAll(srcExp, replaceText)
file.write(fileText);
}
}
}
)
I could have made it a bit "fancier", taking command line parameters and what not, but this is just a simple script I solved to have a simple immediate problem I was having. With a couple of simple changes it can be adapted to search different file types, search and replace strings. Enjoy.
Posted at 07:18PM Jul 30, 2009 by David R. Heffelfinger in Groovy |




