Monday, April 2, 2012

Avoiding long classpath issue in Ant

Recently i faced a hard time to fix the long classpath issue which occurred in windows, when we use Ant as our building tool.. I looked for various options in the net but none of them worked..

My initial build script was like this;
 <target name="init">
        <mkdir dir="${class.dir}"/>
        <mkdir dir="./../../work/temp/sampleClient"/>
        <path id="javac.classpath">
            <pathelement path="${class.dir}"/>
              <fileset dir="../../repository/components/plugins">        
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="../../lib">
                <include name="*.jar"/>
            </fileset>                
        </path>
</target>


<target name="compile" depends="init" description="Compile all Java"> 
        <javac srcdir="src" destdir="${class.dir}" includeantruntime="false" >         
            <classpath refid="javac.classpath"/>         
        </javac>     
</target>
According to following Ant guide[1], Manifestclasspath, which did the trick to solve the windows long classpath  issue



I corrected my script as follows using Manifestclasspath..Here the pathing.jar which contains a single manifest file where all classpaths are defined. In manifest file, to get references for the actual paths, we define another path which contains all the jars locations explicitly defined. Then we can refer the pathing.jar inside our path element, which should be pointed as "javac.classpath's" classpath reference.
Eg:
<path id="jar.classpath">
            <pathelement path="${class.dir}"/>
            <pathelement path="../../lib"/>         
            <fileset dir="../../repository/components/plugins">            
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="../../lib">
                <include name="*.jar"/>
            </fileset>        
  </path>

<target name="init">
        <mkdir dir="${class.dir}"/>
        <mkdir dir="./../../work/temp/sampleClient"/>
        <manifestclasspath property="tem.classpath" jarfile="pathing.jar">
            <classpath refid="jar.classpath"/>
        </manifestclasspath>      
        <jar destfile="pathing.jar" basedir="target\classes">
            <manifest>            
                <attribute name="Class-Path" value="${tem.classpath}"/>
            </manifest>
        </jar>
        <path id="javac.classpath">
            <pathelement path="${class.dir}"/>
            <pathelement path="pathing.jar"/>          
        </path>
 </target>

 <target name="compile" depends="init" description="Compile all Java">
        <javac srcdir="src" destdir="${class.dir}" includeantruntime="false">
            <classpath refid="javac.classpath"/>
        </javac>
  </target>

[1]http://ant.apache.org/manual/Tasks/manifestclasspath.html

Friday, February 10, 2012

Implementing incremental logic in XSLT

I faced difficult time to implement the incremental logic for xslt 1.0 today..My requirement was to printout consequent numbers upto a certain value.
Here is my sample segment xslt code for this..

  <xsl:element name="Numbers">
                              <xsl:for-each select="//flight_info">
                              <xsl:if test="position()= last()">
                                  <xsl:call-template name="incrementValue">
                                      <xsl:with-param name="i"  select="1"/>                                                                    
                                     <xsl:with-param name="pos" select="position()"/>
                                </xsl:call-template>
                                </xsl:if>                                     
                             </xsl:for-each>
     </xsl:element>


//This template does the trick, which contains the recursive call to the same template..

    <xsl:template name="incrementValue">
        <xsl:param name="i" />
        <xsl:param name="pos" />

        <xsl:if test="$i <= $pos">
            <xsl:value-of select="concat($i,' ')" />
            <xsl:call-template name="incrementValue">
                <xsl:with-param name="i" select="$i + 1" />
                <xsl:with-param name="pos" select="$pos" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>