Saturday, May 16, 2009

Shopping Cart Web Application - Part 4 - Build

Introduction
What is the benefit of running build scripts? As a developer you may at first think it is a waste of time. After all most IDE’s are able to build package and deploy your projects with the click of a button. So why go through all the hassle of writing a build script?

A build process becomes essential when developing in a team and where the application you are working on requires other projects that are not necessarily built by you but by another team in your organization. It is also a good idea to automate your build process so that the build is started automatically by another service. There are a couple of tools available that can be used to manage your build process. One I am familiar with and like very much is Hudson continuous integration tool.

What you need before we get started
  • I use Eclipse as my IDE. Eclipse comes with a built in Ant plug-in so if you have Eclipse installed you have almost everything you need to get started.
  • You will need the previous third party libraries that were mentioned and used in earlier parts to this tutorial

What we will cover
  • Which build tool to use?
  • The build script

Level
  • Beginner

Which build tool to use?
There are a number of tools available that you can use to write build scripts. Two of the most popular are:
  1. Apache Ant
  2. Apache Maven

Ant has been around a lot longer than Maven however the Maven framework is growing in popularity every day. They both are very good tools to use. I like Ant because it is very simple to use and you can do pretty much anything you want with it. I also like Maven for a number of reasons one of them being Maven’s dependency management framework. Ant also has its own dependency management framework called Ivy which is very similar to Maven’s dependency management.

I have chosen to use Ant for my examples as I think it is quicker to get up and running then Maven and the full extent of Maven and Ivy is out of the scope for this tutorial.

The build script
Please download the ShoppingCartPart4.zip and import the shopping-cart-core-build-part4 project into Eclipse. I created a build file called build.xml and saved it to a new folder called build in the project root.

To run the Ant script targets within Eclipse you need to open the Ant View, you can do this by selecting Window -> Show View -> Other -> Ant -> Ant. An Ant tab will appear in your workspace. Right click in the Ant view window and select Add Buildfiles. Navigate to the build.xml file in the shopping-cart-core-build-part4 project and select Ok. You should see the following targets in the Ant view:

  • build – builds the whole project
  • clean – deletes compilation directory
  • compile – compiles the source files
  • package – packages the classes into a JAR
  • test – runs the JUnit tests we created in the previous tutorial

You can run any of them by double clicking on their name. If you double click on build all the targets will be run and you should see build information displayed in your Console view.








































































8 comments:

nonty said...

I find this tutorials so helping and interesting,all i want to know is it a must to have a build part for every web application?

Unknown said...

If you are only interested in building a web app that will be deployed to your local pc through your IDE than you don't need a build script. However most organizations would (or should) have a build process for their applications. It is therefore an important step to know in developing web applications.

Unknown said...

hi Ross,

im trying to build shopping cart.and i keep on getting this error

Test com.my domain.shopping cart.service.test.ShoppingServiceTest failed (crashed)

what must i do in order for it to build succesful

tx

Unknown said...

It looks like the build fails when running the JUnit target. Is there a more detailed message you can send me as the one you sent me doesn't tell me why it failed?

Unknown said...

Hi Ross ,

i keep getting the following errors from my shopping cart , any idea how i can go around this

Severity and Description Path Resource Location Creation Time Id
Project 'shopping-cart-core-test-cases-part3' is missing required library: '/LIBS/junit-4.5.jar' shopping-cart-core-test-cases-part3 Build path 1255070138844 6

Severity and Description Path Resource Location Creation Time Id
The project cannot be built until build path errors are resolved shopping-cart-core-test-cases-part3 Unknown 1255335306572 14

Unknown said...

The answer is in the error message you sent me. Does the junit-4.5.jar exist in the LIBS project? If not you can download it here

Jan Moseneke said...

Livemy how can you build without Junit?

Jan Moseneke said...

Gad add JUnit to you directory