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
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:
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.