寫在前面
創建私有CocoaPod的文章有很多,而且寫的都很全面,我這里推薦下面兩篇文章供參考,其他部分是個人實操以及工作中的一些總結
- 入門文章:手把手教你利用cocoapod搭建私有庫
- 進階文章:使用Cocoapods創建私有podspec
建立私有倉庫需要的工具(2個倉庫,1個配置)
1. 在gitHub上建立一個git倉庫,代表repo specs倉庫,它的作用主要是用來存放pods庫的索引;
說明:
repo specs倉庫,就是一個容器,它包含了所含Pods庫的一個索引(每一個庫的spec文件),當你使用了Cocoapods后它會被clone到本地的~/.cocoapods/repos目錄下,可以進入到這個目錄看到master文件夾就是這個官方的Spec Repo了。里面的結構如下:
├── Specs
└── [SPEC_NAME]
└── [VERSION]
└── [SPEC_NAME].podspec
如果說自己想有個類似于cocoaPods的倉庫,然后想把所有自己寫的pod庫(如:AFNetworking...)管理起來,那這個repo specs就是這個作用。如果說別人已經有了這個repo specs,你也可以不自己建立這個repo,直接把你寫好的pod庫代碼推到他指定的倉庫即可。
2. 在gitHub上再建立一個倉庫,這個倉庫暫且稱其為pod代碼庫(*.a或者framework),因為這個倉庫主要存放著你按照pod規范寫的共享代碼;
說明:
為了使我們的庫符合規范,我們直接用cocoaPod給我們提供的工具即可pod lib create MyLibrary,具體用法可以參考Using Pod Lib Create這里就不多加贅述。創建好以后,結構如下:
MyLib
├── .travis.yml
├── _Pods.xcproject
├── Example
│ ├── MyLib
│ ├── MyLib.xcodeproj
│ ├── MyLib.xcworkspace
│ ├── Podfile
│ ├── Podfile.lock
│ ├── Pods
│ └── Tests
├── LICENSE
├── MyLib.podspec
├── Pod
│ ├── Assets
│ └── Classes
│ └── RemoveMe.[swift/m]
└── README.md
可以看到,MyLib.podspec已經為我們創建好了。
以我自己在gitHub上創建的為例:
這里的NetworkSpecs就是索引倉庫(repo Specs),Network是代碼pod倉庫,也就是我要分享出去的代碼;
3. 對pod庫中的podspec文件進行配置,它相當于一個描述文件,指定你pod庫的地址,tag等等信息。怎么配,查看官方文檔podSpecs配置官網
Pod::Spec.new do |s|
s.name = "PodTestLibrary" #名稱
s.version = "0.1.0" #版本號
s.summary = "Just Testing." #簡短介紹,下面是詳細介紹
s.description = <<-DESC
Testing Private Podspec.
* Markdown format.
* Don't worry about the indent, we strip it!
DESC
s.homepage = "https://coding.net/u/wtlucky/p/podTestLibrary" #主頁,這里要填寫可以訪問到的地址,不然驗證不通過
# s.screenshots = "www.example.com/screenshots_1", "www.example.com/screenshots_2" #截圖
s.license = 'MIT' #開源協議
s.author = { "wtlucky" => "wtlucky@foxmail.com" } #作者信息
s.source = { :git => "https://coding.net/wtlucky/podTestLibrary.git", :tag => "0.1.0" } #項目地址,這里不支持ssh的地址,驗證不通過,只支持HTTP和HTTPS,最好使用HTTPS
# s.social_media_url = 'https://twitter.com/' #多媒體介紹地址
s.platform = :ios, '7.0' #支持的平臺及版本
s.requires_arc = true #是否使用ARC,如果指定具體文件,則具體的問題使用ARC
s.source_files = 'Pod/Classes/**/*' #代碼源文件地址,**/*表示Classes目錄及其子目錄下所有文件,如果有多個目錄下則用逗號分開,如果需要在項目中分組顯示,這里也要做相應的設置
s.resource_bundles = {
'PodTestLibrary' => ['Pod/Assets/*.png']
} #資源文件地址
s.public_header_files = 'Pod/Classes/**/*.h' #公開頭文件地址
s.frameworks = 'UIKit' #所需的framework,多個用逗號隔開
s.dependency 'AFNetworking', '~> 2.3' #依賴關系,該項目所依賴的其他庫,如果有多個需要填寫多個s.dependency
end
有了上面的東西,其他的步驟就簡單了:
- 給pod庫代碼打tag,注意要和spec中的tag對應
- 驗證spec文件中的信息正確性(非必須)命令:pod lib lint
- 上傳spec文件到repo specs庫中 命令:pod repo push
上傳命令,官方說明:
Usage:
$ pod repo push REPO [NAME.podspec]
Validates `NAME.podspec` or `*.podspec` in the current working dir,
creates a directory and version folder for the pod in the local copy of
`REPO` (/Users/mac/.cocoapods/repos/[REPO]), copies the podspec file into
the version directory, and finally it pushes `REPO` to its remote.
Options:
--allow-warnings Allows pushing even if
there are warnings
--use-libraries Linter uses static
libraries to install the
spec
--use-modular-headers Lint uses modular headers
during installation
--sources=https://github.com/artsy/Specs,master The sources from which to
pull dependent pods
(defaults to all available
repos). Multiple sources
must be comma-delimited.
--local-only Does not perform the step
of pushing REPO to its
remote
--no-private Lint includes checks that
apply only to public repos
--skip-import-validation Lint skips validating that
the pod can be imported
--skip-tests Lint skips building and
running tests during
validation
--commit-message="Fix bug in pod" Add custom commit message.
Opens default editor if no
commit message is
specified.
--use-json Push JSON spec to repo
--swift-version=VERSION The SWIFT_VERSION that
should be used when
linting the spec. This
takes precedence over a
.swift-version file.
--no-overwrite Disallow pushing that
would overwrite an
existing spec.
--silent Show nothing
--verbose Show more debugging
information
--no-ansi Show output without ANSI
codes
--help Show help banner of
specified command
使用到的一些命令:
1. pod repo add [Private Repo Name] [GitHub HTTPS clone URL]
pod repo add [xxx] https:``//[coding.net/wtlucky/WTSpecs.git](http://coding.net/wtlucky/WTSpecs.git)
將索引倉庫克隆到~/.cocoapod/repo這個目錄下面,所有cocoaPod管理的索引倉庫都在這個下面,
本地倉庫名字可以自定義哈
2、 pod lib create [代碼倉庫名稱]
這個命令是來創建pod代碼倉庫,按照提示一路向西
3、pod lib lint --allow-warnings
用來本地驗證podspec文件是否有效
最好能本地驗一下,盡早發現錯誤,要不然你上傳的時候它會遠程校驗,出錯的話會花費很長時間
。 --allow-warnings忽略警告驗證podspec文件是否有誤。
4、pod spec lint --allow-warnings
遠程驗證,知道就行了,感覺沒必要
5、pod repo push REPO [NAME.podspec]
`$ pod repo push [WTSpecs] [PodTestLibrary.podspec] --allow-warnings ``
前面是本地repo名字 后面是podspec名字,前提是要cd到podspec文件所在的目錄
repo的名字就是你之前自己定義的
備注:
如果我們的pod庫依賴于其他的私有庫,那么在提交的時候需要加入私有庫所在的源:
pod repo push 10101111-ucar_ios_team-specs UCARIntelligenceUnit.podspec --verbose --use-libraries --allow-warnings --sources=``"[http://gitlab.10101111.com:8888/ucar_ios_platform/specs.git,master"](http://gitlab.10101111.com:8888/ucar_ios_platform/specs.git,master)`
遇到的問題
pod search找不到自己trunk push的庫的解決方法
參考地址:https://blog.csdn.net/callzjy/article/details/70171868