Xcodebuild簡介
Xcodebuild是命令行工具包的其中一項。
命令行工具包(Command Line Tools)是一個輕量的、可以與XCode分開的、在Mac上單獨下載的命令行工具包。
它有兩部分組成:Mac OS SDK和用戶系統庫目錄/usr/bin下的諸多命令工具。例如:gcc/g++編譯器,make、git、nase、xcodebuild、xcrun等等。
命令行工具包(Command Line Tools)的安裝
Xcode-select命令
xcode-select是Mac系統自帶的命令行工具,屬於用戶系統內/usr/bin。當電腦上安裝多個Xcode時,xcode-select用來選擇命令行工具為哪一個版本的Xcode服務。
命令行工具安裝指令:xcode-select --install
選擇指定的Xcode路徑:xcode-select --switch <path>
常見命令
man命令
man可以進行命令用法的在線文檔查詢,包括使用例子。比如:man xcodebuild。
xcpretty命令
命令行輸出美化小工具,可以對錯誤,警告給予高亮顯示。使用方式:xcodebuild | xcpretty
xcrun命令
xcrun用於調用其他命令執行,如:xcrun xcodebuild。
xcrun的調用是基於xcode-select選擇的工具鏈,當電腦中存在多個版本的xcode時,使用xcrun調用可以保證命令的環境一致性。
xcodebuild命令
下面重點介紹xcodebuild如何使用。
在使用xcodebuild時,從終端進入到projectname .xcodeproj 目錄下。
有workspace時,參數中要帶-workspace和-scheme。
只有project時,則參數中要帶 -project和-scheme選項。
xcodebuild在Xcode中存在的默認配置在路徑project/info頁面中

xcodebuild的常見使用場景
簡單命令行build
xcodebuild
單寫一個xcodebuild,工程編譯使用默認的scheme和編譯配置。
scheme、targets、配置、目標設備、SDK和導出數據位置可以在后面自定義配置
archive打包操作
xcodebuild archive -workspace PROJECT_NAME.xcworkspace -scheme SCHEME_NAME -configuration release -archivePath EXPORT_ARCHIVE_PATH -archivePath:設置項目的歸檔路徑
導出ipa文件
xcodebuild -exportArchive -archivePath EXPORT_ARCHIVE_PATH -exportPath EXPORT_IPA_PATH -exportOptionsPlist ExportOptionsPlistPath -allowProvisioningUpdates -exportArchive:導出ipa -exportPath:導出ipa文件的路徑 -exportOptionsPlist:文件導出時的配置信息 -allowProvisioningUpdates:允許xcodebuild與蘋果網站通訊,進行自動簽名,證書自動更新,生成。
單元測試
xcodebuild test -project PROJECT_NAME.xcodeproj -scheme SCHEME_NAME -destination 'platform=iOS Simulator,name=iPhone 6s,OS=11.2' - configuration Debug -derivedDataPath output -derivedDataPath:產生的緩存文件放在./output目錄下 configuration:編譯環境,選擇Debug/Release -destination :選擇test時的目標設備和系統版本號
UI測試/單元測試,針對某個方法進行測試
xcodebuild test -project PROJECT_NAME.xcodeproj -scheme SCHEME_NAME -destination 'platform=iOS Simulator,name=iPhone 6s,OS=11.2' -only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME -quiet -only-testing: 只測試某一個方法,target名/類名/方法名 -quiet : 除了錯誤和警告不打印任何信息
使用上次編譯成功的測試用例進行測試
注意:app創建時需要指定app的bundle名
self.app = [[XCUIApplication alloc] initWithBundleIdentifier:@"com.xxx.id"]; [self.app launch];
1.UI測試/單元測試,不進行代碼編譯,利用上次編譯的緩存(包括工程編譯+測試用例編譯),進行重新跑測試。
xcodebuild test-without-building -workspace PROJECT_NAME.xcworkspace -scheme doctor -destination 'platform=iOS Simulator,name=iPhone 6s,OS=12.0' -only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME
2.UI測試,使用選項-xctestrun生產測試文件,進行測試調試
//1.產生xctestrun文件 xcodebuild build-for-testing -project PROJECT_NAME.xcodeproj -scheme SCHEME_NAME -destination 'platform=iOS Simulator,name=iPhone 6s,OS=11.2' - configuration Debug -derivedDataPath output -derivedDataPath: derivedDataPath/Build/Products目錄下生成一個.xctestrun文件,包含測試信息 //2.使用xctestrun文件(不帶-workspace/-project/-scheme參數) xcodebuild test-without-building -destination 'platform=iOS Simulator,name=iPhone 6s,OS=12.0' -xctestrun DerivedDataPath.xctestrun -only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME -xctestrun:有這個選項就從指定的路徑下尋找bundle,沒有這個選項在derivedDataPath下尋找bundle -only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME
xcodebuild常見action

另外一些常見的命令
genstrings 命令
本地化命令,根據指定的C/Object-C源文件生成.strings文件。
genstrings -a /path/to/source/files/*.m
ibtool 命令
本地化命令,作用於xib文件。
ibtool --generate-strings-file Localizable.strings en.lpoj/Interface.xib
文章參考: