apache ant - cse.iitm.ac.inmanas/docs/apache-ant.pdf · using ant properties 4 targets: init: to...

15
Apache ANT CS3310, Language Translators By Manas Thakur

Upload: others

Post on 23-Jul-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Apache ANT

CS3310, Language Translators

By Manas Thakur

Page 2: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

ANT???

Page 3: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

What is Apache ANT?

“Another Neat Tool”

Automates the build process

Written for Java

Open-source

Uses XML

Page 4: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

But I was happy using make files!

Why should I use ANT? Do u remember, why Java

was conceived?

Page 5: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Using ANT

Download the OS-specific contents from

ant.apache.org, and setup the PATH (complete

details are given there)

Create a build file(contents are explained next)

◦ build.xml is the default name

Run the command ant

◦ If your build file is abc.xml

Run the command ant –f abc.xml

Page 6: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Hello, World!

build.xml <project default="hello">

<target name="hello">

<echo message="Hello, World!"/>

</target>

</project>

$ ant Buildfile: build.xml

hello: [echo] Hello, World

BUILD SUCCESSFUL

Page 7: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Multiple Targets

<project default="hello">

<target name="hello">

<echo message="Hello, World"/>

</target>

<target name="goodbye">

<echo message="Goodbye, Cruel World"/>

</target>

</project>

$ ant goodbye

Buildfile: multitargets.xml

goodbye: [echo] Goodbye, Cruel World

BUILD SUCCESSFUL

Page 8: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Specifying Dependencies

<project default="hello">

<target name="hello">

<echo message="Hello, World"/>

</target>

<target name="goodbye">

<echo message="Goodbye, Cruel World"/>

</target>

<target name="all" depends="hello, goodbye" />

</project>

$ ant all Buildfile: build.xml

hello: [echo] Hello, World

goodbye: [echo] Goodbye, Cruel World

all:

BUILD SUCCESSFUL

Page 9: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Compile, create jar, and execute a

Java source: all in one go!

Hello.java

public class Hello {

public static void main(String[] args) {

System.out.println(“Hello, World!”);

}

}

Suppose this file is in current directory (represented by “.”)

Page 10: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Compile, create jar, and execute a

Java source: all in one go!

hello.xml

<project default="compile">

<target name="compile">

<javac srcdir="." />

</target> <target name="jar" depends="compile"> <jar destfile=“Hello.jar" basedir="." includes="**/*.class" />

</target> <target name="run" depends="jar">

<java classname=“Hello" fork="true"> <classpath path=“Hello.jar" /> </java>

</target>

</project>

Page 11: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Compile, create jar, and execute a

Java source: all in one go!

$ ant –f hello.xml run

compile: jar: run: [java] Hello World BUILD SUCCESSFUL

Page 12: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

So, that’s it???

Hold on man! There’s

much more you can do

with ANT.

This tool really looks

POWERFUL!

Page 13: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Using ANT Properties

<project default="all">

<property name="obj-dir" location="obj" />

<property name="lib-dir" location="lib" />

<target name="init">

<mkdir dir="${obj-dir}" />

<mkdir dir="${lib-dir}" />

</target>

<target name="clean-init">

<delete dir="${obj-dir}" />

<delete dir="${lib-dir}" />

</target>

<target name="all" depends="init"/>

<target name="clean" depends="clean-init"/>

</project>

Page 14: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Using ANT Properties

4 targets:

◦ init: to build the work directory structure

◦ clean-init: to remove the work directory structure

◦ all: the build target that depends on init

◦ clean: to clean target that depends on clean-init

2 properties:

◦ obj-dir: the root directory for our .class files

◦ lib-dir: the root directory for our .jar files

Page 15: Apache ANT - cse.iitm.ac.inmanas/docs/apache-ant.pdf · Using ANT Properties 4 targets: init: to build the work directory structure clean-init: to remove the work directory structure

Final Code Demonstration in Eclipse

(plus a surprise!!)

What a Combination!