參考資料:http://www.jianshu.com/p/9e2c7a7c1211
私有庫A與私有庫B之間的依賴處理:
例如私有庫B依賴於私有庫A,在私有庫B本地驗證時:
pod lib lint
會報錯,提示找不到私有庫A。
執行命令為:
pod lib lint --sources='http://[privateLibName]/cocoaspecs.git,https://github.com/CocoaPods/Specs.git'
關鍵詞:庫、模塊式開發、CocoaPods、私有庫、
工具:終端、git、CocoaPods
步驟總結:
1、打開終端,進入要建立私有庫項目工程的路徑,並執行pod庫工程創建命令行
2、進入我們的Example項目工程,執行安裝CocoaPods項目命令行
3、添加庫源碼文件
4、書寫CocoaPods配置文件LATAlert.podspec(建議使用Atom + cocoa-pods-ruby-snippets插件來編輯該文件)
5、創建私有庫git地址,並完善.podspec配置文件
6、再次進入我們的Example文件,執行pod更新指令,安裝本地庫源碼
7、添加樣例代碼,運行樣例測試
8、本地pod配置文件驗證
9、項目工程發布tag 0.0.1
10、私有庫發布
11、驗證發布的私有庫
涉及到的命令行:
Git:
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git remote add origin https://git.oschina.net/KKLater/LATAlert.git Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git add . Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git commit -a -m "0.0.1" Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git pull origin master Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git push origin master Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git tag 0.0.1 Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git push origin 0.0.1
Pod:
Later@localhost:~/Desktop/私有庫創建教程$ pod lib create LATAlert Later@localhost:~/Desktop/私有庫創建教程/LATAlert/Example$ pod install --no-repo-update Later@localhost:~/Desktop/私有庫創建教程/LATAlert/Example$ pod update --no-repo-update Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod repo add LATSpecs https://git.oschina.net/KKLater/LATPodspecs.git Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod lib lint Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod lib lint --verbose Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec --verbose
//忽略警告
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod lib lint --allow-warnings
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec --allow-warnings
Later@localhost:~$ pod repo remove LATSpecs
下面我們通過一個例子來具體講解整個步驟流程。
例子里面,我們創建一個私有庫LATAlert,導入LATAlert私有庫的項目,可以調用私有庫的方法來執行彈出一個Alert顯示信息!
1、打開終端,進入要建立私有庫項目工程的路徑,並執行pod庫工程創建命令行
先創建個文件夾來放置 這個例子的文件夾名字:私有庫創建教程
Later@localhost:~$ cd /Users/Later/Desktop/私有庫創建教程 Later@localhost:~/Desktop/私有庫創建教程$ pod lib create LATAlert
注意:cocoapods要升級到最新版本。 我之前電腦是舊版本,是不會出現下面的界面的。 至於舊版本怎么設置下面這些屬性個人沒研究,有興趣的可以自己試試。
終端得到以下界面:
這里會詢問幾個問題(答案根據實際情況設置),分別是:
1、語言選擇
—— 教程選擇Objc,如果要做Swift私有庫,請選擇輸入Swift
2、是不是需要一個demo項目工程
—— 教程選擇Yes,需要創建一個demo工程,建議創建一個demo工程
3、測試框架使用哪一個
—— 教程選擇None
4、是不是需要做基本的測試
—— 教程選擇Yes
5、類前綴是什么
—— 教程輸入LAT
如果出現上面的界面,說明,私有庫工程創建成功了。
2、進入我們的Example項目工程,執行安裝CocoaPods項目命令行
Later@localhost:~$ cd /Users/Later/Desktop/私有庫創建教程/LATAlert/Example Later@localhost:~/Desktop/私有庫創建教程/LATAlert/Example$ pod install --no-repo-update
終端界面如下:
雙擊LATAlert.xcworkspace運行項目Example工程,驗證工程的正確性。
3、添加庫源碼文件
將源碼文件復制到文件夾路徑:LATAlert/LATAlert/Classes下。
資源文件放到Assets下。
注:1.如果還沒有源碼,則可以在Example下直接創建源碼文件,實際測試通過后,再按照下面的方式來添加源碼文件。2.方便起見這里直接將之前使用的源碼文件拷貝過來,其他根據實際情況,看官自行添加。3.文件路徑並不是絕對的,但是建議按指定路徑存放,便於整體的后期維護更新
4、書寫CocoaPods配置文件LATAlert.podspec(建議使用Atom + cocoa-pods-ruby-snippets插件來編輯該文件)
雙擊打開LATAlert.podspec文件,此處我設置了默認Atom打開,看官自行配置,但是要注意編輯內容一定要教程一樣。
我們可以把沒有用的注釋刪掉。具體常用的配置如下:
# # Be sure to run `pod lib lint LATAlert.podspec' to ensure this is a # valid spec before submitting. # # Any lines starting with a # are optional, but their use is encouraged # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html # Pod::Spec.new do |s| #庫名 s.name = 'LATAlert' #庫版本 s.version = '0.0.1' #庫簡短描述 s.summary = 'A short description of LATAlert.' #庫詳細描述 s.description = <<-DESC TODO: Add long description of the pod here. DESC #庫介紹主頁地址 s.homepage = 'https://github.com/<GITHUB_USERNAME>/LATAlert' #庫開源許可 s.license = { :type => 'MIT', :file => 'LICENSE' } #作者信息 s.author = { 'Later' => 'lshxin89@126.com' } #源碼git地址 s.source = { :git => 'https://github.com/<GITHUB_USERNAME>/LATAlert.git', :tag => s.version.to_s } #庫依賴系統版本 s.ios.deployment_target = '8.0' #源碼文件配置 s.source_files = 'LATAlert/Classes/**/*' #資源文件配置 s.resource_bundles = { 'LATAlert' => ['LATAlert/Assets/*.png'] } #源碼頭文件配置 s.public_header_files = 'Pod/Classes/**/*.h' #系統框架依賴 s.frameworks = 'UIKit', 'MapKit' #第三方框架依賴 s.dependency 'AFNetworking', '~> 2.3' end
清理掉不需要的配置項之后,整體如下:
Pod::Spec.new do |s| #庫名 s.name = 'LATAlert' #庫版本 s.version = '0.0.1' #庫簡短描述 s.summary = 'A short description of LATAlert.' #庫詳細描述 s.description = <<-DESC TODO: Add long description of the pod here. DESC #庫介紹主頁地址 s.homepage = 'https://github.com/<GITHUB_USERNAME>/LATAlert' #庫開源許可 s.license = { :type => 'MIT', :file => 'LICENSE' } #作者信息 s.author = { 'Later' => 'lshxin89@126.com' } #源碼git地址 s.source = { :git => 'https://github.com/<GITHUB_USERNAME>/LATAlert.git', :tag => s.version.to_s } #庫依賴系統版本 s.ios.deployment_target = '8.0' #源碼文件配置 s.source_files = 'LATAlert/Classes/**/*' #源碼頭文件配置 s.public_header_files = 'LATAlert/Classes/*.h' #系統框架依賴 s.frameworks = 'UIKit' end
細心的看官注意到,這里我們使用的homepage和source並不完整下面就需要我們創建一個git私有庫,用於項目工程的存放,獲取我們所需要的地址。
5、創建私有庫git地址,並完善.podspec配置文件
點擊創建,我們的私有庫就創建完成了,復制我們的私有庫地址,也就是我們的.podspec文件里面source所需要的地址了。
source有了,但是我們的homepage怎么辦呢?如果有可以訪問的高大上的介紹網頁頁面,建議使用其網址。
至此,我們的配置文件就結束了。此時細心的看客又說了,我們創建了git私有庫,但是還沒有添加文件啊?
6、再次進入我們的Example文件,執行pod更新指令,安裝本地庫源碼
Later@localhost:~/Desktop/私有庫創建教程/LATAlert/Example$ pod update --no-repo-update
終端截圖如下:
此時進入我們的Example文件夾,雙擊LATAlert.xcworkspace打開看看:
我們的源碼已經在里面了。
這時,可以填寫我們的樣例代碼來進行測試嘍。
7、添加樣例代碼,運行樣例測試
找到我們的樣例文件LATViewController.m,引入頭文件LATAlert.h,填寫代碼,點擊屏幕時執行彈出Alert提示框,具體代碼如下:
// // LATViewController.m // LATAlert // // Created by Later on 10/14/2016. // Copyright (c) 2016 Later. All rights reserved. // #import "LATViewController.h" #import "LATAlert.h" @interface LATViewController () @end @implementation LATViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [LATAlert showAlertWithTitle:@"測試" message:@"這時測試信息" OkBlock:^{ NSLog(@"點擊了確認"); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
運行項目測試:
alert信息彈出了,說明,我們的本地運行例程是正常的。下面就需要我們把項目發布到git,並添加到pod了。
8、本地pod配置文件驗證
為了保證項目的正確性,尤其是pod配置文件的正確性,在正式提交前,我們需要執行以下本地驗證。在本地驗證正常的情況下,再上傳發布還是比較穩妥的。
終端進入我們的項目文件路徑:
Later@localhost:~/Desktop/私有庫創建教程/LATAlert/Example$ cd /Users/Later/Desktop/私有庫創建教程/LATAlert
如果現在終端正處於Example文件路徑下,也可以執行
Later@localhost:~/Desktop/私有庫創建教程/LATAlert/Example$ cd ..
退回到上層文件夾,也就是我們的項目文件路徑了。
執行pod本地驗證指令:
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod lib lint
或者忽略警告的指令:
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod lib lint --allow--warning
界面顯示:
如果經驗豐富,大概一看便知這里的警告是告訴我們這個summary簡介不是很有內涵。此處我們改一下我們的podspec文件內部的簡介和描述,消除下警告。修改完的podspec文件如下。
# # Be sure to run `pod lib lint LATAlert.podspec' to ensure this is a # valid spec before submitting. # # Any lines starting with a # are optional, but their use is encouraged # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html # Pod::Spec.new do |s| #庫名 s.name = 'LATAlert' #庫版本 s.version = '0.0.1' #庫簡短描述 s.summary = 'LATAlert pod Use.' #庫詳細描述 s.description = <<-DESC TODO: LATAlert pod Use. DESC #庫介紹主頁地址 s.homepage = 'http://mobile.fblife.com' #庫開源許可 s.license = { :type => 'MIT', :file => 'LICENSE' } #作者信息 s.author = { 'Later' => 'lshxin89@126.com' } #源碼git地址 s.source = { :git => 'https://git.oschina.net/KKLater/LATAlert.git', :tag => s.version.to_s } #庫依賴系統版本 s.ios.deployment_target = '8.0' #源碼文件配置 s.source_files = 'LATAlert/Classes/**/*' #源碼頭文件配置 s.public_header_files = 'LATAlert/Classes/*.h' #系統框架依賴 s.frameworks = 'UIKit' end
鑒於是初學者,還是建議掌握下查看具體原因的指令比較靠譜。具體指令也很簡單,在執行本地驗證的指令后面添加上--verbose在執行下就可以了。
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod lib lint --verbose
我們再次執行下驗證,查看結果,BUILD SUCCEEDD,本地驗證成功:
至此,我們的源碼已經導入、樣例工程已經驗證執行、podspec配置文件本地已經驗證,那么我們是不是就可以直接在pod里面使用了呢?答案是肯定的,還是不行!目前只是處於本地狀態,並沒有發布,所以還是不能使用的。
9、項目工程發布tag 0.0.1
在項目工程文件下執行git相關指令,並添加tag0.0.1,發布到git。
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git remote add origin https://git.oschina.net/KKLater/LATAlert.git Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git add . Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git commit -a -m "0.0.1" Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git pull origin master Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git push origin master Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git tag 0.0.1 Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ git push origin 0.0.1
相關指令執行結束后,此時我們再去看我們的git項目:
10、私有庫發布
對於開源框架,podspec文件實在cocoapod的開源管理spec項目上面的,見下圖:
在這里的Specs文件加內,我們能看到我們熟悉的 AFNetworking
但是我們創建的是私有庫,所以我們需要創建自己的Specs管理庫。
例如我們要創建的是LATSpecs,首先需要再創建一個git私有庫,用於管理我們的LATSpecs。
創建之后獲取到git地址:https://git.oschina.net/KKLater/LATPodspecs.git
在終端執行Specs創建指令:
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod repo add LATSpecs https://git.oschina.net/KKLater/LATPodspecs.git
執行之后的結果是:
看到這個,我們可以愉快的發布了。執行發布命令,直接發布好了。
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec
如果有問題 可以試試忽略警告
Later@localhost:~/Desktop/私有庫創建教程/LATAlert$ pod repo push LATSpecs LATAlert.podspec --allow--warnings
發布成功之后,我們來看一下我們的LATSpecs的git項目:
查看我們本地的Specs庫:
直接Findle ->右鍵 -> 前往文件夾 -> 輸入:~/.cocoapods/repos ->點擊前往
至此,我們的私有庫創建發布結束。但是我們注意到,LATSpecs的README.md文件是空的,為了后期的維護更新,以及團隊小伙伴的使用方便,建議完備一下信息。
完備之后的信息如下:
終端執行一下進入我們的私有庫管理Specs,git更新提交下:
Later@localhost:~$ cd /Users/Later/.cocoapods/repos/LATSpecs Later@localhost:~/.cocoapods/repos/LATSpecs$ git add . Later@localhost:~/.cocoapods/repos/LATSpecs$ git commit -a -m "Add LATAlert" Later@localhost:~/.cocoapods/repos/LATSpecs$ git pull origin master Later@localhost:~/.cocoapods/repos/LATSpecs$ git push origin master
命令行執行結束之后,我們再看一下我們的git:
有沒有高大上呢?
既然已經提交發布,那下面我們新建一個CocoaPods管理的項目,來驗證一下我們的庫吧。
11、驗證發布的私有庫
新建PodTest項目,並創建Podfile文件,安裝CocoaPods工程。
Podfile文件內的代碼如下:
platform :ios,'8.0' target 'PodTest' do pod 'LATAlert',:git => 'https://git.oschina.net/KKLater/LATAlert.git' end
注意:podfile文件里要加上
source 'https://github.com/CocoaPods/Specs.git'
source 'http://112.124.41.46/tuchaoprivatecommon/xgtcprivatepodsrepo.git'
如果你不配置系統的索引庫 會找不到其他的庫 只能找到你自己創建的倉庫
最近一個命令 pod install
Nice 完成了
終端進入我們的測試工程根目錄,並執行pod安裝指令:
Later@localhost:~$ cd /Users/Later/Desktop/私有庫創建教程/PodTest Later@localhost:~/Desktop/私有庫創建教程/PodTest$ pod install --no-repo-update
終端界面如下:
我們重新打開測試項目:
在ViewController.m文件導入文件,並按照Example的樣例書寫代碼驗證。代碼如下:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [LATAlert showAlertWithTitle:@"測試" message:@"這時測試信息" OkBlock:^{ NSLog(@"點擊了確認"); }]; }
不同的是,這里引入頭文件時,我們使用的是:#import <LATAlert/LATAlert.h>而不是Example樣例工程里面的引入方式:#import "LATAlert.h"。
運行PodTest項目,點擊屏幕,是不是也彈出Alert提示了呢?
至此,我們的私有庫發布結束。具體更詳細的信息,建議各位看客多多參考CocoaPods官網,會有更不一樣的體驗。英語不好的,體驗會更加深刻喲!
至於更新版本的話
localhost:xgoalibrary kfj$ git add . //新增的文件 加入倉庫
localhost:xgoalibrary kfj$ git commit -a -m "0.1.2" "0.1.2" -- tag 號 // 需要提交的文件上傳確認
localhost:xgoalibrary kfj$ git pull origin master //下啦最新資源
localhost:xgoalibrary kfj$ git push origin master //上傳本地最新代碼
localhost:xgoalibrary kfj$ git tag 0.1.2 //打tag
localhost:xgoalibrary kfj$ git push --tags
localhost:xgoalibrary kfj$ pod lib lint --allow-warnings //驗證本地庫是否正確 lib 換成 spec 的 話 就是 就是驗證遠程服務器的 庫 是否正確
localhost:xgoalibrary kfj$ pod repo push XGOALibrary XGOALibrary.podspec --allow-warnings
命令行指令:
history 查看自己用過的歷史指令
pod repo list 查看本地repo私有庫資源信息
open ~/.cocoapods 打開本地私有庫資源的位置
