xcode 命令行中的Xcode“构建和存档”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2664885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:21:31  来源:igfitidea点击:

Xcode "Build and Archive" from command line

xcodecommand-linebuild

提问by Dan Fabulich

Xcode 3.2 provides an awesome new feature under the Build menu, "Build and Archive" which generates an .ipa file suitable for Ad Hoc distribution. You can also open the Organizer, go to "Archived Applications," and "Submit Application to iTunesConnect."

Xcode 3.2 在 Build 菜单下提供了一个很棒的新功能,“Build and Archive”,它生成一个适合 Ad Hoc 分发的 .ipa 文件。您还可以打开管理器,转到“存档的应用程序”和“将应用程序提交到 iTunesConnect”。

Is there a way to use "Build and Archive" from the command line (as part of a build script)? I'd assume that xcodebuildwould be involved somehow, but the manpage doesn't seem to say anything about this.

有没有办法从命令行使用“构建和存档”(作为构建脚本的一部分)?我认为这xcodebuild会以某种方式参与其中,但该man页面似乎没有对此进行任何说明。

UPDATEMichael Grinich requested clarification; here's what exactly you can't do with command-line builds, features you can ONLY do with Xcode's Organizer after you "Build and Archive."

更新Michael Grinich 要求澄清;以下是命令行构建无法实现的功能,只有在“构建和存档”后才能使用 Xcode 的管理器实现的功能。

  1. You can click "Share Application..." to share your IPA with beta testers. As Guillaume points out below, due to some Xcode magic, this IPA file does not require a separately distributed .mobileprovision file that beta testers need to install; that's magical. No command-line script can do it. For example, Arrix's script (submitted May 1) does not meet that requirement.
  2. More importantly, after you've beta tested a build, you can click "Submit Application to iTunes Connect" to submit that EXACT same build to Apple, the very binary you tested, without rebuilding it. That's impossible from the command line, because signing the app is part of the build process; you can sign bits for Ad Hoc beta testing OR you can sign them for submission to the App Store, but not both. No IPA built on the command-line can be beta tested on phones and then submitted directly to Apple.
  1. 您可以单击“共享应用程序...”与 beta 测试人员共享您的 IPA。正如 Guillaume 在下面指出的,由于 Xcode 的一些魔法,这个 IPA 文件不需要 Beta 测试人员需要安装的单独分发的 .mobileprovision 文件;这很神奇。没有命令行脚本可以做到这一点。例如,Arrix 的脚本(5 月 1 日提交)不符合该要求。
  2. 更重要的是,在您对构建进行 Beta 测试后,您可以单击“将应用程序提交到 iTunes Connect”将完全相同的构建提交给 Apple,即您测试的二进制文件,而无需重新构建它。从命令行这是不可能的,因为签署应用程序是构建过程的一部分;您可以为 Ad Hoc beta 测试签名,也可以签名以提交到 App Store,但不能同时签名。任何基于命令行构建的 IPA 都不能在手机上进行 Beta 测试,然后直接提交给 Apple。

I'd love for someone to come along and prove me wrong: both of these features work great in the Xcode GUI and cannot be replicated from the command line.

我很想有人来证明我错了:这两个功能在 Xcode GUI 中都很好用,不能从命令行复制。

采纳答案by vdaubry

I found how to automate the build and archive process from the comand line, I just wrote a blog articleexplaining how you can achieve that.

我找到了如何从命令行自动化构建和存档过程,我刚刚写了一篇博客文章解释了如何实现这一点。

The command you have to use is xcrun:

您必须使用的命令是xcrun

/usr/bin/xcrun -sdk iphoneos PackageApplication \
-v "${RELEASE_BUILDDIR}/${APPLICATION_NAME}.app" \
-o "${BUILD_HISTORY_DIR}/${APPLICATION_NAME}.ipa" \
--sign "${DEVELOPER_NAME}" \
--embed "${PROVISONING_PROFILE}"

You will find all the details in the article. If you have any questions dont hesitate to ask.

您将在文章中找到所有详细信息。如果您有任何问题,请随时提出。

回答by Reid Ellis

With Xcode 4.2 you can use the -scheme flag to do this:

使用 Xcode 4.2,您可以使用 -scheme 标志来执行此操作:

xcodebuild -scheme <SchemeName> archive

After this command the Archive will show up in the Xcode Organizer.

执行此命令后,存档将显示在 Xcode 管理器中。

回答by Raj

Updating my answer with Xcode 9 and Swift

用 Xcode 9 和 Swift 更新我的答案

Archive

档案

xcodebuild -workspace <ProjectName>/<ProjectName>.xcworkspace \
  -scheme <schemeName> clean archive -configuration release \
  -sdk iphoneos -archivePath <ProjectName>.xcarchive

IPA Export(please note the export option plist)

IPA 导出请注意导出选项 plist

xcodebuild -exportArchive -archivePath  <ProjectName>.xcarchive \
  -exportOptionsPlist  <ProjectName>/exportOptions.plist \
  -exportPath  <ProjectName>.ipa


For those who don't know about exportOptions.plist, https://blog.bitrise.io/new-export-options-plist-in-xcode-9

对于那些不了解 exportOptions.plist 的人,https://blog.bitrise.io/new-export-options-plist-in-xcode-9



Those who were using this for building project in CI/CD tools like teamcity/jenkins, please make sure you are using the right xcode installed in the build agent for for both archive and export.

那些使用它在诸如 teamcity/jenkins 之类的 CI/CD 工具中构建项目的人,请确保您使用的是构建代理中安装的正确 xcode,用于存档和导出。

You can use either of below 2 options for this.

为此,您可以使用以下 2 个选项之一。

  1. Use the full path to xcodebuild,
  1. 使用 xcodebuild 的完整路径,
/Applications/Xcode 9.3.1.app/Contents/Developer/usr/bin/xcodebuild
  1. Use xcode-select,
  1. 使用 xcode-select,
xcode-select -switch /Applications/Xcode 9.3.1.app


Below is my old answer

下面是我的旧答案

Here is command line script for creating archive and IPA example. I have an iPhone xcode project , which is located in Desktop/MyiOSApp folder.

这是用于创建存档和 IPA 示例的命令行脚本。我有一个 iPhone xcode 项目,它位于 Desktop/MyiOSApp 文件夹中。

Execute following commands one by one:

一一执行以下命令:

cd /Users/username/Desktop/MyiOSApp/

xcodebuild -scheme MyiOSApp archive \
  -archivePath /Users/username/Desktop/MyiOSApp.xcarchive

xcodebuild -exportArchive -exportFormat ipa \
  -archivePath "/Users/username/Desktop/MyiOSApp.xcarchive" \
  -exportPath "/Users/username/Desktop/MyiOSApp.ipa" \
  -exportProvisioningProfile "MyCompany Distribution Profile"

This is tested with Xcode 5 and working fine for me.

这是用 Xcode 5 测试的,对我来说工作正常。

回答by Arrix

I've been using my own build script to generate the ipa package for ad hoc distribution.

我一直在使用我自己的构建脚本来生成临时分发的 ipa 包。

die() {
    echo "$*" >&2
    exit 1
}

appname='AppName'
config='Ad Hoc Distribution'
sdk='iphoneos3.1.3'
project_dir=$(pwd)

echo using configuration $config

echo updating version number
agvtool bump -all
fullversion="$(agvtool mvers -terse1)($(agvtool vers -terse))"
echo building version $fullversion

xcodebuild -activetarget -configuration "$config" -sdk $sdk build || die "build failed"

echo making ipa...
# packaging
cd build/"$config"-iphoneos || die "no such directory"
rm -rf Payload
rm -f "$appname".*.ipa
mkdir Payload
cp -Rp "$appname.app" Payload/
if [ -f "$project_dir"/iTunesArtwork ] ; then
    cp -f "$project_dir"/iTunesArtwork Payload/iTunesArtwork
fi

ipaname="$appname.$fullversion.$(date -u +%Y%m%d%H%M%S).ipa"
zip -r $ipaname Payload

echo finished making $ipaname

The script also increment the version number. You can remove that part if it's not needed. Hope it helps.

该脚本还会增加版本号。如果不需要,您可以删除该部分。希望能帮助到你。

回答by Nik

The xcodebuildtool can build and export archive products with the -exportArchiveflag (as of Xcode 5). The export step was previously only possible via the Xcode Organizer UI.

xcodebuild工具可以使用-exportArchive标志(从 Xcode 5 开始)构建和导出存档产品。导出步骤以前只能通过 Xcode Organizer UI 实现。

First archive your app:

首先归档您的应用程序:

xcodebuild -scheme <scheme name> archive

Given $ARCHIVE_PATH(the path to the .xcarchivefile), export the app from the archive with one of the following:

给定$ARCHIVE_PATH.xcarchive文件的路径),使用以下方法之一从存档中导出应用程序:

iOS .ipafile:

iOS .ipa文件:

xcodebuild -exportArchive -exportFormat ipa -archivePath "$ARCHIVE_PATH" -exportPath "myApp.ipa" -exportProvisioningProfile "My App Provisioning profile"

Mac .appfile:

Mac .app文件:

xcodebuild -exportArchive -exportFormat app -archivePath "$ARCHIVE_PATH" -exportPath "myApp.app" -exportSigningIdentity "Developer ID Application: My Software Company"

In both commands the -exportProvisioningProfileand -exportSigningIdentityarguments are optional. man xcodebuildfor details on the semantics. In these examples, the provisioning profile for the iOS build specified an AdHoc distribution provisioning profile, and the signing identity for the Mac app specified a Developer ID for export as a 3rd party application (i.e. not distributed via the Mac App Store).

在这两个命令中,-exportProvisioningProfile-exportSigningIdentity参数都是可选的。man xcodebuild有关语义的详细信息。在这些示例中,iOS 版本的供应配置文件指定了一个 AdHoc 分发供应配置文件,Mac 应用程序的签名标识指定了一个开发人员 ID,用于作为第 3 方应用程序导出(即不通过 Mac App Store 分发)。

回答by FuePi

We developed an iPad app with XCode 4.2.1 and wanted to integrate the build into our continuous integration (Jenkins) for OTA distribution. Here's the solution I came up with:

我们使用 XCode 4.2.1 开发了一个 iPad 应用程序,并希望将构建集成到我们的持续集成 (Jenkins) 中以进行 OTA 分发。这是我想出的解决方案:

# Unlock keychain
security unlock-keychain -p jenkins /Users/jenkins/Library/Keychains/login.keychain

# Build and sign app
xcodebuild -configuration Distribution clean build

# Set variables
APP_PATH="$PWD/build/Distribution-iphoneos/iPadApp.app"
VERSION=`defaults read $APP_PATH/Info CFBundleShortVersionString`
REVISION=`defaults read $APP_PATH/Info CFBundleVersion`
DATE=`date +"%Y%m%d-%H%M%S"`
ITUNES_LINK="<a href=\"itms-services:\/\/?action=download-manifest\&url=https:\/\/xxx.xxx.xxx\/iPadApp-$VERSION.$REVISION-$DATE.plist\">Download iPad2-App v$VERSION.$REVISION-$DATE<\/a>"

# Package and verify app
xcrun -sdk iphoneos PackageApplication -v build/Distribution-iphoneos/iPadApp.app -o $PWD/iPadApp-$VERSION.$REVISION-$DATE.ipa

# Create plist
cat iPadApp.plist.template | sed -e "s/${VERSION}/$VERSION/" -e "s/${DATE}/$DATE/" -e "s/${REVISION}/$REVISION/" > iPadApp-$VERSION.$REVISION-$DATE.plist

# Update index.html
curl https://xxx.xxx.xxx/index.html -o index.html.$DATE
cat index.html.$DATE | sed -n '1h;1!H;${;g;s/\(<h3>Aktuelle Version<\/h3>\)\(.*\)\(<h3>&Auml;ltere Versionen<\/h3>.<ul>.<li>\)/\
${ITUNES_LINK}\
<\/li>\
<li>/g;p;}' | sed -e "s/${ITUNES_LINK}/$ITUNES_LINK/" > index.html

Then Jenkins uploads the ipa, plist and html files to our webserver.

然后 Jenkins 将 ipa、plist 和 html 文件上传到我们的网络服务器。

This is the plist template:

这是 plist 模板:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>items</key>
    <array>
        <dict>
            <key>assets</key>
            <array>
                <dict>
                    <key>kind</key>
                    <string>software-package</string>
                    <key>url</key>
                    <string>https://xxx.xxx.xxx/iPadApp-${VERSION}.${REVISION}-${DATE}.ipa</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>full-size-image</string>
                    <key>needs-shine</key>
                    <true/>
                    <key>url</key>
                    <string>https://xxx.xxx.xxx/iPadApp.png</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>display-image</string>
                    <key>needs-shine</key>
                    <true/>
                    <key>url</key>
                    <string>https://xxx.xxx.xxx/iPadApp_sm.png</string>
                </dict>
            </array>
            <key>metadata</key>
            <dict>
                <key>bundle-identifier</key>
                <string>xxx.xxx.xxx.iPadApp</string>
                <key>bundle-version</key>
                <string>${VERSION}</string>
                <key>kind</key>
                <string>software</string>
                <key>subtitle</key>
                <string>iPad2-App</string>
                <key>title</key>
                <string>iPadApp</string>
            </dict>
        </dict>
    </array>
</dict>
</plist>

To set this up, you have to import the distribution certificate and provisioning profile into the designated user's keychain.

要进行此设置,您必须将分发证书和配置文件导入到指定用户的钥匙串中。

回答by zekel

I found some of the other answers here hard to get going. This articledid if for me. Some paths may need to be absolute, as mentioned in the other answers.

我发现这里的其他一些答案很难开始。这篇文章对我来说确实如此。如其他答案中所述,某些路径可能需要是绝对路径。

The Command:

命令:

xcrun -sdk iphoneos PackageApplication \
    "/path/to/build/MyApp.app" \
    -o "output/path/to/MyApp.ipa" \
    --sign "iPhone Distribution: My Company" \
    --embed "/path/to/something.mobileprovision"

回答by Duane Fields

You CAN actually resign a build, just as XCode does, so that you can test and ship the same binary. For example in my script (similar to those above) I build my release version signed as an AdHoc build, then I archive that as an IPA for testing, then resign with my distribution cert and create a zip file, which is what I send to Apple. The relevant line is:

实际上,您可以像 XCode 一样放弃构建,以便您可以测试和发布相同的二进制文件。例如,在我的脚本中(类似于上面的那些),我构建了我的发布版本,签名为 AdHoc 构建,然后我将其存档为 IPA 进行测试,然后使用我的分发证书辞职并创建一个 zip 文件,这是我发送到的苹果。相关行是:

codesign -f -vv -s "$DistributionIdentity" "$APPDIR"

回答by Teja Swaroop

I have given a brief description of steps to follow, and parameters to pass while generating an ipa using terrminal below:

我已经简要描述了要遵循的步骤,以及使用下面的终端生成 ipa 时要传递的参数:

  1. Go to the folder which contains the MyApp.xcodeproject file in terminal

  2. By using the command given below you will get all the Targets of the application

    /usr/bin/xcodebuild -list 
    
  3. After the above command is executed, you will get a list of targets of which you should select a specific target you need to generate .ipa

    /usr/bin/xcodebuild -target $TARGET -sdk iphoneos -configuration Release
    
  4. The above command builds the project and creates a .app file.The path to locate the .app file is ./build/Release-iphoneos/MyApp.app

  5. After Build gets succeeded then execute the following command to generate .ipa of the application using Developer Name and Provisioning Profile using the syntax below:

    /usr/bin/xcrun -sdk iphoneos PackageApplication -v “${TARGET}.app” -o “${OUTDIR}/${TARGET}.ipa” –sign “${IDENTITY}” –embed “${PROVISONING_PROFILE}”
    
  1. 转到终端中包含 MyApp.xcodeproject 文件的文件夹

  2. 通过使用下面给出的命令,您将获得应用程序的所有目标

    /usr/bin/xcodebuild -list 
    
  3. 执行上述命令后,您将获得一个目标列表,您应该从中选择一个需要生成 .ipa 的特定目标

    /usr/bin/xcodebuild -target $TARGET -sdk iphoneos -configuration Release
    
  4. 上面的命令构建项目并创建一个.app文件。定位.app文件的路径是 ./build/Release-iphoneos/MyApp.app

  5. 构建成功后,执行以下命令以使用以下语法使用开发人员名称和供应配置文件生成应用程序的 .ipa:

    /usr/bin/xcrun -sdk iphoneos PackageApplication -v “${TARGET}.app” -o “${OUTDIR}/${TARGET}.ipa” –sign “${IDENTITY}” –embed “${PROVISONING_PROFILE}”
    

Explanation of each Parameter in the above syntax:

上述语法中每个参数的解释:

${TARGET}.app                == Target path (ex :/Users/XXXXXX/desktop/Application/build/Release-iphoneos/MyApp.app)
${OUTDIR}                    == Select the output directory(Where you want to save .ipa file)
${IDENTITY}                   == iPhone Developer: XXXXXXX (XXXXXXXXXX)(which can be obtained from Keychain access)
${PROVISONING_PROFILE}   == Path to the provisioning profile(/Users/XXXXXX/Library/MobileDevice/Provisioning Profiles/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.mobileprovision”)
  1. ipa will be generated at selected output directory "${OUTDIR}"
  1. ipa 将在选定的输出目录“ ${OUTDIR}”中生成

回答by DawnSong

For Xcode 7, you have a much simpler solution. The only extra work is that you have to create a configuration plist file for exporting archive.

对于Xcode 7,您有一个更简单的解决方案。唯一的额外工作是您必须创建一个用于导出存档的配置 plist 文件。

(Compared to Xcode 6, in the results of xcrun xcodebuild -help, -exportFormatand -exportProvisioningProfileoptions are not mentioned any more; the former is deleted, and the latter is superseded by -exportOptionsPlist.)

(与Xcode 6相比,在结果中不再提及xcrun xcodebuild -help-exportFormat-exportProvisioningProfile选项;前者被删除,后者被取代-exportOptionsPlist。)

Step 1, change directory to the folder including .xcodeproject or .xcworkspace file.

步骤 1,将目录更改为包含 .xcodeproject 或 .xcworkspace 文件的文件夹。

cd MyProjectFolder

Step 2, use Xcode or /usr/libexec/PlistBuddy exportOptions.plistto create export options plist file. By the way, xcrun xcodebuild -helpwill tell you what keys you have to insert to the plist file.

第 2 步,使用 Xcode 或/usr/libexec/PlistBuddy exportOptions.plist创建导出选项 plist 文件。顺便xcrun xcodebuild -help说一下,会告诉您必须将哪些键插入到 plist 文件中。

Step 3, create .xcarchive file (folder, in fact) as follows(build/ directory will be automatically created by Xcode right now),

第3步,创建.xcarchive文件(实际上是文件夹)如下(现在Xcode会自动创建build/目录),

xcrun xcodebuild -scheme MyApp -configuration Release archive -archivePath build/MyApp.xcarchive

Step 4, export as .ipa file like this, which differs from Xcode6

第 4 步,像这样导出为 .ipa 文件,这与 Xcode6 不同

xcrun xcodebuild -exportArchive -exportPath build/ -archivePath build/MyApp.xcarchive/ -exportOptionsPlist exportOptions.plist

Now, you get an ipa file in build/ directory. Just send it to apple App Store.

现在,您将在 build/ 目录中获得一个 ipa 文件。只需将其发送到Apple App Store。

By the way, the ipa file created by Xcode 7 is much larger than by Xcode 6.

顺便说一句,Xcode 7 创建的 ipa 文件比 Xcode 6 大得多。