David R. HeffelfingerSoftware Development Noteshttp://mail.ensode.net/roller/dheffelfinger/feed/entries/atom2022-05-31T09:53:37+00:00Apache Roller (incubating)http://mail.ensode.net/roller/dheffelfinger/entry/groovy_script_to_find_javaGroovy Script to find Java classes in JAR filesDavid R. Heffelfinger2010-02-13T15:48:10+00:002010-02-13T15:48:43+00:00
<p><style type="text/css"><br/>
<!--<br/>
body {color: #000000; background-color: #ffffff; font-family: Monospaced}<br/>
pre {color: #000000; background-color: #ffffff; font-family: Monospaced}<br/>
table {color: #000000; background-color: #e9e8e2; font-family: Monospaced}<br/>
.comment {color: #969696}<br/>
.QUOTED_STRING_LITERAL {color: #ce7b00}<br/>
.keyword {color: #0000e6}<br/>
--><br/>
</style></p>
<p><p><br/>
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. </p><p>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.</p><p>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.</p><p>Without further ado, here is the script. Enjoy <br /></p></p>
<p><pre><span class="comment">#!/usr/bin/env groovy</span><br/>
<span class="keyword">def</span> cDir = <span class="keyword">new</span> File(<span class="QUOTED_STRING_LITERAL">"."</span>);<br /><span class="keyword">def</span> jarContents;<br />cDir.eachFileRecurse{file -><br /> <span class="keyword">if</span> (file.name =~ <span class="QUOTED_STRING_LITERAL">/.*\.jar$/</span>)<br /> {<br /> jarContents = <span class="QUOTED_STRING_LITERAL">"</span><span class="QUOTED_STRING_LITERAL">jar tvf $</span>{file}<span class="QUOTED_STRING_LITERAL">"</span>.execute().text;<br /> jarContents.eachLine{line -><br /> <span class="keyword">if</span> (line.contains(args[0])){<br /> print <span class="QUOTED_STRING_LITERAL">"*** found in $</span>{file.canonicalPath}<span class="QUOTED_STRING_LITERAL">:"</span>;<br /> println line;<br /> }<br /> }<br /> }<br />}<br /></pre></p>
http://mail.ensode.net/roller/dheffelfinger/entry/groovy_script_to_sftp_scpGroovy Script to SFTP files using AntBuilder and GrapesDavid R. Heffelfinger2010-02-03T03:33:05+00:002010-02-04T01:37:33+00:00
<p><style type="text/css"><br/>
<!--<br/>
body {color: #000000; background-color: #ffffff; font-family: Monospaced}<br/>
pre {color: #000000; background-color: #ffffff; font-family: Monospaced}<br/>
table {color: #000000; background-color: #e9e8e2; font-family: Monospaced}<br/>
.comment {color: #969696}<br/>
.QUOTED_STRING_LITERAL {color: #ce7b00}<br/>
.keyword {color: #0000e6}<br/>
--><br/>
</style></p>
<p><p>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.</p><p>Since this script is going to be used by several people, I wanted to use groovy Grapes to pull the dependency automatically.</p><p>Turns out that AntBuilder doesn't see dependencies pulled with the Grab annotation, after a little bit of some more googling I found <a href="http://stackoverflow.com/questions/1641116/groovy-with-grape-and-antbuilder-classloader-problem">the solution</a>. 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.</p><p>Without further ado, here is the script in it's entirety for your copying and pasting pleasure:<br /></p><pre><span class="comment">#!/usr/bin/env groovy</span><br /><span class="keyword">import</span> groovy.grape.Grape;<span class="keyword"><br /><br /></span>Grape.grab(group:<span class="QUOTED_STRING_LITERAL">"ant"</span>, module:<span class="QUOTED_STRING_LITERAL">"ant-jsch"</span>, version:<span class="QUOTED_STRING_LITERAL">"1.6.5"</span>, classLoader:<span class="keyword">this</span>.<span class="keyword">clas</span><span class="keyword">s</span>.classLoader.rootLoader)<br />Grape.grab(group:<span class="QUOTED_STRING_LITERAL">"com.jcraft"</span>, module:<span class="QUOTED_STRING_LITERAL">"jsch"</span>, version:<span class="QUOTED_STRING_LITERAL">"0.1.42"</span>, classLoader:<span class="keyword">this</span>.<span class="keyword">class</span>.classLoader.rootLoader)<br /><br /><span class="keyword">def</span> ant = <span class="keyword">new</span> AntBuilder();<br /><span class="keyword">def</span> pw;<br /><br />print <span class="QUOTED_STRING_LITERAL">"password: "</span>;<br /><span class="comment">//the following line does not work under Cygwin, System.console() returns null</span><br />pw = System.console().readPassword();<br /><span class="keyword">def</span> pwStr = <span class="keyword">new</span> String(pw);<br />ant.scp(trust:<span class="QUOTED_STRING_LITERAL">'true'</span>,file:<span class="QUOTED_STRING_LITERAL">"/path/to/file.txt"</span>,<br /> todir:<span class="QUOTED_STRING_LITERAL">"user@host:/home/user"</span>,<br /> password:<span class="QUOTED_STRING_LITERAL">"$</span>{pwStr}<span class="QUOTED_STRING_LITERAL">"</span>,verbose:<span class="QUOTED_STRING_LITERAL">"true"</span>);<br /><br /><br /><br /><br /></pre></p>
http://mail.ensode.net/roller/dheffelfinger/entry/groovy_script_to_zip_aGroovy Script to zip a directory excluding certain files and subdirectoriesDavid R. Heffelfinger2009-08-01T17:01:00+00:002009-08-02T13:44:37+00:00
<p><p>Like I mentioned in <a href="http://www.ensode.net/roller/dheffelfinger/entry/groovy_script_to_find_and">my last post</a>, 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.</p><p>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 <a href="http://www.ensode.net/roller/dheffelfinger/entry/excluding_directories_from_zip_files">blog post</a> explaining how to do it primarily so that I could refer to it myself.<br /></p><p> <a href="http://ant.apache.org/">ANT</a> 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.</p><p>Without further ado, here's the script:</p><br/>
<style type="text/css"><!-- .comment {color: #969696} .QUOTED_STRING_LITERAL {color: #ce7b00} .keyword {color: #0000e6} --><br/>
</style><br/>
<pre><span class="comment">#!/usr/bin/env groovy</span><br/>
<span class="keyword">def</span> excludes=<span class="QUOTED_STRING_LITERAL">"**/*.swp, somefile.txt, somedir/**"</span><br/>
<span class="keyword">def</span> ant = <span class="keyword">new</span> AntBuilder()<br /><br />println <span class="QUOTED_STRING_LITERAL">"args = $</span>{args}<span class="QUOTED_STRING_LITERAL">"</span><br/>
<span class="keyword">if</span> (args.length > 0) {<br /> <span class="keyword">if</span> (args[0].endsWith(<span class="QUOTED_STRING_LITERAL">"/"</span>) || args[0].endsWith(<span class="QUOTED_STRING_LITERAL">"\\"</span>)) {<br /> args[0] = args[0].substring(0, args[0].length() -1)<br /> }<br /> ant.zip(baseDir:args[0], destFile:args[0] + <span class="QUOTED_STRING_LITERAL">".zip"</span>, excludes:excludes,<br /> update:<span class="keyword">true</span>)<br />}<br /><span class="keyword">else</span> {<br /> println <span class="QUOTED_STRING_LITERAL">"usage: ZipDir.groovy [dirName]"</span><br/>
}<br/>
</pre><br/>
<p>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.<br/>
</p></p>
http://mail.ensode.net/roller/dheffelfinger/entry/groovy_script_to_find_andGroovy Script to Find and Replace TextDavid R. Heffelfinger2009-07-30T23:18:50+00:002009-08-01T16:45:23+00:00
<p><style type="text/css"><!--.comment {color: #969696} .QUOTED_STRING_LITERAL {color: #ce7b00}.keyword {color: #0000e6}--></style><br/>
<p>Lately I've been reading a bit about <a href="http://groovy.codehaus.org/">Groovy</a> .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.<br /></p><p>I know this can be accomplished as a <a title="Groovy One Liners" href="http://groovy.codehaus.org/Groovy+CLI">one liner</a>, 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:</p><p> </p></p>
<p><pre><span class="comment">#!/usr/bin/env groovy</span><br /><br /><span class="keyword">def</span> currentDir = <span class="keyword">new</span> File(<span class="QUOTED_STRING_LITERAL">"."</span>);<br /><br /><span class="keyword">def</span> backupFile;<br /><span class="keyword">def</span> fileText;<br /><br /><span class="comment">//Replace the contents of the list below with the </span><br /><span class="comment">//extensions </span><span class="comment">to search for</span><br /><span class="keyword">def</span> exts = [<span class="QUOTED_STRING_LITERAL">".txt"</span>, <span class="QUOTED_STRING_LITERAL">".foo"</span>]<br /><br /><span class="comment">//Replace the value of srcExp to a String or regular expression</span><br /><span class="comment">//to search for.</span><br /><span class="keyword">def</span> srcExp = <span class="QUOTED_STRING_LITERAL">"dumm</span><span class="QUOTED_STRING_LITERAL">y"</span><br /><br /><span class="comment">//Replace the value of replaceText with the value new value to </span><br /><span class="comment">//replace srcExp</span><br /><span class="keyword">def</span> replaceText = <span class="QUOTED_STRING_LITERAL">"awesome"</span><br /><br />currentDir.eachFileRecurse(<br /> {file -><br /> <span class="keyword">for</span> (ext <span class="keyword">in</span> exts){<br /> <span class="keyword">if</span> (file.name.endsWith(ext)) {<br /> fileText = file.text;<br /> backupFile = <span class="keyword">new</span> File(file.path + <span class="QUOTED_STRING_LITERAL">".bak"</span>);<br /> backupFile.write(fileText);<br /> fileText = fileText.replaceAll(srcExp, replaceText)<br /> file.write(fileText);<br /> }<br /> }<br /> }<br />)<br /><br /><br /><br /></pre></p>
<p><p>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.<br /></p></p>