Sunday 28 February 2016

Ant - Build.xml Updates - 28 Feb

Guys, in my earlier posts we learnt how to write a simple basic build.xml and also learnt about the build.properties. Today we'll modify, update our build.xml to include some more functionality.

Let's add the following 2 features :
  • add a MANIFEST.MF file to the jar package which will contain the packaging information
  • create documentation using javadoc.
Check out the updated build.xml with the tasks for the same below :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="generate-docs" basedir=".">

<property name="build.home" value="${basedir}" />
<property file="build.properties"/>

<tstamp>
<format property="timestamp" pattern="dd-MM-yyyy"/>
</tstamp>

<target name="clean">
<delete dir="${build.home}/src" />
<delete dir="${build.home}/bin" />
<delete dir="${build.home}/lib" />
<delete dir="${build.home}/test" />
<delete dir="${build.home}/dist" />
<delete dir="${build.home}/docs" />
<delete dir="${build.home}/logs" />
</target>

<target name="createdir" depends="clean">
<mkdir dir="${build.home}/src" />
<mkdir dir="${build.home}/bin" />
<mkdir dir="${build.home}/lib" />
<mkdir dir="${build.home}/test" />
<mkdir dir="${build.home}/dist" />
<mkdir dir="${build.home}/docs" />
<mkdir dir="${build.home}/logs" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

<target name="copydir" depends="createdir">
<copydir src="${workspace.src}"
dest="${build.home}/src"></copydir>
</target>

<target name="compile" depends="copydir, clean">
<javac classpathref="classpath" includeantruntime="false"
srcdir="${build.home}/src"
destdir="${build.home}/bin"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/${timestamp}-TestJar-${versionnum}.jar"
includes="**/*.class">
<manifest>
<attribute name="Main-class" value="com.ironcladzone.FileSize"/>
</manifest>
</jar>
</target>
<target name="generate-docs" depends="create-jar">
<javadoc sourcepath="${build.home}/${src.dir}" destdir="docs"/>
</target>

</project>

Notice the "create-jar" target where we have added the <manifest></manifest> tags in which we have mentioned the main class for instance. This will create a MANIFEST.MF file within the jar. I'll write a new post on the Manifest file soon.

Also for the javadoc, we have a new target named "create-documentation". If you check the folder structure, you'll find the index.html within the docs folder. Try looking into the index.html, you'll see the class and package details in a sample webpage decorated with the default javadoc css stylesheet.

Recover and restore deleted files from Eclipse Navigator

Have you come across a situation where you accidentally deleted source code from the Eclipse Navigator. Just don't panic in such a case. There's a way to recover and restore these deleted files.

Simply right click the project from the Navigator pane and choose the following option :


In the next screen, you'll see the list of all files that you deleted and wish to restore back. Just choose the necessary files, click Restore and relax!


View Jar contents without opening on Mac OS X

Hello readers, today's topic is a fairly basic interview question wherein you are asked to view the jar package contents without exploding/opening it.

In order to do so, type the following in terminal for example :

unzip -l 28-02-2016-TestJar-1.8.jar 

Output : 

Archive:  28-02-2016-TestJar-1.8.jar
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  02-28-16 13:34   META-INF/
      142  02-28-16 13:33   META-INF/MANIFEST.MF
        0  02-28-16 13:33   com/
        0  02-28-16 13:33   com/ironcladzone/
      571  02-28-16 13:33   com/ironcladzone/Create_Directory.class
      755  02-28-16 13:33   com/ironcladzone/FileSize.class
      367  02-28-16 13:33   com/ironcladzone/HelloICZ.class
      463  02-28-16 13:33   com/ironcladzone/String_mani1.class
      534  02-28-16 13:33   com/ironcladzone/String_manip2.class
      601  02-28-16 13:33   com/ironcladzone/String_manip3.class
      407  02-28-16 13:33   com/ironcladzone/String_manip4.class
      474  02-28-16 13:33   com/ironcladzone/String_manip5.class
      935  02-28-16 13:33   com/ironcladzone/User_Input.class
      201  02-28-16 13:33   com/ironcladzone/callMacApp.class
      909  02-28-16 13:33   com/ironcladzone/operator1.class
 --------                   -------
     6359                   15 files

Note the -l switch is for listing the jar contents. You can use the same technique for any kind of package - jar / war / tar / ear etc.

Saturday 27 February 2016

Automate tasks using Expect scripts on Mac OS X

Expect is one of the hidden gems and quite esoteric scripting language for automating tasks on Mac OS X and on most unix flavors.

The idea behind expect is you can program the response to a programmed request string. It's much like a pre-programmed auto-complete feature, in which you have already told the system what the response should be, if it comes across a specific expected request.

Let's try out a basic simple script. In terminal create a new script file : vi expect_test.exp
Note the .exp extension, similar to .sh of bash scripts.

#!/usr/bin/expect

expect "hello"

send "Welcome to IroncladZone \n"

Now if you execute the script ./expect_test.exp and type the string "hello", it will automatically send the response "Welcome to IroncladZone".

Thursday 25 February 2016

Ant - Delete only files from directories

For an Ant build, if you just want to delete files without deleting the directory, consider the following. For illustration, we'll take the folder structure as mentioned in our earlier post.

Let's say I want to only clear the class files generated in the bin/com/ironcladzone directory.

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="clean-class-files" basedir=".">

<property name="build.home" value="${basedir}" />
<property file="build.properties"/>

<target name="clean-class-files">
<delete>
<fileset dir="${bin.dir}">
<include name="**/*"/>
</fileset>
</delete>
</target>


</project>

Note : by using **/*, it will recursively delete all the files from the bin/ subdirectories. Using the <fileset> tag, we mention the directory tree, on which the delete operation needs to be performed.

List only target names from build.xml in Apache Ant

In Apache Ant, for listing only the target names defined in the build.xml, all we have to do is type the following from command line :

Eg :

ant -p build.xml

Output :

Buildfile: /Users/ironcladzone/Documents/workspace/AntBuildTest/build/build.xml

Main targets:

Other targets:

 clean
 compile
 copydir
 create-jar
 createdir

Default target: create-jar

Wednesday 24 February 2016

Build properties in Ant - An Introduction

Guys, in our previous post we saw a basic build.xml file with some basic target definitions. Note that we mentioned a property or two in it. But have you ever wondered, what happens if you have multiple properties to define? Yea that's when we use a build.properties file.

Let's create a build.properties file and we define all the properties inside it. The advantage of using a properties file is that whenever you want to modify/update the value of any property you only have to do it once in this build.properties. You don't have to look into the build.xml to see when and where you want to modify things. This makes life a lot easier, which otherwise would have been cumbersome.

Ok so I create a new build.properties with the following updated folder structure :


You must have observed that in our older post we included some hardcoded directory paths directly in the build.xml. That's a quite crude way to define the paths and is not advisable. Hardcoded paths should be avoided as much as possible. So what we do is create some properties and define their values equal to the directory paths. Something like this :

versionnum=1.3
workspace.src=/Users/ironcladzone/Documents/workspace/AntBuildTest/src
src.dir=/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src
bin.dir=/Users/ironcladzone/Documents/workspace/AntBuildTest/build/bin

Note that we have also included an additional property named "versionnum" to assign a certain version number to the jar package that we created.

Now we only need to provide a reference to these properties in our build.xml file. Take a look at the new updated build.xml below :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="create-jar" basedir=".">

<property name="build.home" value="${basedir}" />
<property file="build.properties"/>

<target name="clean">
<delete dir="${build.home}/src" />
<delete dir="${build.home}/bin" />
<delete dir="${build.home}/lib" />
<delete dir="${build.home}/test" />
<delete dir="${build.home}/dist" />
</target>

<target name="createdir" depends="clean">
<mkdir dir="${build.home}/src" />
<mkdir dir="${build.home}/bin" />
<mkdir dir="${build.home}/lib" />
<mkdir dir="${build.home}/test" />
<mkdir dir="${build.home}/dist" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
</path>

<target name="copydir" depends="createdir">
<copydir src="${workspace.src}"
dest="${src.dir}"></copydir>
</target>

<target name="compile" depends="copydir, clean">
<javac classpathref="classpath" includeantruntime="false"
srcdir="${src.dir}"
destdir="${bin.dir}"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/TestJar-${versionnum}.jar"
includes="**/*.class"></jar>
</target>

</project>

Don't you think the new build.xml looks much cleaner now? It does! Right? Okay so with today's post I've introduced you to the concept of properties. In the next post, we'll try to refine the script and introduce some new features into it. Keep visiting folks.

BONUS TIP : Note that properties are immutable. So once a build starts, the value assigned to the properties remain constant. However, if am not wrong, we can update the properties at build time using the propertyFile task. Note that this is different than the <property file> tag. Notice there is no space there in the propertyFile task.

Ok guys, signing out for the day. Let's meet up again tomorrow. Till then, ciao!

Tuesday 23 February 2016

Change text Case using shell script

In today's post we cover a simple problem - change the case of text i.e either from lowercase to uppercase or vice versa. Let's look at a few examples.

Let's say I have a file SedText.rtf as follows :

1.Here is a cute little tutorial for
2.trying out different things using
3.the Stream Editor or Sed in Unix.
4.The author of this tutorial is
5.none other than IroncladWriter
6.himself. The tutorial aims to
7.teach folks and educate how
8.they could unlock the power
9.of Unix to the fullest. Am posting
10.this on the blog IronCladZone
11.The best blog on the whole of
12.internet, where you can learn
13.technical stuff as well enjoy 
14.entertaining tidbits like movies,
15.music, television, fashion, food,
16.shopping, travel, trends etc.
17.Just sit back, relax and

18.ENJOY THE RIDE :)

Now for converting the file into all Uppercase from the command line, use the following :

tr a-z A-Z < SedText.rtf 

Output : 

1.HERE IS A CUTE LITTLE TUTORIAL FOR\
2.TRYING OUT DIFFERENT THINGS USING\
3.THE STREAM EDITOR OR SED IN UNIX.\
4.THE AUTHOR OF THIS TUTORIAL IS\
5.NONE OTHER THAN IRONCLADWRITER\
6.HIMSELF. THE TUTORIAL AIMS TO\
7.TEACH FOLKS AND EDUCATE HOW\
8.THEY COULD UNLOCK THE POWER\
9.OF UNIX TO THE FULLEST. AM POSTING\
10.THIS ON THE BLOG IRONCLADZONE\
11.THE BEST BLOG ON THE WHOLE OF\
12.INTERNET, WHERE YOU CAN LEARN\
13.TECHNICAL STUFF AS WELL ENJOY \
14.ENTERTAINING TIDBITS LIKE MOVIES,\
15.MUSIC, TELEVISION, FASHION, FOOD,\
16.SHOPPING, TRAVEL, TRENDS ETC.\
17.JUST SIT BACK, RELAX AND\
18.ENJOY THE RIDE :)


Similarly for converting from uppercase to lowercase,  use the following command :

tr A-Z a-z < SedText.rtf 

Saturday 20 February 2016

Ant - Build.xml example on Mac OS X

In today's post guys, we'll cover the topic of building a very simple Java based project using Apache Ant. We'll learn how to write a basic build.xml file for building it.  Let's first look into the code for build.xml and then we'll breakdown the code with the explanation.

build.xml :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="create-jar" basedir=".">

<property name="build.home" value="${basedir}" />

<target name="clean">
<delete dir="${build.home}/src" />
<delete dir="${build.home}/bin" />
<delete dir="${build.home}/lib" />
<delete dir="${build.home}/test" />
<delete dir="${build.home}/dist" />
</target>

<target name="createdir" depends="clean">
<mkdir dir="${build.home}/src" />
<mkdir dir="${build.home}/bin" />
<mkdir dir="${build.home}/lib" />
<mkdir dir="${build.home}/test" />
<mkdir dir="${build.home}/dist" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
</path>

<target name="copydir" depends="createdir">
<copydir src="/Users/ironcladzone/Documents/workspace/AntBuildTest/src"
dest="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"> </copydir>
</target>

<target name="compile" depends="copydir">
<javac classpathref="classpath" includeantruntime="false"
srcdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"
destdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/bin"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/TestJar.jar"
includes="**/*.class"></jar>
</target>


</project>

Now let's look into the folder structure in the Eclipse Package Explorer :


Now, let's get down to the explanation guys :

1. First of all, AntBuildTest is my project workspace which has the source code in it.

2. Next I created a build directory under which I created the above build.xml file.

3. Now in the builds.xml I have defined various steps/tasks also known as targets viz : clean, createdir, copydir, compile, create-jar. Note that a build.xml will still successfully build even if it doesn't have a single target defined.

4. For every build.xml file, we first have to define the <project></project> tag, in which we mention the project name, basedir and default target to be executed.

5. Basedir is the location of the current root directory in which build.xml exists i.e the absolute path

6. The default target that we define, will be the task/target which will be executed. Now the target can have dependencies on other targets.

7. Note that target can have one or many dependencies on multiple targets.

8. The 'clean' target is the target to delete all sub-directories that we create in the 'createdir' target. So every time we perform a clean build, the directories created in the last build will be deleted.

9. In the 'createdir' target, we make directories namely : src, bin, lib, test, dist.

10. We now want to copy the source code from our local workspace to the 'src 'directory that we create in the 'createdir' target,. So in the 'copydir' target we define the step to copy the source code.

11. Now once we have the source code, we need to compile it right. So we define the 'compile' target wherein we define 'srcdir' i.e the location where we have our java files. Also we mention the 'destdir' as the bin folder i.e this is where we want our .class files to be saved.

12. During compilation, we need to specifically tell the xml where the .class files would be. So we need to define the classpath. 

13. We define the classpath i.e the bin directory which will have the .class files.

<pathelement location="${build.home}/bin"></pathelement>

14. After compilation, we define the 'create-jar' target, in which we create the jar package - TestJar.jar. Make sure not to miss the .jar file extension. Note that we have mentioned the include parameter for including the **/*.class files that need to be included in it.

15. We have also defined the 'destfile' parameter i.e the location where we want the jar file to be saved i.e the 'dist' directory for distribution.

16. Oh and I missed the 'lib' directory, which will basically hold the external jar files required for our project.

17. Well, I hope I covered the simple basic fundamental points surrounding an Ant build.  Let me know if I missed any point guys. If I did, I'll try to cover them in detail in an another upcoming post covering an advanced scenario. Till then, ciao!

Friday 19 February 2016

Arithmetic Operators in Java - Live practical examples : Part 1

In today's post we'll cover the topic of arithmetic operators in Java with some real live examples. In this post, we'll skip the theory and go straightaway for some practical examples. With these examples, you will get to grasp and understand the operator functions much quicker than ever.

Check out the following code in which we have used multiple combinations of arithmetic operators in different positions.

package com.ironcladzone;

public class operator1 {

public static void main(String[] args) {
System.out.println("Example 1 :" + 3 + 4 + 5);
System.out.println("Example 2 : " + 3 + 4 * 5);
System.out.println("Example 3 : " + 3 + 12 * 10);
System.out.println("Example 4 : " + 3 * 4 + 5);
System.out.println("Example 5 : " + 3 * 4 / 5);
System.out.println("Example 6 : " + 5 / 4 * 5);
System.out.println("Example 7 : " + 10 / 4 + 5);
System.out.println("Example 8 : " + 10 + 4 / 5);
System.out.println("Example 9 : " + 10 + 15 / 5);
System.out.println("Example 10 : " + 125 / 10 / 5);
System.out.println("Example 11 : " + 50 / 10 / 5);
System.out.println("Example 12 : " + 23 / 10 / 5);
System.out.println("Example 13 : " + 23 / 10 / 5 + 4);
System.out.println("Example 14 : " + 3 + 4 + 5 / 7);
System.out.println("Example 15 : " + 3 + 4 + 5 * 2);
System.out.println("Example 16 : " + 3 + 5 / 5 * 2);
System.out.println("Example 17 : " + 3 + 22 / 5 * 2);
System.out.println("Example 18 : " + 3 + 7 / 5 * 2);
System.out.println("Example 19 : " + 2 + 8 / 5 * 2);
System.out.println("Example 20 : " + 3 * 9 / 5 + 2.59);

}


}

Output : 

Example 1 :345
Example 2 : 320
Example 3 : 3120
Example 4 : 125
Example 5 : 2
Example 6 : 5
Example 7 : 25
Example 8 : 100
Example 9 : 103
Example 10 : 2
Example 11 : 1
Example 12 : 0
Example 13 : 04
Example 14 : 340
Example 15 : 3410
Example 16 : 32
Example 17 : 38
Example 18 : 32
Example 19 : 22
Example 20 : 52.59

If you observe closely, you may find a few of the combinations are tricky. For instance, note that the + sign we have used, is for string concatenation and not to be confused with the plus sign used for addition.

This was just the first set of live examples. Another exhaustive post on more examples is on its way. Stay tuned folks. Am also quite sure this code will help you cover the basic operator concepts if you're planning to give the Oracle Java Certification 1Z0-803. Good luck guys. Signing out.
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker