Create a SQLite database during an ant build

I have a SQLite database that is used by my application.  It is not the main database but provides some supporting data for the application.  Every time I deploy my application to my local tomcat installation using ant, I want to recreate this database, using a sql script.  I do this by executing a sqlite .read command as one step in my build.

  1. Install sqlite and ensure it is in your path.
  2. Update my Eclipse Ant installation to include the Ant Contrib jars.
    1. I unpacked the jars to my eclipse/plugins/org.apache.ant.XXX/lib directory.
    2. In Eclipse->Window->Preferences->Ant, go to the Runtime section and add the ant-contrib.jar to the classpath.
  3. Within my ant build file I did the following:

<project name="myproject" basedir="." default="clean-deploy">

<taskdef resource="net/sf/antcontrib/"/>

   <property file=""/>
   <property environment="env"/>

   <!-- assumes you have sqlite3 installed on the local machine and in the path -->
   <target name="createsqlitedb" description="build sqlite db">
      <propertyregex property="loc" input="${env.CATALINA_HOME}" regexp="\\" replace="\\\\\\\\\\\\\\\\" />
      <property name="myapp.web.dir" value="${loc}/webapps/${property.projectname}"/>
      <property name="tomcat.conf.dir" value="${loc}/conf"/>

      <echo message="src file for sqlitedb: ${myapp.web.dir}${property.sqlitedb.src}"/>
      <echo message="dest db for sqlitedb: ${tomcat.conf.dir}${property.sqlitedb.dest}" />
      <exec executable = "sqlite3">
          <arg value="${tomcat.conf.dir}${property.sqlitedb.dest}" />
          <arg value=".read ${myapp.web.dir}${property.sqlitedb.src}"/>


The file has the properties in it such as property.sqlitedb.src and property.sqlitedb.dest.  The file itself has standard sqlite sql statements in it.

Helpful links:

Update 8/2/12 – I had to use the ant-contrib jars because I needed the propertyregex functionality. This can be avoided by defining the properties differently. If the properties are defined to have the full path, such as:

Then the ant task can look like this:

<exec executable="${property.propertydb.exe}">
	<arg value="${property.propertydb.loc}"/>
	<arg value=".read ${property.propertydb.sql}${property.sqlitedb.src}"/>

Of course properties can be set up however it makes sense. The main point being, if using full paths, no need for propertyregex.


Separating test dir in Eclipse

I’ve never worked in an environment that totally works within the IDE. Instead of running Tomcat from w/in Eclipse as you can find in tutorials, I run a script that copies my webapp to the correct directories in my tomcat install and then I start/stop Tomcat from within Eclipse so I can debug. For a new app I am trying to get the unit testing in there from the start, but I don’t want the test classes or test properties deployed to my server. I was able to do this by changing the output directory of the testdir to a separate directory, for example bin_test, instead of the default bin directory that Eclipse is using for src.

Hiding files in Eclipse

How to hide the .* files in the navigation pane

1.Click on the “Navigation” pane.
2.Press [CTRL]+[F10] or click on the “View Menu” icon (which looking like an upside down triangle.)
3.Click on the “Filters…” option.
4.In the Navigation Filters, find the “.*” option and click in the checkbox just to the left of the label.
5.Click “OK”.

Found this here:

Using Bookmarks in Eclipse

I love being able to use bookmarks when I’m coding to quickly move between places in the code. Yes, there are all sort of navigational aids in IDEs these days, but nothing is faster than using some key commands to set a bookmark and get back to it. Must be a holdover from my vi days. Since then it has been downhill. Well, Visual Studio was good. IntelliJ okay, but Eclipse is horrible. I just want to quickly tag a place in my code, not name it. Well, a little search lead me to a great write up about Eclipse and it’s bookmark functionality by Luis de la Rosa at Eclipse Tip: Use Bookmarks to track important places in your code. But, even more helpful was the link to the plugin in the comments at Eclipse Bookmarks Plugin which adds quick bookmarking and navigating. It’s a little old, but it still seems to work. I did have to update the key mappings some, but the basic functionality seems to be working.