Monday, January 12, 2009

Ant Build Script for War File Creation

This build script will create two separate war files out of a single project. Useful when building a project that will requires to create multiple war file for deployment (i.e. maintenance page and front page).

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

<project default="main" basedir=".">

<property name="project.name" value="PROJECT_NAME"/>
<property name="base.dir" value="."/>

<property name="web.dir" value="${base.dir}\WebContent"/>
<property name="lib.dir" value="${web.dir}\WEB-INF\lib"/>
<property name="build.dir" value="${base.dir}\build"/>

<property name="dest.dir" value="DESTINATION"/>
<property name="out.internal.war" value="${dest.dir}\internal\${project.name}.war"/>
<property name="out.external.war" value="${dest.dir}\external\${project.name}.war"/>

<!-- Cleanup task -->

<target name="cleanup">
<echo message="Deletion of previously created war files:"></echo>
<delete file="${out.internal.war}" />
<delete file="${out.external.war}" />
</target>

<!-- This task will create the directory for external -->

<target name="external" depends="cleanup">
<echo message="Creating the temp directory for external pages."></echo>
<mkdir dir="${base.dir}\temp_external"/>
<copy todir="${base.dir}\temp_external">
<fileset dir="${web.dir}">
<exclude name="**/web.xml"/>
<include name="**/*.*"/><!-- files that needs to be included in external page -->
</fileset>
</copy>
</target>

<!-- This task will create the directory for internal -->

<target name="internal" depends="external">
<echo message="Creating the temp directory for internal pages."></echo>
<mkdir dir="${base.dir}\temp_internal"/>
<copy todir="${base.dir}\temp_internal">
<fileset dir="${web.dir}">
<exclude name="**/web.xml"/>
<include name="**/*.*"/><!-- files that needs to be included in internal page -->
</fileset>
</copy>
</target>

<target name="generate" depends="internal">
<echo message="Creating the internal war file"></echo>
<war destfile="${out.internal.war}" webxml="${web.dir}\WEB-INF\web.xml">
<fileset dir="${base.dir}\temp_internal"/>
<lib dir="${lib.dir}"/>
<classes dir="${build.dir}\classes"/>
</war>

<echo message="Creating the external war file"></echo>

<war destfile="${out.external.war}" webxml="${web.dir}\WEB-INF\web.xml">
<fileset dir="${base.dir}\temp_external"/>
<lib dir="${lib.dir}"/>
<classes dir="${build.dir}\classes"/>
</war>
</target>

<target name="main" depends="generate">
<echo message="End of ant build task..."></echo>
<delete dir="${base.dir}\temp_internal" />
<delete dir="${base.dir}\temp_external" />
</target>

</project>


Actually this is my first time creating ant build script so feel free to give comments on how can I improve or optimize this script.

Thursday, January 8, 2009

Javascript Auto-Ellipse Technique

This tutorial will help you add ellipsis(...) from your text and make sure that you can maximize the space provided by the DOM element.

Actually, I follow the tutorial from http://blog.paranoidferret.com/?p=15 but with some minor modification to meet my needs.


@param element - DOM Element placeholder of the text
@param text - the text for truncation
@param width - desired width
@return text - the truncated text with ellipsis

function autoEllipseText(element, text, width){

var inSpan = document.createElement('span');

inSpan.innerHTML = text;
inSpan.style.whiteSpace = 'nowrap';

$(document.body).append(inSpan);

if(inSpan.offsetWidth > width)
{
var i = 1;
inSpan.innerHTML = '';
var len = text.length;
do{
inSpan.innerHTML = text.substr(0, i) + '...';
}while(inSpan.offsetWidth < width && ++i < len);

text = inSpan.innerHTML;
}
$(inSpan).remove();
return text;
}


The code snippet from http://blog.paranoidferret.com/?p=15 does not work when the DOM element is not yet existing or written in the DOM itself. So the workaround I did was to temporarily write the DOM element in the document's body, apply ellipsis on the text, remove the DOM element, and finally, return the text.

Hope you find it useful. :)