Section 1.3. Automating the Example

team bbl


1.3. Automating the Example

It'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:


src

This directory has the home for all of the source code in your application. As usual, you'll want the directory structure to mirror your package structure.


test

This directory has the home for all of your unit tests. We'll talk more about JUnit in the last lab.


db

This directory has the home for all database specific scripts, configuration, and code. Some of these will be configuration files, some will help set up a database with the correct schema, and some will help initialize the database with test data, if necessary. If you support multiple databases, each will have its own directory under db.


war

The war file is the typical deployable unit for the web application. If you're using a J2EE web container or a servlet container like Tomcat, the web.xml configuration file will go here. Spring's configuration files will also go here. We'll talk more about Spring's configuration files as we layer on additional labs.

To start with, place the source files like this:

  • RentABike.java goes in src, in the correct package hierarchy.

  • ArrayListRentABike goes in src, with RentABike.

  • Bike goes in src, with RentABike.

  • CommandLineView goes in src, with RentABike.

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.
  • You structure the code in the same directories that we do, so that packages will line up correctly.

  • You make sure that your IDE can reach all of the .lib files that we tell you to use.

  • In future chapters, you'll need to worry about how to deploy and run a web application, and how to run JUnit.

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.

    team bbl



    Spring. A developer's Notebook
    Spring: A Developers Notebook
    ISBN: 0596009100
    EAN: 2147483647
    Year: 2005
    Pages: 90

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net