I am Computer Engineer by profession, passionate about technological advances in software technology, enthusiast learner. Also loves to read literature of historical & social importance.
Validation is often a basic requirement for any kind of application, be it user input over web page, desktop application, mobile application or be it schematic constraints at database level or compliance of XML with respect to XSD.
Validations are implemented in several ways, at various levels in technology stack from User interface to database schema. We may restrict that “User Name” field must not be empty on login form, we may restrict that “userName” column not be empty in database table, we may restrict that in XSD that input xml tag “UserName” shouldnt be empty. Apart from this several frameworks provide their own ready to use configurable mechanisms, struts provides Validator interceptors, Hibernate provide its own annnoation over bean properties corresponding to reflect database schema.
Java bean validation can be just another approach, for several reasons. Because It is easy, because POJO java beans are used in almost all frameworks so portability ensured, or because it reduces boilerplate codes which need not be written, or because it can be looked upon as alternative to avoid to database schema, XSD xml schema or User Interface, to give performance improvement choice.
Java bean validation needs Java8 or higher version. This specification is defined by JSR380. There can be several implementations for this specification.
Above link provides detailed developers documentation about all capabilities of ant build. It can give detailed documentation about syntax, procedures, what are all in built tasks available, how to create customized tasks, how to configure Ant with various development IDEs etc.
In previous tutorial, we have set several paths hardcoded in buildConfig configuration file. However Ant provides capability to either declare those values as properties in configuration file, load values from resource file or pass resource key-value pairs from command line. We will use same buildConfig file to modify it.
1.Declare Values as Properties in configuration file:
We have cleanLogs target in buildConfig file, which is receiving directory path to delete files within. Lets declare that path as property value in beginning of path, like below
Target can be seen as module of tasks in ant configuration file. We can request ant to execute specific target, by passing command line input. Each target may have one or more tasks. Following is sample buildConfig.xml
Exec as Target : please refer target named “stopOracle” having exec as task tag inside. It contains executable command to stop oracle service. if we pass this as target it will stop oracle databse service named “OracleServiceORCLDB”. Similarly we have “startOracle” to start the oracle database instance service.
Dependent Target : Target “deleteLogs” is having dependency on “stopOracle” target. Hence if we execute “deleteLogs” target, it will first invoke “stopOracle”, then its own tasks are executed.
Multiple Tasks : If we want two or more tasks to be executed together as single module, multiple tasks can be mentioned in single target. Target “compileSource” contains the two tasks compile .java files and also create the jar of compiled classes.
Following is HelloWorld configuration file “buildConfig.xml”. File can have any name. Open command prompt and copy file in current directory.
<?xml version="1.0"?>
<project name="AntDemo" default="hello">
<target name="hello">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
</project>
Execute following Ant command
>ant -buildfile buildConfig.xml hello
As seen in command, we are invoking ant.bat having two command line parameters -buildFile and target to execute in the build file.
In the buildConfig.xml target “hello” is mentioned and invoked. Target “hello” consists of single in-built task echo to print the input message in config file.
It prints output on console by default. Each target is executed & printed on console. Here “hello:” target is executed & printed on console its output that is echo message. In the end it says “BUILD SUCCESSFUL” with return code 0.
Ant has several optional input parameters, that can be seen using following command
Check that compatible java version is already installed on local machine & environment variable JAVA_HOME is already set. Following commands can help to check that.
Unzip the Ant downloaded distribution package
Set environment variable ANT_HOME pointing to base directory of unzipped ant distribution.
Add new entry in PATH environment variable, to add path of ant bin directory.
To check if ant is successfully installed, open command prompt and type command “ant -version”, it should show installed version of ant.
Ant is open source build framework provided by Apache Foundation, based on Java. At can help us to automate day to day tasks mainly for software build procedure. It can also serve as developer tool to automate day to day mundane tasks like start-stop services, delete logs, update database, codebase etc.
Ant takes XML configuration file as input build File. Build file contains the tags based instructions on what & how to perform certain tasks.
Build file may mention more than one target definitions. Each target may have one or more tasks to perform. Target can be passed as another input to Ant script, to instruct which target to perform. Target may have dependency on some other targets. In such case, dependency targets are executed first, followed by tasks in current target.
Each target may have one or more tasks to perform like copy files, delete files, compile, execute etc. Tasks are in built tag definitions of Ant. Ant provides rich set of inbuilt tasks viz copy, delete, echo, exec etc. Those in built tasks can be used passing attributes as their inputs. Also customized ant tasks can also be developed.
XML based build file may be viewed as static configuration. For dynamic configurations, input values can be passed through command line arguments or resource files.
In general, Function is a block of code which can be called multiple times from any point in program. Function may receive input and it may return output after processing.
Function with No input and no output returned
::To demonstrate the functions
@echo off
::call to function
call :helloworld
::return after function call
exit /B %ERRORLEVEL%
::function definition starts
:helloworld
echo Hello World
pause
exit /B 0
::function definition ends
Output
2. Function that receives input but returns no output
::To demonstrate the functions
@echo off
::call to function with 2 parameters
call :WishMessage Peter Indiana
exit /B %ERRORLEVEL%
::Function definition starts
:WishMessage
::%~n is used to refer input parameters
echo Hello Good Morning %~1 %~2
pause
exit /B 0
::Function definition ends
output
3. Function that receives input and returns some output
::To demonstrate the functions
@echo off
call :WishMessage Peter Indiana value1
echo today is %value1%
exit /B %ERRORLEVEL%
:WishMessage
echo Hello Good Morning %~1 %~2
set value1=Monday
pause
exit /B 0
::This program is used to demonstrate the return statement
::Every comamnd or batch file returns an integer to indicate result of program
:: if returned code is XERO, in general it is agreed as succesful execution
:: in case of error it supposed to return NON-ZERO integer, to indicate error specific
@echo off
:: ERRORLEVEL is in-built variable which is popped up with return code from last executed command or program
ping foo.com
if %ERRORLEVEL%==0 (echo foo ping was successful) else (echo foo ping was having issue)
ping google.com
if %ERRORLEVEL%==0 (echo google ping was successful) else (echo google ping was having issue)
echo "program will exit with 0 return code, after you press enter"
pause
::at time of return, program should return with error code
exit 0