Total Pageviews

Tuesday, February 12, 2013

Adding custom design tag using Macrodef in ANT



Below we will learn how to use custom design ANT tag,

Macrodef is one of the most powerful ways of reusing Ant code.  Macrodefs allow us to define a “private method” with “parameters”, called attributes. Most uses of "ant calls"can be replaced by macrodefs. Macrodefs allow easier code flow than trying to specify “depends” which increase faster execution of build script.

       

** Note-  "propertyregex " performs regular expression operations on an input string, and sets the results to a property.

Input- The input String to be processed.

Global-Should a replacement operation be performed on the entire string, rather than just the first occurrence .

Benefit of using Macrodef.

Suppose as a user I have to define property java.compile.rversion and I passed compiler value as 1.5 but by mistake a space or tab character comes before the compiler value. If we will use the same it will fail while compiling but if we use "triminputproperty" and pass the compiler value into this it will auto remove the space before and after the passed value string and we can use the correct value.

property name=" java.compile.version" value="  1.5" ( Input with space character) (Removed angle bracket as they are not supported by blogspot)

  (now using trim if you check the value it will 1.5 without any space)
triminputproperty property="java.compiler.trimmed" input="${java.compile.version}"  

Now using echo we can verify the output.
echo message="${java.compiler.trimmed}" 

Cheers !


Tuesday, February 5, 2013

Batch file which can run on two or more Java/Jre version

Suppose you have a requirement to install a product/application on different type of hardware some of them uses Java/Jre5 and rest of them uses Java/Jre6 and we needed a single batch or sh file which can identify version according to hardware and install the application into the machine.

Here you go, this batch file will help you to implement these kind of requirements.




@echo off
REM Author @Kapil Pant
set JAVA_VERSION=1.6
set JAVA_VERSION2=1.4
if "%JAVA_HOME%" == "" goto javahomenotfound

REM Find Java executable

if not exist "%JAVA_HOME%\bin\java.exe" goto setjre
set JAVAEXE=%JAVA_HOME%\bin\java.exe
goto setjava
:setjre

if not exist "%JAVA_HOME%\jre\bin\java.exe" goto javahomenotfound
set JAVAEXE=%JAVA_HOME%\jre\bin\java.exe
:setjava

REM Verify correct Java version is being used
"%JAVA_HOME%\bin\java" -version 2>&1  | findstr /C:"%JAVA_VERSION1%"
If %ERRORLEVEL% EQU 0 goto goodversion
"%JAVA_HOME%\bin\java" -version 2>&1  | findstr /C:"%JAVA_VERSION2%"
If %ERRORLEVEL% NEQ 0 goto badjavaversion
:goodversion
set PATH=%JAVA_HOME%\bin;%PATH%
goto EOF

:javahomenotfound
echo You must set JAVA_HOME to a JRE or JDK
goto EOF

:badjavaversion
echo
echo Required Java %JAVA_VERSION% not found under %JAVA_HOME%
goto EOF
:EOF