|
1.3. Automating the ExampleIt's time for a little housekeeping. To go much further, you need to automate. You probably already use Ant. It's a standardized way to organize all of the tasks that you need to build your application. If you don't already use it, you need to. Ant has become ubiquitous. In order to work with Java tools, you need to be able to speak the language. We're not going to provide yet another justification for Ant in this chapter, since you've probably read enough of them already. 1.3.1. How do I do that?You'll need to download an Ant distribution. You may as well use the one that comes with Spring (http://springframework.org/ ). To run all of the examples for this book, you'll want to get Spring version 1.1, or later. Follow all of the directions for your platform. Next, you'll organize the directory structure in a way that's friendly for web applications and Spring. I recommend that you use the organization that you'll find with the Spring sample applications, so that you'll have a working example. These are the directories that you'll use:
To start with, place the source files like this:
Finally, you'll need an Ant build script, which we'll place in the root folder of our project. Example 1-8 is the one that we'll use to start with. Example 1-8. build.xml<?xml version="1.0"?> <project name="RentABike" default="compile" basedir="."> <property name="src.dir" value="src"/> <property name="test.dir" value="test"/> <property name="war.dir" value="war"/> <property name="class.dir" value="${war.dir}/classes"/> <target name="init"> <mkdir dir="${class.dir}"/> </target> <target name="compile" depends="init" description="Compiles all source code"> <javac srcdir="${src.dir}" destdir="${class.dir}" /> </target> <target name="clean" description="Erases contents of classes dir"> <delete dir="${class.dir}"/> </target> </project> To run a build, switch to the directory called C:\RentABikeApp> and type: [1]C:\RentABikeApp>ant Buildfile: build.xml init: [mkdir] Created dir: C:\RentABikeApp\war\WEB-INF\classes compile: [javac] Compiling 5 source files to C:\RentABikeApp\war\WEB-INF\classes BUILD SUCCESSFUL Total time: 2 seconds 1.3.2. What just happened?Note: That's what I call automation. We've reduced the total keystrokes that you'll need to build the project to four. Ant built the system in one automated step. That's not such a big deal now, but it will be increasingly important as the build steps get more sophisticated. You'll want the system to run unit tests, add any precompilation steps such as JDO's byte code enhancement in Chapter 5, or copy configuration files to the appropriate place. You can also have special tasks to initialize a database or deploy our war file to an application server. 1.3.3. What about......the fact that some integrated development environments don't need Ant at all? If you want to work with these without Ant, you'll need to make sure that: Note: Actually, we're developing this application in the IDE called IDEA, made by JetBrains. We find that it has the best refactoring support out there. Take heart. We also test the Ant version of each lab.
From here on out, we'll tell you when you need to add a new directory, execute the application, or make a new library available. We won't tell you how. We assume you know how to use your own IDE, or Ant directly. |
|