Skip to Content

Another Neat Tool

Apache Ant is a tool for automating software build processes. It is lightweight, easy to understand and extensible. And best of all, it has a build-in support in eclipse. In this blog post, I want to show you how you can use Eclipse and Ant to create a basic build file for your SAPUI5 apps. You can download the finished build script underneath the post.

Step 1: Setup a project

If you don’t know, how you can create a basic SAPUI5 development environment and project, then check out this post. When you complete step seven, you have done all preparations for adding a build script.

Step 2: Create a build file

At first you should ensure, that you have opened the correct perspective in eclipse (Java EE). After that, right click on your project and click “New – Other…”. Choose XML File as shown in the screenshot.

/wp-content/uploads/2013/07/xml_245507.pngIn the next dialog, choose “build.xml” for file name and save it in the root directory of your project. After that, go to “Window – Show View – Others” and open the Ant view.

/wp-content/uploads/2013/07/ant_view1_245508.png

After that, a new view should appear in your eclipse. I recommend to shift this view to the right side of your editor.

Step 3: The Coding

Open your build.xml in the editor. Then paste this initial lines to your file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<project name="SAPUI5_APP">
          <description>ANT-Script to build SAPUI5-Apps</description>

</project>

As you can see, the build file consists of several XML-tags, which define the project name and the description. When you drag this file from your project explorer view to your ant view, you will see an entry like this.

/wp-content/uploads/2013/07/ant_view2_245509.png

Then we will define some global properties, which contains important information about your application and runtime. As you can see, you can also create an external properties file and refer to it from your script.

<!DOCTYPE project>
<project name="SAPUI5_APP">
          <description>ANT-Script to build SAPUI5-Apps</description>
          <!-- Define the properties used by the build -->
          <property name="app.name" value="SAPUI5_APP" />
          <property name="app.version" value="1.0" />
          <property name="server.home" value="C:/Program Files/apache tomcat/apache-tomcat-7.0.21" />
          <property name="sapui5.home" value="C:/Program Files/sapui5" />
          <property name="build.home" value="${basedir}/build" />
          <property name="dist.home" value="${basedir}/dist" />
          <property name="src.home" value="${basedir}/src" />
          <property name="web.home" value="${basedir}/WebContent" />
          <!-- You can also use an external propterties file -->
          <!-- <property file="build.properties" /> -->
</project>

Now it’s important to replace two values with individual information.

  1. server.home: This property has to refer to your tomcat installation directory.
  2. sapui5.home: This property has to refer to a directory with sapui5 libs. Your best bet would be to create a new directory called “sapui5”, where you store all files from the downloaded evaluation package. In this directory, create a new one called “lib”, where you store all necessary libraries, which will be needed for your build script. You can copy these libs from “tools-updatesite\plugins”. The screenshot shows all required files.

/wp-content/uploads/2013/07/libs_245503.png

Next step is to define the classpath for compiling. All dependent libraries need to be available for Ant, so you don’t get any errors at build time.

<!-- Define the CLASSPATH -->
          <path id="compile.classpath">
                    <!-- Servlet-API -->
                    <fileset dir="${server.home}/lib">
                              <include name="*.jar" />
                    </fileset>
                    <!-- SAPUI5 -->
                    <fileset dir="${sapui5.home}/lib">
                              <include name="*.jar" />
                    </fileset>
                    <!-- Additional Libs -->
                    <fileset dir="${web.home}/WEB-INF/lib">
                              <include name="*.jar" />
                    </fileset>
          </path>

After that you can create your Ant targets. A target is a container of tasks that cooperate to reach a desired state during the build process. Put another way, a target is a collection of actions that Ant must take to build that target. Look at the following examples.

        <target name="clean" description="Delete old work and dist directories">
                    <delete dir="${build.home}" />
                    <delete dir="${dist.home}" />
          </target>
          <target name="prepare" depends="clean" description="Create working directories">
                    <mkdir dir="${dist.home}" />
                    <mkdir dir="${build.home}/classes" />
          </target>
          <target name="compile" depends="prepare" description="Compile Java sources">
                    <javac srcdir="${src.home}" destdir="${build.home}/classes" includeAntRuntime="false">
                              <!-- Reference to the defined CLASSPATH -->
                              <classpath refid="compile.classpath" />
                    </javac>
          </target>

You can see, that there are dependencies between targets. So when you run “compile”, first “clean” then “prepare” and at last “compile” is executed. When you save your file now, you will see in the “Ant-View” these targets, which can be started via double-click. To deploy your application to a server, you will need a war-file. With the next target, we are going to create this file by Ant.

         <target name="dist" depends="compile" description="Create WAR file for binary distribution">
                  <war destfile="${dist.home}/${app.name}-${app.version}.war" webxml="${web.home}/WEB-INF/web.xml">
                            <!-- Copy files from web.home-->
                            <fileset dir="${web.home}" />
                            <!-- Copy all necessary libs from the following directories into WEB-INF/lib -->
                            <lib dir="${sapui5.home}/lib" excludes="*branding*,*osgi*" />
                            <lib dir="${web.home}/WEB-INF/lib" />
                            <!-- Copy classes from working directory into WEB-INF/classes-->
                            <classes dir="${build.home}/classes" />
                  </war>
          </target>

When you run this target, you should see an console output like this. Now you can deploy the output war file to your server.

/wp-content/uploads/2013/07/console_out_245502.png

Some more Information

  1. You can do a lot of more things with Ant, e.g. creating Java-Doc, running JUnit tests or deploying your application to a server (have a look at this post for Apache Tomcat). Here is an overview of all Apache Ant tasks.
  2. You can trigger Ant targets from console, too. To do this, you have to install the full Ant library from this site (the built-in support in eclipse can’t handle this). Here is link to the official manual.
  3. Besides, you can use Ant as a part of continuous integration.
  4. Apache Ivy is an optional dependency manager for Ant. Very useful, when you have a lot of Java libs in your project.
  5. I can recommend Apache Maven or Gradle as popular alternatives to Ant. It depends on your team, infrastructure, skills and project scope, which build tool you should choose. Read this article, to get a better understanding of the differences: Ant, Maven and Gradle – A Side by Side Comparison.
To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply