Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Automating the Build Process

using ANT

1
What is build automation?
The build is a process of preparing source code for
production so that users can happily interact with
it.
 It may include compilation
 file compression
 updating database schema
 creating an installer or executable file, etc.

2
Contt..
Build automation is an approach to handling
builds within a CI/CD pipeline that has several
steps.
 A developer commits source code to a repository
 a CI server detects the change, runs the build process
outside of the developer’s IDE (on a dedicated cloud or
in-house machine)
 checks it with unit tests, and either returns for fixes or
sends it further down the pipeline

3
Need for a Build Tool
Software utilities known as “build tools” stream-
line the transformation of source code into de-
ployable forms like executables and libraries.

4
Top 10 Benefits of Build Tools
1. Automation: Build Tools eliminate the re-
quirement for manual involvement & errors by
automating routine activities.
2. Consistent: Build tools guarantee reproducible
outcomes across a wide range of development
settings & platforms by strictly adhering to pre-
scribed build processes.

5
Contt..
3. Dependency Management: It makes it simpler
to include third-party code in the project by
handling requirements on external libraries &
frameworks.
4.Task Parallelism: Many build programs can run
multiple tasks simultaneously, reducing build
times and boosting productivity. It is beneficial
for large projects with many moving parts.

6
Contt..
5. Incremental Builds: Tools for construction allow for
progressive construction, which saves time and mate-
rials. Instead of recompiling the entire application, they
detect changes in the source code and merely recom-
pile the affected sections.
6. CI/CD Integration: Build tools that integrate with
CI/CD pipelines transparently to developers. This helps
to automate the build & deployment procedures fully.
7. Code compilation: is the process by which build
tools transform source code into binary executables or
intermediary representations.
7
Contt..
8. Testing and Quality Assurance:
Automated tests may be run as part of the build
process.
9. Compatible and expandable: These resources
are compatible with numerous languages, frame-
works, and operating systems.
10. Deployment and Packaging: They simplify
transferring software to end-users or different
groups by helping package it into deployable for-
mats.
8
What is ANT?
ANT (Another Neat Tool) is a utility that auto-
mates the process such as compile, run, test
and assemble Java application

It can be run independently of Eclipse, Net-


Beans, or any other Java development envi-
ronment

ANT only depends on the JDK utilities (Java


compiler, JAR utility, etc.)
9
Why ANT?
Typically, large projects need to be built and
tested frequently during the Verification
phase
 Builds are done daily, or even more frequently
 Usually by the people doing the verification
 ANT can completely automate the tasks of
 retrieving code from a Repository
 Completely recompiling all source (.java) files
 JAR’ing rebuilt class (.java) files
 Copying built files to a distribution directory for access
by testers
10
Why ANT?
Dedicated PCs are usually used to perform the
compile and build
 The OS is at a known fixed revision level

 The build PC is kept “unpolluted” except for the


tools needed to build the target application

 The JDK and other tools are at a known fixed re-


vision level

11
Features of Apache Ant
• It is the most complete Java build and de-
ployment tool available.
• It is platform neutral and can handle plat-
form specific properties, such as file sepa-
rators.
• It can be used to perform platform specific
tasks such as modifying the modified time
of a file using 'touch' command.
• Ant scripts are written using plain XML. If
you are already familiar with XML, you can
learn Ant pretty quickly.
12
Features of Apache Ant
• Ant is good at automating complicated
repetitive tasks.
• Ant comes with a big list of predefined
tasks.
• Ant provides an interface to develop cus-
tom tasks.
• Ant can be easily invoked from the com-
mand line and it can integrate with free
and commercial IDEs

13
The idea behind ANT and other
similar build automation tools
ANT is based on build scripts with rules that describe
1. Targets: the end result – usually file(s) that need to be
created as the end “product” There may be one or more
 E.g. a JAR file targets defined
2. The dependencies of the target on other files or targets
 E.g. JAR file depends on .class files (and maybe Javadoc .html
files) that need to be built first There is typically a hierarchy of
 .class files depend on .java files dependencies; the final target
depends on intermediate tar-
 .html files depend on .java files gets
3. The tasks for creating the target(s):
 Use jar.exe utility to create a JAR file from .class files
 Use javac.exe is use to compile .java files into .class files
 Use javadoc.exe to process comments in .java files into .html
files 14
For a standalone ANT engine, you
can Install ANT from www.apache.org

Eclipse comes with its own


ANT “engine”, so you don’t need
to install a standalone version
of ANT unless you want to run
ANT outside of Eclipse

15
Anatomy of a simple ANT
script
Every ANT script starts with an xml statement
<?xml version="1.0"?> similar to this which identifies
this file as containing xml statements
The project element names the Ant project, and optionally
specifies the default target, base directory, etc.
<project name="Ant demo script" default=“printMessage" basedir=".">

Properties are name/value pairs that can be declared for


<property subsequent symbolic
name=“output" access within
value="Hello the Ant script.
SE2030!"/>
Targets contain rules and tasks that the Ant engine executes
in order achieve some goal.
<target name=“printMessage" description="Prints a message.">

<echo message="The message is ${output}"/>


</target>

</project> Echo is a very simple task. See the Ant manual for a list of
other tasks at ant.apache.org/manual in the section “Overview of
Ant Tasks” 16
Creating an ANT script to
compile (build) your project
 Apache ANT is a Java based build tool
from Apache Software Foundation.
 Apache ANT’s build files are written in
XML and they take advantage of being
open standard, portable and easy to
understand.

17
Element of the Build xml
 Project
is a container that wraps targets and tasks into a sin-
gle unit.
The XML element project has three attributes.

18
Contt..
 Target
 A target is a collection of tasks that you want to run as one
unit.
Example. provide an informational message to the user.

 Targets can have dependencies on other targets.

19
Contt..
 Target
 The target element has the following attributes −

20
Contt..
 Tasks
 Task is a piece of code that can be executed.
 The target element has the following attributes −
• Built-in Tasks
• User defined Tasks

21
Built-in Tasks
• Archive Tasks
• Audit Tasks
• Compile Tasks
• Execution Tasks
• File Tasks
• Logging Tasks
• Mail Tasks
 and many more...

22
Contt..
 Archive Tasks
Tasks which are used to compress and uncompress data are know
as archive tasks.

23
Contt..
 Execution Tasks
Tasks which are used to execute of run applications are know as
execution tasks.

24
Contt..
 Example
<project name="java-ant project" default="run">
<target name="run" depends="compile">
<java classname = "com.javatpoint.Hello">
<classpath path="test"></classpath>
</java>
</target>
</project

25
Contt..
 To run the ant build file, open up com-
mand prompt and navigate to the
folder, where the build.xml resides, and
then type ant info.
 You could also type ant instead. Both
will work,because info is the default
target in the build file.

26

You might also like