Saturday, 26 April 2014

Stashing changes in Git

Guys, this is a post discussing the simple concept of "stashing" in Git. Let's say you are working on some changes in Git. However, before committing your changes, you are asked to work on something else urgently. Git gives you the ability to immediately shift your focus and keep your changes in a "stash", which can be committed later.

Let's try a simple example :

Assuming you have git installed and a repository to work on.

Go to the Git repository and create a new file : vi " Test.rtf"

Now, add the file to the staging list by using the command

git add "Test.rtf"

Note that now, the Test.rtf file is added to the staging list and is not yet committed. Lets say you don't want to commit your changes as yet. So you can stash your changes temporarily by using the following command.

git stash 

You will see a message like this :

Saved working directory and index state WIP on master: 4bed021 Added Test.rtf file
HEAD is now at 4bed021 Added Test.rtf file

You can view the stash list by using the following command :

git stash list

You'll see something like :

stash@{0}: WIP on master: 4bed021 Added Test.rtf file

Suppose you make multiple changes and stash un-committed changes, you can see the multiple changes when you run the command "git stash list"

Eg : 

stash@{0}: WIP on master: 385553f added 2 new files
stash@{1}: WIP on master: 4bed021 Added Test.rtf file

Sometime later when you are ready to commit your changes, all you have to do is pop the stash list

Type "git stash pop". This will pop the first element from the list i.e stash@{0} and make it ready to be committed.

In other words, stash is like an array stack with multiple elements (i.e changes) in it. When you pop the stash, the last element will be popped first (similar to LIFO - Last In First Out function).

You can then commit these changes using

git commit . -m "Adding pre-stashed changes"

So all your -ex stashed changes are now committed. Stashing can come in very handy if you have to work on something else before committing your changes. This is one of the strengths of Git versioning.

Invoke Ant build from Maven

The jar created by the Ant build can be then attached to the project using the Build Helper Maven plugin.

Consider the following example snippet of the pom.xml

<plugins>
   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-antrun-plugin</artifactId>
         <version>1.7</version>
            <executions>
              <execution>
                <tasks>
                      Place any Ant task here
                </tasks>
             </execution>
           <executions>
   </plugin>
 </plugins>

If required, you can then attach the jar created by the Ant build to this Maven project using another plugin named "build-helper-maven-plugin".

This plugin has various goals like build-helper:add-resource and build-helper:attach-artifact

Sunday, 20 April 2014

MySQL JDBC Connector on Mac OSX

If you're working on any Java based program which uses MySQL as it's back-end database, you might find this article extremely helpful.

In order to pull data from the MySQL database, it is first important to establish a connection with it so that the script can talk with it.

The connection can be established by using the MySQL JDBC driver. Now, this driver needs to be separately downloaded. Here's the download link btw. If you're working on Mac OSX, please select the platform-independent version of the Connector from the drop down.

Once downloaded, untar the tar (or unzip the zip file). Copy the mysql-connector-x.x-bin.jar to /Library/Java/Extensions.

If you're using Eclipse IDE for editing class files, you need to make some changes in Eclipse's preferences. So go to Eclipse -> Preferences -> Java -> User Libraries. Click 'New' to create a new user library named as "MYSQL_CONNECTOR_LIBRARY" for instance. Now, click "Add External JARs..." button and browse to the downloaded jar : mysql-connector-x.x-bin.jar.

So if you're Java application is trying to connect to MySQL database, the following code snippet might come in handy for quick reference.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;


try {

connection=DriverManager.getConnection("jdbc:mysql://localhost:3306", "username", "password");

}

Tuesday, 8 April 2014

Check if a file exists using Java

I have long thought about including some content about Core Java concepts on my blog. So here is one of the first ones in the series, which covers some basic Java fundamentals. I assume you have some knowledge about using the Eclipse IDE for project creation. I also assume that you have some basic idea about the Java language (theoretical concepts like OOPS, Inheritance, Data abstraction etc). Don't worry if you don't know much because I plan to cover a few posts on those basics later.

At this point, this post here is all about writing a simple script in Java for checking if a specific file exists at a certain location. You may use the following example for quick reference.

Briefly speaking, we would be importing the java.io.File class and use one of it's methods - isFile.

Please note that the best preferred function to check if a files exists is exists(). In this example, we see how isFile() can also be used to check the same.

Here we go - Create a new class file in Eclipse named as "CheckFileExists_Example". If you maybe aware, the class name and filename has to be the same in Java.

package my.ironcladzone.com;

import java.io.IOException;
import java.io.File;

public class CheckFileExists_Example {

    public static void main(String[] args) {
            
            File f = new File("/usr/tmp/DocuTest.txt");
           
            if (f.isFile()) {
               
                System.out.println("File exists \n");
               
            } else {
       
                System.out.println("File does not exist \n");
            }
     }

}

Here, the method isFile is used to check if "DocuTest.txt" is a file or not. It returns a boolean value - true or false, depending on whethere it actually is a file or not. It will return false if the absolute path mentioned by us, turns out to be a directory.

Play around with code and check for yourself. Edit the path as just :

File f = new File("/usr/tmp/");

Now, the isFile function returns false since the path mentions the '/usr/tmp' location, which is a directory and not a file. So it will directly jump to the else block and print our "File does not exist" statement.

Try one more scenario - specify an imaginary filename in the path. Let's say for example xyz123.txt.

File f = new File("/usr/tmp/xyz123.txt");

It will return false if a filename xyz123.txt does not exist at a specific location.

The point here is how isFile() function can also be used, instead of traditional exists() function for checking if a specific filename exists. It serves a same-to-similar purpose.

I'd cover some more basic topics on I/O in Java i.e reading from files, writing to files in some future posts. So, stay tuned folks. In the meantime, do post your comments for any suggestions & tips. Ciao.

Ant Mail Task

Ant provides a very useful Mail task which can be used in the build.xml to send email notifications.

Eg : 

<target name="mail-upload-notification">
<mail from="build_notification_email_group@company.com"
          tolist="build_receipient_list@company.com"
          subject="${war-file} has been uploaded to the server"
          message="The ${war-file} was uploaded to the ${ftp-server}" />
</target>

Detailed information about this task can be read here.

Tuesday, 1 April 2014

Validate Fonts on Mac OS X

Occasionally or infact very rarely, fonts might get corrupt on Mac OS X. Mac OS X offers the option to validate the authenticity of fonts so that they are safe to use.

Open Fontbook

Click "All Fonts" from the Collection bar on left. Now select and highlight all the installed fonts using shift key (from top to bottom).

From the File menu -> choose Validate Fonts

You'll see the results of the output, something like below. This is a useful procedure to check the font authenticity, if you download & install some third party fonts from the Internet. Just an extra step of caution to make sure that nothing is corrupted. Hope it helps. Ciao.


Saturday, 29 March 2014

Quaker Oats Uttapam - The Indian Pizza

Uttapam is a traditional south Indian dish which closely resembles the pizza. Typically, Uttapam is prepared using urad dal and rice. We twist it's recipe a bit, to create a much healthier and tastier Uttapam using Quaker Oats.

If you want to create the traditional way, the urad dal and rice batter has to be created and stored overnight. However, using Quaker Oats, you can create Uttapam instantly.

Oats as you know is rich in fibre and protein. Thus the Quaker Oats adds much more power and punch to this classic dish.

The ingredients list :

Quaker Oats.
Gram Flour (a.k.a Besan).
Chopped Onions.
Sliced Tomatos.
Cilantro (a.k.a Coriander).
Amul Yellow Butter.
Salt & Pepper.

Mix the Quaker Oats and Besan in sensible proportions with water in a bowl, till it haves a semi-thick batter like consistency.

Heat a pan / skillet and add some Amul yellow butter.

Now spread the batter on the skillet and top it with chopped onions, sliced tomatoes & cilantro.

Sprinkle some salt and black pepper on top. Cook the Uttapam on both sides till the Uttapam gets fully cooked.

Server hot with some Amul butter on top. Wow it tastes absolutely delicious and leaves you extremely filling.

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker