官术网_书友最值得收藏!

Make

The make program was originally created in April 1976, so this is not a new tool. It is included in the Unix system, so this tool is available without any extra installation on Linux, Mac OS X, or any other Unix-based system. Additionally, there are numerous ports of the tool on Windows, and some version is/was included in the Visual Studio compiler toolset.

The Make is not tied to Java. It was created when the major programming language was C, but it is not tied to C or any other language. The make is a dependency description language that has a very simple syntax.
The make, just like any other build tool, is controlled by a project description file. In the case of make, this file contains a rule set. The description file is usually named Makefile, but in case the name of the description file is different, it can be specified as a command-line option to the make command.

Rules in Makefile follow each other and consist of one or more lines. The first line starts at the first position (there is no tab or space at the start of the line) and the following lines start with a tab character. Thus, Makefile may look something like the following code:

run : hello.jar 
java -cp hello.jar HelloWorld

hello.jar : HelloWorld.class
jar -cf hello.jar HelloWorld.class

HelloWorld.class : HelloWorld.java
javac HelloWorld.java

This file defines three so-called targets: run, hello.jar, and HelloWorld.class. To create HelloWorld.class, type the following line at the command prompt:

    make HelloWorld.class

Make will look at the rule and see that it depends on HelloWorld.java. If the HelloWorld.class file does not exist, or HelloWorld.java is newer than the Java class file, make will execute the command that is written on the next line and it will compile the Java source file. If the class file was created following the last modification of HelloWorld.java, then make knows that there is no need to run the command.

In the case of creating HelloWorld.class, the make program has an easy task. The source file was already there. If you issue the make hello.jar command, the procedure is more complex. The make command sees that in order to create hello.jar, it needs HelloWorld.class, which itself is also a target on another rule. Thus, it may need to be created.

First, it starts the problem the same way as before. If HelloWorld.class is there, and is older than hello.jar, there is nothing to do. If it is not there, or is newer than hello.jar, then the jar -cf hello.jar HelloWorld.class command needs to be executed, although not necessarily at the moment when it realizes that it has to be performed. The make program remembers that this command has to be executed sometime in the future when all the commands that are needed to create HelloWorld.class are already executed successfully. Thus, it continues to create the class file exactly the same way as I described earlier.

In general, a rule can have the following format:

target : dependencies 
command

The make command can create any target using the make target command by first calculating which commands to execute and then executing them one by one. The commands are shell commands executing in a different process and may pose problems under Windows, which may render the Makefile files' operating system dependent.

Note that the run target is not an actual file that make creates. A target can be a file name or just a name for the target. In the latter case, make will never consider the target to be readily available.

As we do not use make for a Java project, there is no reason to get into more details. Additionally, I cheated a bit by making the description of a rule simpler than it should be. The make tool has many powerful features out of the scope of this book. There are also several implementations that differ a little from each other. You will most probably meet the one made by the Free Software Foundation—the GNU make. And, of course, just in case of any Unix command-line tool, man is your friend. The man make command will display the documentation of the tool on the screen.

The main points that you should remember about make are as follows:

  • It defines the dependencies of the inpidual artifacts (targets) in a declarative way
  • It defines the actions to create the missing artifacts in an imperative way

This structure was invented decades ago and has survived up until now for most of the build tools, as you will see in the next few chapters.

主站蜘蛛池模板: 古田县| 新沂市| 邵阳县| 宣化县| 洱源县| 沂源县| 镇赉县| 凌海市| 如皋市| 乌鲁木齐县| 安多县| 青海省| 平阳县| 安龙县| 咸宁市| 德保县| 四平市| 遵化市| 遵义县| 三门峡市| 郯城县| 海伦市| 安新县| 手游| 白沙| 天气| 济源市| 闸北区| 灯塔市| 鹤峰县| 大洼县| 沅陵县| 岳西县| 页游| 大同县| 聂拉木县| 五莲县| 新乐市| 蓬安县| 成武县| 宁城县|