需要在鸿蒙项目中自定义构建脚本
根目录hvigorfile.ts,参考代码:
import { hvigor, HvigorNode, HvigorPlugin } from '@ohos/hvigor';
import { appTasks, hapTasks,OhosHapContext,OhosHarContext, OhosAppContext, OhosPluginId, Target } from '@ohos/hvigor-ohos-plugin';
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
context() {
return {
data: 'customPlugin test'
};
},
async apply(currentNode: HvigorNode): Promise<void> {
hvigor.nodesEvaluated(async () => {
console.log("=========自定义插件开始=========")
customTask(currentNode);
console.log("=========自定义插件结束=========")
});
}
};
}
function customTask(currentNode: HvigorNode) {
const appContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
//设置版本号
const appJson5: any = appContext.getAppJsonOpt();
const buildMode = appContext?.getBuildMode();
if (buildMode === 'debug') {
appJson5['app']['versionName'] = appJson5['app']['versionName']+'-debug';
} else {
appJson5['app']['versionName'] = appJson5['app']['versionName']+'-release';
}
appContext.setAppJsonOpt(appJson5);
const currentProduct = appContext?.getCurrentProduct();
//获取命令行-p参数
const extParams = hvigor.getParameter().getExtParams();
console.log("参数:"+JSON.stringify(extParams));
const extraData = extParams["extraData"]
currentNode.subNodes((node: HvigorNode) => {
console.log("子节点:"+node.getNodeName())
const ids:String[] = node.getAllPluginIds()
console.log("插件:"+JSON.stringify(ids))
//获取hap的context
const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
if (!hapContext) {
return;
}
const moduleName = hapContext?.getModuleName();
console.log('hapContext:'+moduleName)
let buildProfileOpt:any = hapContext.getBuildProfileOpt()
if(!buildProfileOpt){
buildProfileOpt = new Object();
}
//参数中存在extraData,直接覆盖本地配置,参数中不存在,不作处理,直接走本地build-profile.json5中的["buildOption"]["arkOptions"]["buildProfileFields"]参数
if (extraData) {
if(!buildProfileOpt["buildOption"]){
buildProfileOpt["buildOption"] = new Object();
}
if(!buildProfileOpt["buildOption"]["arkOptions"]){
buildProfileOpt["buildOption"]["arkOptions"] = new Object();
}
if (!buildProfileOpt["buildOption"]["arkOptions"]["buildProfileFields"]) {
buildProfileOpt["buildOption"]["arkOptions"]["buildProfileFields"] = new Object();
}
buildProfileOpt["buildOption"]["arkOptions"]["buildProfileFields"]["extraData"] = extraData
hapContext.setBuildProfileOpt(buildProfileOpt)
}
});
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
HAP模块hvigorfile.ts
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}
HAR模块hvigorfile.ts
import { harTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}
jenkins打包脚本参考:
#!/bin/bash
# 检查是否传递了目录参数
if [ -z "$1" ]; then
echo "使用方法: $0 <项目目录> [extraData]"
exit 1
fi
PROJECT_DIR="$1"
EXTRA_DATA="$2"
# 切换到指定目录
echo "切换到项目目录:$1"
if ! cd "$PROJECT_DIR"; then
echo "无法切换到目录 $PROJECT_DIR"
exit 1
fi
# 安装依赖
echo "执行依赖安装..."
if ! ohpm install --all; then
echo "依赖安装失败"
exit 1
fi
# 清理项目
echo "执行代码审查..."
# 代码检查
if ! codelinter; then
echo "代码检查失败"
exit 1
fi
# 清理项目
echo "正在清理项目..."
if ! hvigorw clean --no-daemon; then
echo "清理项目失败"
exit 1
fi
# 构建项目
echo "正在构建项目..."
if [ -n "$EXTRA_DATA" ]; then
echo "使用 extraData: $EXTRA_DATA"
if ! hvigorw assembleHap --mode module -p product=default -p buildMode=debug -p extraData="$EXTRA_DATA" --no-daemon; then
echo "构建项目失败"
exit 1
fi
else
if ! hvigorw assembleHap --mode module -p product=default -p buildMode=debug --no-daemon; then
echo "构建项目失败"
exit 1
fi
fi
echo "项目构建完成"
jenkins中调用打包脚本:
sh ./ohos_package.sh ${WORKSPACE} "$extraData"