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!
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker