Pages

Groovy substring

Can't imagine any terser way of doing this than

println "teststring"[4..6]

Ain't it groovy :)

Netbeans not allowing to create project on windows

At times Netbeans does not allow you to create projects in some directories (on windows). I searched a lot but could not find any reasonable explanation for why windows turns some folders as read only. Anyways, run the following commands on those directories in which Netbeans is not letting you create the project.

Go to the command prompt and, issue
>attrib -r -s c:\some\directory
>attrib -r -s c:\other\directory

This removes the read only status.

Script to enable/disable proxy


To Enable Proxy
EnablePorxy.vbs

set shell = Wscript.CreateObject("Wscript.Shell")
shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"


To disable Proxy
DisablePorxy.vbs

set shell = Wscript.CreateObject("Wscript.Shell")
shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"


Maven: multiple builds from cmd file

This is just a followup on/refinement of my previous post. This cmd/bat Script focuses on clean-build-installing multiple projects from a single script. This script halts at the first build failure. If you happen to run from window explorer by doble clicking the script, In case of error the command window stays open (Press any key to continue). If ever build was successful, the window closes automatically. In the following example, I am skipping the test execution for third project you can add/remove -DskipTests=true to any mvn calls.

REM Uncomment following if you want to clean local repo on each run
REM rmdir /S /Q "%HOMEPATH%\.m2\repository\com\mygroup\id"

call mvn -f ProjectA\pom.xml clean install
if not "%ERRORLEVEL%" == "0" goto error

call mvn -f ProjectB\pom.xml clean install
if not "%ERRORLEVEL%" == "0" goto error

call mvn -DskipTests=true -f ProjectC\pom.xml clean install
if not "%ERRORLEVEL%" == "0" goto error

exit

:error
@echo Build Failed
pause


Hope you find this helpful.

Reference:
This  question at SO helped.

Calling multiple bat files from a bat file

Earlier I had posted about closing command window that is left open after executing a bat file. This post is about calling multiple bat files from a bat file.

Today, while trying to execute multiple maven builds from a bat file, I observed that only first build one was called and once the first build was over, the execution stopped. Next builds were not called at all. I Did some search and found this. Yeah mvn command is a bat file, I knew that. What i missed was putting a call before the mvn commands:

cd ProjectA
call mvn install
cd ..

cd ProjectB
call mvn install
cd ..


The post linked above goes one level further by checking the %ERRORLEVEL% and exiting the script if any any build fails.

So lesson learnt today :
If you need to call multiple bat files from a bat file, start each bat command with a call

Google Code adds Git support

Git finally makes it to the Google's code hosting platform. Git happens to be third Version Controlling System supported on
Google Code, following Subversion and Mercurial.

The announcement states:
Since our original announcement of Mercurial support, Git has grown significantly more popular and user-friendly, and on the technical side, it has added an efficient “smart” HTTP protocol that fits with Google’s HTTP-based infrastructure. (Note that this feature is only available in version 1.6.6 and later.)

This is very welcome addition as Git's popularity is increasing exponentially but I am not sure how many people will actually switch to Google code for Git hosting as more open source git projects are already using trendy and hep Github. Github is more social too. This is certainly going to help those who already have projects hosted on Google Code and want to switch to git.

I personally like mercurial slightly more than git for its simplicity. Git, for novice, is overwhelming. Whatever your choice may be, Distributed Version Controlling Systems have started taking over and are becoming mainstream. If you are still unsure about what it all means, don't worry, you are just git init / hg init away.

Groovy performance : iterating with closure vs loop

Toady while listening to this video I discovered an interesting fact about usage of closure vs regular loop for iterating over list. So I wrote a quick test to confirm and yes loops outperform closures in terms of performance in case of iterating over a list.

But in a slightly similar case of File.eachLine{} vs loading file in a list and then iterating with for, the closure still works out faster on the whole. I have not tested this for different file sizes, but damn, whatever it may be new File(file).eachLine{} just rocks :)

Here is the github gist, to get the latest version of code.

def file = "input.txt"
// just to heat up the jvm :)
new File(file).eachLine{
    it.toUpperCase()
}

// with closure
start = System.nanoTime()
new File(file).eachLine{
    it.toUpperCase()
}
println System.nanoTime() - start

// with loop
start = System.nanoTime()
lines = new File(file).readLines()
for (line in lines){
    line.toUpperCase()
}
println System.nanoTime() - start

// read lines into a list
lines = new File(file).readLines()

// with list.each {}
start = System.nanoTime()
lines.each { line->
    line.toUpperCase()
}
println System.nanoTime() - start

// with loop over list
start = System.nanoTime()
for (line in lines){
    line.toUpperCase()
}
println System.nanoTime() - start

Results (with groovy-1.8.0 and jdk-1.6)
 Run # 1   Run # 2   Run # 3   Run # 4
 4792229   4773791   4840280   5428064
11169576  10254934  10791595  12546008
 2638604   2424610   2477969   2466514
  465702    315403    315403    412063

Display System Properties in Groovy

It can't get any simpler than this.

System.properties.each { k,v->
    println "$k = $v"
}

Groovy rocks!!

Back in the Saddle ... Back in Black

I am back to blogging after a brief ;-) hiatus of 2 years. Don't get mislead by the title of the post. This post has got nothing to do with the two tracks, Aerosmith or ACDC.

It's like a rebirth. Giving a fresh life to a thing I care about. Yesterday I finished reading the fantastic book named "Clutter Busting". It has inspired me lot to clean up the junk, that i dint consider junk so far, from my life. I have started removing many of my precious belonging from my computer attic.

I cant believe its almost been a decade of getting hooked to computers. The Digital Clutter had been gathering for long time that kept me distracted and less focused. You can expect few things coming straight out of my attic making their way to this blog.

Leaking the secret of not posting for so long: I had decided that I will move to a hosted blogging platform (am a control freak, I guess) and own a domain name and only then I'll publish my next post. The sad news is neither of that happened so far and the good news is I have decided to change the way I have programmed myself to think.

By the way today happens to be the day when I started my professional career six years back.

Hello world!