Informatika Mihelac

Articles tagged with automating

March 26 2006 automating 3 comments

Speeding web development with Apache Ant

Modern web development includes many iterations. Bigger project have more iterations and as project is nearing completion, these iterations endure for shorter time and are performed more often. With every new iteration I go thought similar process – for example: backup test database and copy it to local server, update database structure, update program code, fix bugs, preform tests, validate XHTML, deploy all changes to test server for client to see. Apache Ant can help, automating repetitive tasks and reducing errors caused by human factor.

Apache Ant is a open source Java-based tool for automating software build processes. It is normally run from command line. Support for Ant exists in almost every Java IDE’s and there are also some GUI software for working with Ant projects. I prefer Antelope, which can be used as standalone GUI or plugin for JEdit.

Ant’s buildfiles are written in XML. Each buildfile contains one project and at least one (default) target. You might have a target for creating database backup, for example, and a target for deploying program code to test server using FTP protocol. A target can depend on other targets, so you can deploy local database to test server only if test database has been backuped.

Let’s look example buildfile that have following tasks:

  • backup local database
  • backup remote database
  • update test database (but take care that it is backuped first)

<project name="MyProject" default="BackupLocalDatabase">
  <property name="backup_directory" value="d:/backup2/MyProject"/>

  <property name="local_database_host" value="localhost"/>
  <property name="local_database_username" value="root"/>
  <property name="local_database_password" value=""/>
  <property name="local_database_name" value="my_database"/>

  <property name="remote_database_host" value="localhost"/>
  <property name="remote_database_name" value="my_database_remote"/>
  <property name="remote_database_username" value="root"/>
  <property name="remote_database_password" value=""/>

  <target name="init">
    <tstamp>         
      <format property="TODAY" pattern="yyMMdd-HHmm"/>
    </tstamp>
    <mkdir dir="${backup_directory}" />
  </target>

  <target name="BackupDatabase">
    <exec dir="" executable="c:\mysql\bin\mysqldump.exe" 
     output="${backup_directory}/${output}_${TODAY}.sql">
      <arg line="--host=${host}" />
      <arg line="-u${user}" />
      <arg line="--password=${password}" />
      <arg line="--add-drop-table" />
      <arg line="--complete-insert" />
      <arg line="${database_name}" />
    </exec>
  </target>

  <target name="BackupLocalDatabase" depends="init">
    <antcall target="BackupDatabase">
      <param name="output" value="local"/>
      <param name="host" value="${local_database_host}"/>
      <param name="user" value="${local_database_username}"/>
      <param name="password" value="${local_database_password}"/>
      <param name="database_name" value="${local_database_name}"/>
    </antcall>
  </target>

  <target name="BackupRemoteDatabase" depends="init">
    <antcall target="BackupDatabase">
      <param name="output" value="remote"/>
      <param name="host" value="${remote_database_host}"/>
      <param name="user" value="${remote_database_username}"/>
      <param name="password" value="${remote_database_password}"/>
      <param name="database_name" value="${remote_database_name}"/>
    </antcall>
  </target>

  <target name="UpdateRemoteDatabase" 
         depends="BackupRemoteDatabase,BackupLocalDatabase">
    <exec dir="" executable="c:\mysql\bin\mysql.exe" 
        input="${backup_directory}/local_${TODAY}.sql">
      <arg line="--host=${remote_database_host}" />
      <arg line="-u$${remote_database_username}" />
      <arg line="--password=${remote_database_password}" />
      <arg line="${remote_database_name}" />
    </exec>
  </target>
</project>

In this example buildfile I assumed the database is MySql, Ant is run on Windows. For sake of simplicity remote database is on localhost (in practice, we would use real host, username and password, and tell MySql server to allow connecting from our computer). Please, update directory locations to fit your settings as well. From command prompt, we can now execute our tasks:

ant BackupLocalDatabase
ant UpdateRemoteDatabase

Do you find these article interesting? Did you develop some automation scripts yourself? Would you like to see more examples and predefined tasks, such as: validating well formedness of XHTML documents, compressing files to deploy them faster, change file permissions on server (CHMOD)?

About

I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in Informatika Mihelac.

Contact: bmihelac@mihelac.org

RSS feedSubscribe to RSS Feed