Apache Ant/Getting Started/Hello World

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Hello World in Ant

Create a directory "My Project". Use a text editor such as Kate, Gedit or Notepad to create a file called build.xml within directory "My Project":

   <?xml version="1.0"?>
   <project name="My Project" default="hello">
       <target name="hello">
          <echo>Hello World!</echo>
       </target>
   </project>

The first line in the file tells the system that this is an XML file:

  <?xml version="1.0"?>

The next line tells ant that we have a project named "My Project" and that it has a default target called "hello":

  <project name="My Project" default="hello">

Note that the file must always start with a <project> and end with a </project>.

The central three lines are the one and only target in the file. They give the target a name and the target has just one task called "echo"

      <target name="hello">
         <echo>Hello World!</echo>
      </target>

You can now open a shell and cd to the "My Project" directory you created and type "ant"

The default task is a required attribute of the project. Each target must have a name.

[edit] Output of Hello World

  Buildfile: build.xml
  
  hello: 
     [echo] Hello World!
  
  Build Successful
  Total time 0 seconds

[edit] Variations

Try changing the echo line to be the following:

  <echo message="Hello There!"></echo>

What is the result? Try the following also:

<echo message="Hello There!"/>

Next Section