美文网首页
[Cocoapods] 如何发布一个私有Pod

[Cocoapods] 如何发布一个私有Pod

作者: little_ma | 来源:发表于2017-08-16 14:08 被阅读245次

    发布私有CocoaPod Spec

    创建private pod主要步骤

    1、创建并设置一个私有的Spec Repo。
    
    2、创建Pod的所需要的项目工程文件,并且有可访问的项目版本控制地址。
    
    3、编辑podspec文件。
    
    4、podspec认证。
    
    5、向私有的Spec Repo中提交podspec。
    
    6、在个人项目中的Podfile中添加此pod。
    
    7、更新维护podspec。
    

    创建私有Spec Repo

    1、创建git仓库及Spec Repo
    先来说第一步,什么是Spec Repo?它是所有的Pods的一个索引,就是一个容器,所有公开的Pods都在这个里面,它实际是一个Git仓库remote端在GitHub上,但是当你使用了Cocoapods后它会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到master文件夹就是这个官方的Spec Repo了。为了发布我们自己私有的Spec,我们需要创建一个类似于master的私有Spec Repo。首先创建一个 Git仓库:
    [http://192.168.xx.xx:8080/Pods/Specs.git]

    2、添加私有仓库
    在Terminal中执行如下命令:
    pod repo add [Private Specs Name] [GitHub HTTPS clone URL]
    pod repo add Specs http://192.168.xx.xx:8080/Pods/Specs.git

    此时如果成功的话进入到~/.cocoapods/repos目录下就可以看到gr_repos这个目录了。至此第一步创建私有Spec Repo完成。

    创建Pod项目工程文件

    使用Cocoapods进行创建,相关的文档介绍是Using Pod Lib Create。
    以QCNetworking为例

    $ pod lib create QCNetworking
    

    然后terminal中将出现以下问题,可参考下面答案:

    ! Before you can create a new library we need to setup your git credentials.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    
     What is your email?
    
    What language do you want to use?? [ Swift / ObjC ]
     > ObjC
    
    Would you like to include a demo application with your library? [ Yes / No ]
     > Yes
    
    Which testing frameworks will you use? [ Specta / Kiwi / None ]
     > None
    
    Would you like to do view based testing? [ Yes / No ]
     > No
    
    What is your class prefix?
     > QC
    

    选择完这5个问题,自动执行pod install命令创建项目并生成依赖。
    执行 tree -L 2 (http://coderlt.coding.me/2016/03/16/mac-osx-tree/)

    $ tree -L 2 
    .
    └── QCNetworking
        ├── Example
        ├── LICENSE
        ├── README.md
        ├── QCNetworking
        ├── QCNetworking.podspec
        └── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
    
    4 directories, 3 files
    

    通过Cocoapods创建出来的目录本身就在本地的Git管理下,我们需要做的就是给它添加远端仓库:

    git add .
    git commit -s -m "Initial Commit of Library"
    git remote add origin http://192.168.xx.xx:8080/pods/QCNetworking.git  #添加远端仓库
    git push origin master    #提交到远端仓库
    

    修改podspec文件

    参考CocoaPods podspec官方文档,自定义自己的podspec

    #
    # Be sure to run `pod lib lint XYUserLoggerKit.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             = 'QCNetworking'
      s.version          = '0.1.0'
      s.summary          = 'A short description of QCNetworking.'
    
    # This description is used to generate tags and improve search results.
    #   * Think: What does it do? Why did you write it? What is the focus?
    #   * Try to keep it short, snappy and to the point.
    #   * Write the description between the DESC delimiters below.
    #   * Finally, don't worry about the indent, CocoaPods strips it!
    
      s.description      = <<-DESC
    TODO: 在这里添加此仓库的描述信息.
                           DESC
    
      s.homepage         = '添加此仓库的地址'
      # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
      s.author           = { 'lbrsilva-allin' => 'qc' }
      s.source           = { :git => 'https://github.com/lbrsilva-allin/QCNetworking.git', :tag => s.version.to_s }
      # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    
      s.ios.deployment_target = '8.0'
    
      s.source_files = 'QCNetworking/Classes/**/*'
      
      # s.resource_bundles = {
      #   'XYUserLoggerKit' => ['QCNetworking/Assets/*.png']
      # }
    
      # s.public_header_files = 'Pod/Classes/**/*.h'
      # s.frameworks = 'UIKit', 'MapKit'
      # s.dependency 'AFNetworking', '~> 2.3'
    

    一般文件需要更新的s.homepage, s.souce 填写git 地址即可。

    podspec认证

    使用下面pod 命令验证你的 podspec 释放合法

        pod lib lint --allow-warnings --use-libraries --verbose 
    

    如果成功:

     -> QCNetworking (0.1.0)
        - WARN  | summary: The summary is not meaningful.
    
    QCNetworking passed validation.
    

    最后一步,发布私用pod到我们第一步加的仓储

    pod repo push Specs QCNetworking.podspec  --allow-warnings 
    
    pod search QCNetworking
    

    显示如下:

    -> QCNetworking (0.1.0)
       A short description of QCNetworking.
       pod 'QCNetworking', '~> 0.1.0'
       - Homepage: 
       - Source:   
       - Versions: 0.1.0 [Specs repo]
    (END)
    

    注:
    1、认证依赖是有仓库时需添加私有仓库的源,在
    pod lib lint --allow-warnings --use-libraries --verbose
    后添加:
    --sources='http://192.168.xx.xx:8080/Pods/Specs.git,https://github.com/CocoaPods/Specs.git'

    2、如果在认证过程中遇到一些坑,可以参考这篇文章:http://www.jianshu.com/p/283584683b0b

    相关文章

      网友评论

          本文标题:[Cocoapods] 如何发布一个私有Pod

          本文链接:https://www.haomeiwen.com/subject/eqevrxtx.html