在模块级hvigorfile.ts里:使用下述代码可以实现构建的产物hap重命名。import { hapTasks, OhosHapContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin' import { getNode } from '@ohos/hvigor' import { hvigor } from '@ohos/hvigor' const entryNode = getNode(__filename) const appContext = hvigor.getRootNode().getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext const appJsonOpt = appContext.getAppJsonOpt() const versionName = appJsonOpt['app']['versionName'] entryNode.afterNodeEvaluate(node => { const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext if (hapContext && hapContext.getBuildProfileOpt) { const buildProfile = hapContext.getBuildProfileOpt() const product = buildProfile.targets[0] product['output'] = { "artifactName": "xxx-" + versionName + '_' + getTime() + '_' + appContext.getBuildMode(), } hapContext.setBuildProfileOpt(buildProfile) } }) function getTime(): string { let date = new Date() let year = date.getFullYear() let month = (date.getMonth() + 1).toString().padStart(2, '0') let day = date.getDate().toString().padStart(2, '0') let hours = date.getHours().toString().padStart(2, '0') let minutes = date.getMinutes().toString().padStart(2, '0') return `${year}-${month}-${day}_${hours}_${minutes}` } export default { system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ plugins: [] /* Custom plugin to extend the functionality of Hvigor. */ }
在模块级hvigorfile.ts里:使用下述代码可以实现构建的产物hap重命名。