美文网首页藤原とうふ店(自家用)
iOS 自动打包工具 -- xcodebuild

iOS 自动打包工具 -- xcodebuild

作者: bonoCat | 来源:发表于2017-06-14 13:06 被阅读510次

    iOS 自动打包工具 -- xcodebuild


    由于公司产品发布需要由运维人员进行负责,所以准备弄一个打包工具,虽然也有一些比较好的工具 shenzhen,但也是基于
    xcodebuild,既然如此,何不自己写一套呢?虽然简单,但也会很实用~

    主要从下面几个方面入手

    • xcodebuild 相关参数
    • xcodebuild 具体使用命令
    • 需要实现的功能:打包、发布

    xcodebuild 参数分析

    基本描述

    xcodebuild -- build Xcode projects and workspaces
    

    通过 xcodebuild 来构建 Xcode 项目。如果当前目录下有多个,可以使用 -project 来指定具体工程,默认情况下构建第一个。
    如果是构建是 workspace 的,就需要使用 -workspace 和 -scheme 这两个命令构建
    使用 -version, -showsdks, and -usage 可以看一些信息

    $ xcodebuild BDLifeManageProject.xcworkspace -version
      Xcode 8.3.2
      Build version 8E2002
    $ xcodebuild BDLifeManageProject.xcworkspace -showsdks
      iOS SDKs:
        iOS 10.3                        -sdk iphoneos10.3
    
    iOS Simulator SDKs:
        Simulator - iOS 10.3            -sdk iphonesimulator10.3
    
    macOS SDKs:
        macOS 10.12                     -sdk macosx10.12
    
    tvOS SDKs:
        tvOS 10.2                       -sdk appletvos10.2
    
    tvOS Simulator SDKs:
        Simulator - tvOS 10.2           -sdk appletvsimulator10.2
    
    watchOS SDKs:
        watchOS 3.2                     -sdk watchos3.2
    
    watchOS Simulator SDKs:
        Simulator - watchOS 3.2         -sdk watchsimulator3.2
    $ xcodebuild BDLifeManageProject.xcworkspace -usage
       Usage:
           xcodebuild [-project <projectname>] [[-target <targetname>]...|-alltargets] [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
           xcodebuild [-project <projectname>] -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
           xcodebuild -workspace <workspacename> -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
           xcodebuild -version [-sdk [<sdkfullpath>|<sdkname>] [<infoitem>] ]
           xcodebuild -list [[-project <projectname>]|[-workspace <workspacename>]] [-json]
           xcodebuild -showsdks
           xcodebuild -exportArchive -archivePath <xcarchivepath> -exportPath <destinationpath> -exportOptionsPlist <plistpath>
           xcodebuild -exportLocalizations -localizationPath <path> -project <projectname> [-exportLanguage <targetlanguage>...]
           xcodebuild -importLocalizations -localizationPath <path> -project <projectname>
    

    可选参数

         -project projectname
               如果在当前目录有多个 project 文件,使用 projectname 来指定
    
         -target targetname
                由于一个 project 可以包含多个 target,所以需要使用 -target 指定
    
         -alltargets
                编译在 project 下面所有的 target
               Build all the targets in the specified project.
    
         -workspace workspacename
                编译 指定的 workspace 
    
         -scheme schemename
               在编译workspace时,需要使用-scheme指定的 scheme 来编译
    
         -destination destinationspecifier
               用于区分目标设备,默认是与当前选中 scheme 匹配的那个
    
         -destination-timeout timeout
               在查找目标设备的时候设置超时时间,默认30秒
    
         -configuration configurationname
                使用 configuration 来编译的时候通过 configurationname 来指定每个 target 的配置  debug ? release
    
         -arch architecture
               在构建每一个 target 的时候,指定架构体系 arm64 armv7 armv7s
    
         -sdk [<sdkfullpath> | <sdkname>]
               编译 Xcode 的 project 或者 workspace 相关的 SDK
    
         -showsdks
               显示出所有 Xcode 中的SDK
    
         -list
               列举出 project 下的 targets 和 configurations,或者 workspace 下的 schemes
    
         -derivedDataPath path
               在 workspace 中 build 时候就会使用这个文件来代替源数据
               覆盖这个文件的话
    
         -resultBundlePath path
         
                在 workspace 中 构建时,将 bundle 写入指定的 地址
    
         -exportArchive
                指定一个被导出的 archive 文件。
                需要和 -exportFormat,-archivePath和-exportPath 配合使用,不能在编译时单独使用。 
    
         -exportFormat format
               指定导出 archive 的形式: IPA(iOS archives
               only)、PKG (Mac archives only)、 APP, 不指定将会尝试使用 IPA 或 PKG
    
         -archivePath xcarchivepath
               指定 archive 路径
         
         -exportPath destinationpath
                指定导出的目标文件路径。 
    
         -exportProvisioningProfile profilename
                导出 archive 时指定 Profile
        
         -exportSigningIdentity identityname
              导出 archive 时指定应用签名,有可能会从 -exportProvisioningProfile 得出
         
         -exportInstallerIdentity identityname
               导出 archive 时使用安装的签名ID
    
         -exportWithOriginalSigningIdentity
              指定创建可被导出的 archive 文件时所使用的签名文件。
    
         buildaction ...
               Specify a build action (or actions) to perform on the target. Available build actions are:
    
               build    默认编译的动作,编译根对象 (SYMROOT)
    
               analyze     编译并且分析根环境(SYMROOT)下的一个 target 或者 scheme。需要指定一个 scheme。 
    
               archive     Archive a scheme from the build root (SYMROOT).  This requires specifying a scheme.
                
               test        Test a scheme from the build root (SYMROOT).  This requires specifying a scheme and
                           optionally a destination.
    
               installsrc  Copy the source of the project to the source root (SRCROOT).
    
               install     Build the target and install it into the target's installation directory in the dis-tribution distribution
                           tribution root (DSTROOT).
    
               clean       Remove build products and intermediate files from the build root (SYMROOT).
    
         -xcconfig filename
               Load the build settings defined in filename when building all targets.  These settings will over-ride override
               ride all other settings, including settings passed individually on the command line.
    
         -dry-run, -n
               Print the commands that would be executed, but do not execute them.
    
         -skipUnavailableActions
               Skip build actions that cannot be performed instead of failing. This option is only honored if
               -scheme is passed.
    
         setting=value
               Set the build setting setting to value.
    
         -userdefault=value
               Set the user default default to value.
    
         -version
               Display version information for this install of Xcode. Does not initiate a build. When used in
               conjunction with -sdk, the version of the specified SDK is displayed, or all SDKs if -sdk is
               given no argument.  Additionally, a single line of the reported version information may be
               returned if infoitem is specified.
    
         -usage
               Displays usage information for xcodebuild.
    
    

    xcodebuild 具体使用命令

    通过官方的 xcodebuild 可以得到我们所需要的命令

    project:

     xcodebuild [-project <projectname>] -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
     xcodebuild -workspace <workspacename> -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
           
    

    由于公司项目配置比较简单,这边将它进行简化,提取出最简单的即可:

    构建工程

     xcodebuild [-project <projectname>] -scheme <schemeName>  [-configuration <configurationname>] [<buildaction>]...
     
     xcodebuild -workspace <workspacename> -scheme <schemeName>  [-configuration <configurationname>] [<buildaction>]...
    

    输出

    
     xcodebuild -exportArchive -archivePath <xcarchivepath> -exportPath <destinationpath> -exportOptionsPlist <plistpath>
    
    

    exportOptionsPlist

    主要包含了一个method,可以设置的值为:ad-hoc 、app-store、development ,根据设置的值,采用不同的证书进行打包

    为了打包方便,使用plist文件进行管理,只包含 export_method 信息

    参数说明下:
    projectname: 为工程名称
    schemeName: 为所选 scheme 的名称
    configurationname: debug 还是 release
    buildaction : 主要使用的是 archive 和 clean

    最终能得到以下命令

    # clean
    xcodebuild -workspace <workspacename> -scheme <schemeName> [-configuration <configurationname>] clean
    
    # archive
        ##project
     xcodebuild [-project <projectname>] -scheme <schemeName>  [-configuration <configurationname>] -archivePath <xcarchivepath>  archive
        ##workspace
     xcodebuild -workspace <workspacename> -scheme <schemeName>  [-configuration <configurationname>] archive
     
    #export
     xcodebuild -exportArchive -archivePath <xcarchivepath> -exportPath <destinationpath> -exportOptionsPlist <plistpath>
    
    

    由于项目是 workspace 的,所以试下 -workspace 情况

    
    xcodebuild -workspace BDLifeManageProject.xcworkspace -scheme BDLifeManageProject -configuration release clean
    
    ** CLEAN SUCCEEDED **
    
     xcodebuild -workspace BDLifeManageProject.xcworkspace -scheme BDLifeManageProject  -configuration release -archivePath /Users/dingding/Desktop/outPath/BDLifeManageProject.xcarchive archive
     
    ** ARCHIVE SUCCEEDED **
    
     xcodebuild -exportArchive -archivePath /Users/dingding/Desktop/outPath/BDLifeManageProject.xcarchive -exportPath /Users/dingding/Desktop/outPath/ -exportOptionsPlist /Users/dingding/Desktop/outPath/DevelopmentExportOptionsPlist.plist
    
    ** EXPORT SUCCEEDED **
    
    
    导出ipa.jpeg

    通过以上 xcodebuild 命令可以实现打包

    最终成品 Autopackage 点此下载

    相关文章

      网友评论

        本文标题:iOS 自动打包工具 -- xcodebuild

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