Avalonia 打一个dmg包
参考了此篇博客:【亲测免费】 create-dmg 教程:创建精美Mac DMG镜像、还有这篇Avalonia macOS 应用打包指南
跟随本篇操作 可以打出来一个免安装绿色版和dmg(拖拽安装)安装包
在自己的主程序的目录下新增一个xxx.sh脚本,并在文件中粘贴如下内容:
#!/bin/zsh
# ==========================
# 打包配置(只改这里即可)
# ==========================
APP_NAME="LearnAvalonia"
APP_DISPLAY_NAME="测试行情软件"
BUNDLE_ID="com.leranavalonia.client"
VERSION="1.0.0"
DOTNET_SDK_VERSION="net8.0"
PUBLISH_OS_TYPE="osx-arm64"
PUB_DIR="./publish_out"
echo "=====开始构建 macOS Avalonia应用====="
# 1. 清理旧产物
echo "清理旧打包文件..."
rm -rf "${APP_NAME}.app" "${APP_NAME}_Mac.dmg" "${PUB_DIR}" ./create-dmg
# 2. 检查图标
ICON_PATH="./${APP_NAME}.icns"
if [[ ! -f "${ICON_PATH}" ]]; then
echo "❌ 错误:缺失图标 ${ICON_PATH}"
exit 1
fi
echo "恢复项目依赖..."
dotnet restore
echo "发布【单文件自包含】程序..."
# dotnet publish -c Release -r osx-arm64 --self-contained true -p:UseAppHost=true -o ./publish_out
dotnet publish -c Release -f ${DOTNET_SDK_VERSION} -r ${PUBLISH_OS_TYPE} --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:IncludeAllContentForSelfExtract=true -p:PublishTrimmed=true -p:ReadyToRun=true -o ${PUB_DIR}
# 校验发布产物
BIN_PATH="${PUB_DIR}/${APP_NAME}"
if [[ ! -f "${BIN_PATH}" ]]; then
echo "❌ 发布失败:找不到可执行文件 ${BIN_PATH}"
exit 1
fi
echo "创建标准 .app 目录结构..."
mkdir -p "${APP_NAME}.app/Contents/MacOS"
mkdir -p "${APP_NAME}.app/Contents/Resources"
# 复制资源
cp "${ICON_PATH}" "${APP_NAME}.app/Contents/Resources/${APP_NAME}.icns"
cp -r "${PUB_DIR}"/* "${APP_NAME}.app/Contents/MacOS/"
echo "生成完整版Info.plist(兼容全macOS)..."
cat > "${APP_NAME}.app/Contents/Info.plist" << EOF
<?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>CFBundleName</key>
<string>${APP_DISPLAY_NAME}</string>
<key>CFBundleDisplayName</key>
<string>${APP_DISPLAY_NAME}</string>
<key>CFBundleIdentifier</key>
<string>${BUNDLE_ID}</string>
<key>CFBundleExecutable</key>
<string>${APP_NAME}</string>
<!-- 新增图标配置 -->
<key>CFBundleIconFile</key>
<string>${APP_NAME}.icns</string>
<key>CFBundleVersion</key>
<string>${VERSION}</string>
<key>CFBundleShortVersionString</key>
<string>${VERSION}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>LSMinimumSystemVersion</key>
<string>10.15</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2024. All rights reserved.</string>
<!-- Avalonia兼容必填项 -->
<key>LSApplicationCategoryType</key>
<string>public.app-category.finance</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
EOF
# 赋权+临时签名
echo "权限+本机临时签名..."
chmod +x "${APP_NAME}.app/Contents/MacOS/${APP_NAME}"
codesign --force --deep --sign - "${APP_NAME}.app"
codesign --verify --verbose "${APP_NAME}.app"
# 【关键:打包前先清除隔离标记,dmg内部不带隔离属性】
echo "清除隔离属性(解决已损坏)..."
xattr -cr "${APP_NAME}.app"
# 处理create-dmg:优先brew安装,失败再下载
echo "准备create-dmg打包工具..."
if ! command -v create-dmg &> /dev/null; then
if ! brew install create-dmg; then
echo "brew安装失败,在线下载create-dmg"
curl -o create-dmg https://raw.githubusercontent.com/sindresorhus/create-dmg/main/create-dmg
chmod +x create-dmg
DMG_CMD="./create-dmg"
else
DMG_CMD="create-dmg"
fi
else
DMG_CMD="create-dmg"
fi
echo "生成DMG安装包..."
${DMG_CMD} \
--volname "${APP_DISPLAY_NAME}" \
--window-size 520 320 \
--icon-size 100 \
--icon "${APP_NAME}.app" 130 130 \
--app-drop-link 390 130 \
--hide-extension "${APP_NAME}.app" \
"${APP_NAME}_Mac.dmg" ./${APP_NAME}.app
# 清理临时文件
rm -rf "${PUB_DIR}" ./create-dmg
echo "
✅ 打包全部完成!
输出文件:${APP_NAME}_Mac.dmg
"
xxx.sh放置在应用程序项目目录下,另外你还需要为应用程序搞一个icon文件

给xxx.sh设置为可执行chmod +x xxx.sh
执行xxx.sh脚本就可以打出来dmg安装包了~~~

浙公网安备 33010602011771号