Makefile是什麼

  目錄
  
  Makefile是什麼
  
  Makefile基本語法
  
  Docker構建用的指令
  
  參考
  
  使用Makefile構建Docker
  
  剛開始學習docker命令的時候,很喜歡一個字一個字敲,由於這樣會記住命令。後來熟悉了以後,每次想要作一些操做的時候就不得不
  
  重複的輸入之前的命令。當切換一個項目以後,又重複輸入相似但又不徹底相同的命令,僅僅經過history命令加速也有限。
  
  因而想,把要用的命令寫到shell裏,而後調用shell腳本去作。剛開始確實是這樣作的。好比https://github.com/Ryan-Miao/docker-yapi。
  
  直到有一天,發現有人使用Makefile來存儲操做,瞬間感受很棒。
  
  這裏簡單記錄Makefile的簡單用法。
  
  Makefile是什麼
  
  Makefile是make命令的規則配置文件。make命令是什麼?
  
  先來看看make在哪裏
  
  ~ > whereis make
  
  make: /usr/bin/make /usr/share/man/man1/make.1.gz
  
  能夠看到make是bin下的以可執行文件。 看看用戶手冊
  
  MAKE(1)                                                          User Commands                                                         MAKE(1)
  
  NAME
  
  make - GNU make utility to maintain groups of programs
  
  SYNOPSIS
  
  make [OPTION]... [TARGET]...
  
  DESCRIPTION
  
  The  make  utility will determine automatically which pieces of a large program need to be recompiled, and issue the commands to recom‐
  
  pile them.  The manual describes the GNU implementation of make, which was written by Richard Stallman and Roland McGrath, and is  cur‐
  
  rently  maintained  by Paul Smith.  Our examples show C programs, since they are very common, but you can use make with any programming
  
  language whose compiler can be run with a shell command.  In fact, make is not limited to programs.  You can use  it  to  describe  any
  
  task where some files must be updated automatically from others whenever the others change.
  
  To prepare to use make, you must write a file called the makefile that describes the relationships among files in your program, and the
  
  states the commands for updating each file.  In a program, typically the executable file is updated from object  files,  which  are  in
  
  turn made by compiling source files.
  
  Once a suitable makefile exists, each time you change some source files, this simple shell command:
  
  make
  
  suffices  to  perform  all necessary recompilations.  The make program uses the makefile description and the last-modification times of
  
  the files to decide which of the files need to be updated.  For each of those files, it issues the commands recorded in the makefile.
  
  make executes commands in the makefile to update one or more target names, where name is typically a  program.   If  no  -f  option  is
  
  present, make will look for the makefiles GNUmakefile, makefile, and Makefile, in that order.
  
  Normally  you  should  call  your makefile either makefile or Makefile.  (We recommend Makefile because it appears prominently near the
  
  beginning of a directory listing, right near other important files such as README.)  The first name checked, GNUmakefile, is not recom‐
  
  mended for most makefiles.  You should use this name if you have a makefile that is specific to GNU make, and will not be understood by
  
  other versions of make.  If makefile is '-', the standard input is read.
  
  make updates a target if it depends on prerequisite files that have been modified since the target was last modified, or if the  target
  
  does not exist.
  
  大體是說make是GNU中維護和組織程序的。好比咱們的C語言編譯, 再好比源碼安裝某些軟件,好比nginx的時候。那麼GNU是什麼鬼?
  
  GNU(GNU's Not Unix)是一個類Unix系統, 目標是建立一套徹底自由的操做系統。在Linux出現以前,GNU已經完成了除了內核以外大部分的軟件。Linux出現以後,與GNU結合變成GNU/Linux。
  
  嚴格的說,Linux只表明Linux內核,其餘Linux軟件稱爲Linux發行版。但因爲商業發行商堅持稱呼Linux, 雖然已經改名爲GNU/Linux, 但你們依然叫Linux.
  
  ## 好比個人本機Ubuntu
  
  ~ ❯ uname
  
  Linux
  
  ~ ❯ uname -a
  
  Linux ryan-computer 4.18.0-20-generic #21~18.04.1-Ubuntu SMP Wed May 8 08:43:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  
  ## 大部分基於Debian的docker鏡像
  
  airflow@88e36c088b81:~$ cat /etc/issue
  
  Debian GNU/Linux 9 \n \l
  
  ## RedHat
  
  [root@data-docker001 docker-airflow]# cat /etc/redhat-release
  
  CentOS Linux release 7.4.1708 (Core)
  
  [root@data-docker001 docker-airflow]# uname -a
  
  Linux data-docker001 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  
  make的基本用法就是
  
  make target
  
  Makefile基本語法
  
  詳細參見附錄參考,這裏爲了減小認知成本,只羅列用到的知識點。
  
  在當前目錄建立一個叫作Makefile的文件。
  
  聲明變量
  
  簡單的變量賦值,好比聲明name
  
  name=ryan
  
  聲明規則Rule
  
  Makefile文件由一系列規則(rules)構成。每條規則的形式以下。
  
  <target> : <prerequisites>
  
  [tab]  <commands>
  
  target 目標
  
  prerequisites 前置條件
  
  tab command必須由tab隔開
  
  commands 只能有一行的shell
  
  防止target和文件名同樣
  
  當咱們設置的target和當前目錄下的文件名同樣的話,target會被忽略,因此,一般,咱們把target都用作phony target。
  
  .PHONY: build start push
  
  表示, build start push 這3個target,不檢查當前目錄下的文件,直接執行命令。
  
  Docker構建用的指令
  
  我經常使用的Makefile以下
  
  NAME = ryan/airflow
  
  VERSION = 1.10.4
  
  .PHONY: build start push
  
  build:  build-version
  
  build-version:
  
  docker build -t ${NAME}:${VERSION}  .
  
  tag-latest:
  
  docker tag ${NAME}:${VERSION} ${NAME}:latest
  
  start:
  
  docker run -it --rm ${NAME}:${VERSION} /bin/bash
  
  push:   build-version tag-latest
  
  docker push ${NAME}:${VERSION}; docker push ${NAME}:latest
  
  構建一個版本的鏡像
  
  make build
  
  構建完畢,運行一下鏡像,看看內容是否正確
  
  make start
  
  最後推送到docker倉庫
  
  make push
  
  從上面的操做中能夠看出,Phoenix 支持大多數標準的 SQL 語法。關於 Phoenix 支持的語法、數據類型、函數、序列等詳細信息,由於涉及內容不少,能夠參考其官方文檔,官方文檔上有詳細的說明:
  
  語法 (Grammar) :https://www.jintianxuesha.com phoenix.apache.org/language/index.html
  
  函數 (Functions) :http://www.zongxyuLe.com phoenix.apache.org/language/functions.html
  
  數據類型 (Datatypes) :http://www.zheshengyule.com phoenix.apache.org/language/datatypes.html
  
  序列 (Sequences) :http://www.yuanhuapt.cn phoenix.apache.org/sequences.html
  
  聯結查詢 (Joins) :http://www.yasenyuLe.com phoenix.apache.org/joins.html
  
  4、Phoenix Java API#
  
  由於 Phoenix 遵循 JDBC 規範,並提供了對應的數據庫驅動 PhoenixDriver,這使得采用 Java 語言對其進行操做的時候,就如同對其餘關係型數據庫同樣,下面給出基本的使用示例。
  
  4.1 引入Phoenix core JAR包#
  
  若是是 maven 項目,直接在 maven 中央倉庫找到對應的版本,導入依賴便可:
  
  Copy
  
  <!-- https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-core --> <dependency> <groupId>org.apache.phoenix</groupId> <artifactId>phoenix-core</artifactId> www.xinhegzx.cn  <version>4.14.0-cdh5.14.2</version> </dependency>
  
  若是是普通項目,則能夠從 Phoenix 解壓目錄下找到對應的 JAR 包,而後手動引入:
  
  4.2 簡單的Java API實例#
  
  Copy
  
  import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class PhoenixJavaApi { public static void main(String[] args) throws Exception { // 加載數據庫驅動 Class.forName("org.apache.phoenix.jdbc.PhoenixDriver"); /* * 指定數據庫地址,格式爲 jdbc:phoenix:Zookeeper 地址 * 若是 HBase 採用 Standalone 模式或者僞集羣模式搭建,則 HBase 默認使用內置的 Zookeeper,默認端口爲 2181 */ Connection connection = DriverManager.getConnection("jdbc:phoenix:192.168.200.226:2181"); PreparedStatement statement = connection.prepareStatement("SELECT * FROM us_population"); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString("city") + " " + resultSet.getInt("population")); } statement.close(); connection.close(); }html

相關文章
相關標籤/搜索