Build Utilities


Contents


Introduction

Using an integrated development environment (IDE) like Eclipse can make it seem that the process to convert source code into an executable program (application) is trivial and straight-forward, simply write code and run your program. However, there are many types of programs and most require the use of external libraries and resources, as well as interdependent modules or packages.

As projects become larger and more complicated, it takes longer and longer to build all parts and may require multiple steps to complete all processing. In some cases, it is more efficient to build only the parts that are being actively developed. However, this means that the programmer must know (remember) all code dependencies and decide at each step what and how to build the part they are working on. You may imagine that this can get quite complicated for large projects with many developers working simultaneously on different parts.

One way to manage this complexity is to write a program that can keep track of the dependencies and be used to build only the parts that are needed. Make, Maven, and Gradle are three such programs. Log in to a Linux terminal window and try each of them.

Make

A simple to use command line tool available in Linux that can be used to make executables, packages, modules, test results, documentation. Really, make can execute any sequence of commands. A makefile is a text-only file that has all of the information needed for make to build target files. A target file is any file that needs to be generated from your project's source files.

Steps to build project files using make

  1. Create a configuration file named makefile.
  2. Define one or more target(s).
  3. For each target, add the commands that must execute to build the target.
  4. Run the command make target-file-name to build desired target files.

Example makefile

This makefile can be used to compile all java source files in the current directory and then run the main method of the class named App. The name of the file is makefile and it is a text only file.

all:
	javac *.java
	java App

At command prompt

> make

or

> make all
The target's name is all in this example, but it could be any valid filename. There are two instructions for the all target. The first is the command to compile all java source code and the second is a command to run the main method of the class named App. Each instruction line must start with a tab character.

To make the first target in your makefile, run the make command at the terminal's command prompt.

To make other targets, run the make command with the name of the desired target.

The Linux man-pages or review the official documentation at: GNU make or try the command man make at a Linux terminal command prompt.


TEST YOURSELF #1

Question 1: Write a makefile that contains two targets, one named App.class that compiles all java files and then runs the App. The second target is named clean and it deletes all class files in the current directory.

Question 2: Write a makefile that has a target named all that depends upon the second target. The second target is named List.class. The first target compiles the App.java file which depends upon the List.class file. After the App class is compiled in the first target, run the App. The second target compiles the List.java class.

Question 3: What does the first target of this makefile do?

all: run_diff
	java Main

run_diff: tests
	diff out1 out1.txt
	diff out2 out2.txt

tests: copy_files
	javac *.java
	java TestMain in1.txt > out1
	java TestMain in2.txt > out2

copy_files: 
	\cp in/in1.txt .
	\cp in/in2.txt .
	\cp out/out1.txt .
	\cp out/out2.txt .

solution

Publish Files

We can use make for other things to, like copying files to a web server or different file system folder. Most commands that can be executed from a command prompt can be executed from a makefile.


TEST YOURSELF #2

Create an HTML 5 page named about_me.html and create an image named my_photo.png. Then, write a makefile with a target named publish_to_html that will copy local html and css files to your public/html/cs400 folder.

solution

There is much more that make can do, and there are other utilities that can do the same and more. Check out Apache Ant, Gradle, and Apache Maven for utilities that have additional features and different build instruction file formats.

Summary

There are different ways of compiling, testing, publishing, and running software and different utilities exist to help programmers develop software efficiently. Spend some time investigating these and other existing options to avoid having to write your own utilities. Often times, existing tools have already has substantial use and testing and can help you get on with the solving your original problem first.