Tuesday, April 04, 2006

Java from Scratch - part II

A few week back I posted about how long it was since I've built anything with java from scratch. What I want to do in the long run is to produce a DDD oriented projects with hibernate+spring. Bear in mind the last time I used java was with EJBs and JBuilder 8.

So after reading about the myriad of different options that are in the Java world I decided not to try to configure everything for myself and instead rely on a "template" project that would help me. I went with AppFuse to kick start the project since it comes with a mixture of framework decisions you can dream of, from hibernate, IBATIS, Spring and others to JSP, Tapestry and JSF for presentation with included security, transaction support and even AJAX..

So here are the dummy proof steps I followed:

Getting started: Java, Ant, Tomcat and MySQL

The first thing is to download and install the JDK 5.0 in your local computer. For windows there is a simple installer that you can get from Sun's Java site. Try and find something like "Download JDK 5.0 Update XX". After that you should add the JAVA_HOME environment to you system and JAVA_HOME\bin to your path. To do so in XP press Win + Brake, go to Advanced -> Environment Variables and add a new "System Variable" and enter JAVA_HOME and the directory of your java. In my case it looks like this: "C:\Program Files\java\jdk1.5.0_06". Afterwards, find the PATH variables and at add at the end a %JAVA_HOME%\bin entry.

Another thing to install is Tomcat and Ant which I will not get into to much detail. For Tomcat you have the option of a windows installer (I grabbed my 5.5 version here) and for ant you should simply download the latest binaries and uncompress them to the desired folder. After that you should repeat the same procedure as with the JAVA_HOME environment and PATH addition but with CATALINA_HOME and ANT_HOME, the former pointing the Tomcat home installation and latter to ant's.

Finally you need to download MySQL database. Although version 5 is available I downloaded version 4.1 since there seems to be more support so far. A good idea is to also download the Administrator and Query Browser.

Eclipse

Then we need a IDE for easier code writing, in this case eclipse. Eclipse is actually more than just an IDE, but a platform in itself. This is why you will not find a simple download for developing in Java. If you head to the download section you will see smacked in the middle of the screen the more "usual" download: the latest stable download for the eclipse SDK. This download includes the tools necessary to build plugins and expand eclipse, that since are written in Java, serves as an excellent Java IDE (actually the java tools are called Java development tools (JDT)).

Fortunate for me there was a new production release of what is known as the Web Tools Platform (WTP) a few weeks ago. This project aims helping the creation of J2EE and web projects and is bundled with several subproject and goodies that might prove useful in the future. So try and grab the latest version of the WTP and uncompress it to a folder and lunch eclipse.exe.

After you start eclipse, the first thing you should do is to set add the JDK as an installed JRE environment. For this, in eclipse, go to Windows -> Preferences. There, find the Java / Installed JRE entry in the left side tree and click on "Add". In the dialog presented enter any name you feel comfortable with (something like JDKJRE1.5.xx) and point to the same directory as the JAVA_HOME environment set earlier. Why eclipse is not smart enough to add this on its own? My guess is because it searches the Windows registry.

Most of the steps above are similar to what you can find here, but I found so much information on the page confusing for some reason.

AppFuse

As I said before, AppFuse is nice "template" project that help you get started on a new project. It uses Ant to its work, and what you end up having is a project with the name you assign to it in its own directory structure, ready to be built and deployed to a server. The standard tools AppFuse uses is ant and java, Tomcat as a web container and MySQL as its database. That is why with set up those things at the beginning. Also, the default architecture consists of hibernate+spring+struts with other assorted goodies. There are instructions at AppFuse wiki on how to configure these things differently but I haven't tried them yet. I’ll probably delve into this later since I want to use Tapestry instead of Struts.

Obviously the first thing to do is download the latest version and extract it to any directory. Then, with you command prompt pointing in the AppFuse directory type:

ant new

If you set everything alright it will be ask 1) the name of the application, 2) the database name and 3) the name of the packages. This will generate a directory with your project name that is ready to be built, tested and deployed.

Since AppFuse knows nothing about how do you wish to connect to your database you should supply these values by modifying build.properties file at the root of the new application directory. There, just unremark the lines with

database.admin.password=myrootpassword

After that, navigate to the new project directory and type

ant setup


This should build the DB, configure tomcat and deploy the resulting application. After this point you should be able to go to localhost:8080// and see a login page. Quite neat....

Since this blog entry is quite long so far, I will cut it here to return later and explore how AppFuse automates other development tasks and how to use Eclipse to edit the files.