Monday, June 30, 2008

Skip Tests in Maven [Tip]

If your tests are failing and you can't be bothered to fix them, this handy tip will show you how you can skip tests and build your Maven project straightaway!

Simply add the property -Dmaven.test.skip=true to your Maven command. Here is an example:

mvn -Dmaven.test.skip=true install

The compiler will not compile the test sources and will not run your JUnit tests:

[INFO] [compiler:testCompile]
[INFO] Not compiling test sources
[INFO] [surefire:test]
[INFO] Tests are skipped.

But of course, your test classes should always run, right?

Reference:
http://maven.apache.org/general.html

Friday, June 27, 2008

Read a File Without Trimming Leading Whitespace [Shell Scripting]

This is how you would normally read a file, line-by-line, in a shell script:
file=/home/fahd/dummy
while read line
do
    echo "$line"
done < $file
The problem with this is that the "read" command automatically removes leading whitespace from each line and also concatenates a line ending with a backslash with the one following. This means that you cannot properly process a file with lots of leading whitespace e.g. xml files.

The trick around this, is to redefine your IFS (Internal Field Separator) variable. By default, IFS is set to the space, tab and newline characters to delimit words for the read command. This is how you can amend your script:

file=/home/fahd/dummy
OIFS=$IFS
IFS=
while read -r line
do
    echo "$line"
done < $file
IFS=$OIFS
First save the current value of IFS into a temporary variable called OIFS. Then blank out IFS. When you have finished reading your file, set IFS back to its original value of OIFS.

We also provide the read command with the -r flag, so that it treats each backslash to be part of the input line and does not concatenate it with the next line.

Tuesday, June 24, 2008

Shutting Down Java Apps [Howto]

In most cases, users do not always follow the recommended procedure to exit their java applications. They may not type "quit" or "exit" and may instead hit Ctrl+C to terminate the process immediately. In these cases, the application does not get a chance to clean-up.

Fortunately, there is a way to ask the JVM to execute some clean-up code before exiting. This is done by using ShutdownHooks which are registered with System Runtime. The JVM starts all registered shutdown hooks concurrently when shutting down.

Here are the steps involved when creating shutdown hooks:

1. Create the shutdown hook class:

public class AppShutdownHook extends Thread{
    public void run(){
        logger.info("Running shutdown hook...") ;
        //cleanup e.g. close database connections
    }
}
2. Register the shutdown hook:
public class App{
    public App(){
        Runtime.getRuntime().
          addShutdownHook(new AppShutdownHook()) ;
    }
}
Note that no guarantee can be made about whether or not any shutdown hooks will be run if the JVM aborts with the SIGKILL signal (kill -9) on Unix or the TerminateProcess call on MS Windows.

However, SIGTERM (kill with no arguments) will work.

Reference:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)

Monday, June 23, 2008

Moving House

I'm back at work today after having taken time off since Wednesday to move house. I spent Wednesday packing boxes and got the CHAPS transfer and mail redirection (£35.40 for a year) done. Keys were exchanged at 1pm on Thursday, and it took two Luton vans + three removal men (£270) to carry our stuff to the new house. We were all done by 4pm. We've unpacked our important items but are still mostly surrounded by a sea of boxes! Its good to be back at work though!

Here is a list of useful bookmarks if you're moving house: http://del.icio.us/fahdshariff/moving%2Bhouse

Tuesday, June 17, 2008

Firefox 3 Released!

Firefox 3 is here! Download it now and help set a Guinness World Record for the most downloads in 24 hours!

You even get a certificate!