ProjInit: Project kickoff build scripts

One request that I get time and again when posting code is “how do I structure my code”. This is definitely a wide open area that differs from person to person and team to team. I have somewhat settled on the way I structure my directories and code, using my own ideas and of course “borrowing” from others. One topic that I enjoy discussing is being able to automate the tedious and time consuming, and in this case generating that directory structure that I will use in my project. In this post I will share and explain a simple way to generate your directory structure, using Apache Ant, to get up and running quickly and keep all of your projects consistent.

Installing and setting up Ant is not a topic that I will get into with this post, but it is a very straightforward process.

The idea behind the following sample scripts are to quickly get you up and running in a project, automating the creation of the project directories. There are two Ant files associated with this, build.properties and projinit.xml. You will need to place these two files in the root of the directory where you want your project to reside, e.g. “MyProject/build.properties” and “MyProject/build.xml”.

build.properties

So, digging into the first file, build.properties is where you will set the structure of your project. The gist below shows the structure as related to variables that Ant will use, such as “$PROJ_BASE”. Once you study this file for a few minutes you can see that you can easily add other directories if you like, or totally change them to match your typical directory structure. Simply create a file in your project root named “build.properties” (using .properties as the extension), and copy the text below into that new file. You can click on the “raw” link in the GitHub gist footer to grab the base text. You can also click on the “This Gist” link to visit GitHub, then click on the “download” button to download the build.properties file itself.

projinit.xml

Finally, you will need to create one more file in the root of your project, naming it “projinit.xml”. Again, click on “raw” link in the gist below and paste that text into the new file. You can also click on the “This Gist” link to visit GitHub, then click on the “download” button to download the projinit.xml file itself.

Running the script

Now that we have both the “build.properties” and “projinit.xml” files in place we can run the build. Just to clarify, a typical default name for the Ant build process is “build.xml”. In this case we are only running the directory structure creation process one time, because we only need that to start a project. So, in Ant, you can define secondary build files to run by using a “-buildfile” flag. In our case the secondary build file is “projinit.xml” and can be kicked off by typing the following at the command line, remembering to cd into your project root where the build.properties and projinit.xml files are located:

ant -buildfile projinit.xml

Hopefully you received a nice bit of feedback saying “All targets ran successfully.”. If you did not receive this make sure you have Ant set up correctly and also make sure all sections of build.properties are filled in. If everything ran correctly you should now see a project directory structure within your root, containing everything from the namespace and mvc directories in src to a lib folder.

For more information of being in control of your build and deployment process, visit my post at Own the Build.