- Java 9 Programming By Example
- Peter Verhas
- 773字
- 2021-07-02 23:37:30
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.
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- 深入理解Java7:核心技術與最佳實踐
- C語言從入門到精通(第4版)
- 微信公眾平臺開發:從零基礎到ThinkPHP5高性能框架實踐
- C語言程序設計
- Building Serverless Applications with Python
- 執劍而舞:用代碼創作藝術
- Geospatial Development By Example with Python
- Clean Code in C#
- Django 5企業級Web應用開發實戰(視頻教學版)
- Visual Basic程序設計(第三版)
- ASP.NET求職寶典
- 計算語言學導論
- 百萬在線:大型游戲服務端開發
- C語言程序設計教程