// 执行 DeployCmd#execute 方法publicDeploymentexecute(CommandContextcommandContext){// 执行部署returnexecuteDeploy(commandContext);}protectedDeploymentexecuteDeploy(CommandContextcommandContext){// DeploymentEntity 表示部署, 里面包含了流程文件DeploymentEntitynewDeployment=setUpNewDeploymentFromContext(commandContext);// 判断是否过滤重复的// 每次部署流程,可能只有一部分的流程发生了改变,所以不需要部署所有的流程if(deploymentBuilder.isDuplicateFilterEnabled()){...if(!existingDeployments.isEmpty()){DeploymentEntityexistingDeployment=(DeploymentEntity)existingDeployments.get(0);// 对比流程文件是否发生改动if(deploymentsDiffer(newDeployment,existingDeployment)){applyUpgradeLogic(newDeployment,existingDeployment);}else{LOGGER.info("An existing deployment of version {} matching the current one was found, no need to deploy again.",existingDeployment.getVersion());returnexistingDeployment;}}}// 持久化部署,会把流程文件插入到数据库中,也会返回 deploymentId 和 versionpersistDeploymentInDatabase(commandContext,newDeployment);...LOGGER.info("Launching new deployment with version: "+newDeployment.getVersion());// 部署流程commandContext.getProcessEngineConfiguration().getDeploymentManager().deploy(newDeployment,deploymentSettings);...returnnewDeployment;}
// 部署流程@Overridepublicvoiddeploy(DeploymentEntitydeployment,Map<String,Object>deploymentSettings){log.debug("Processing deployment {}",deployment.getName());// The ParsedDeployment represents the deployment, the process definitions, and the BPMN// resource, parse, and model associated with each process definition.// 这里会解析流程文件,很重要, 会在下一节继续解析ParsedDeploymentparsedDeployment=parsedDeploymentBuilderFactory.getBuilderForDeploymentAndSettings(deployment,deploymentSettings).build();// 校验 processDefinitionKey 是否重复bpmnDeploymentHelper.verifyProcessDefinitionsDoNotShareKeys(parsedDeployment.getAllProcessDefinitions());...// 设置一些属性,然后会持久化到数据库中if(deployment.isNew()){Map<ProcessDefinitionEntity,ProcessDefinitionEntity>mapOfNewProcessDefinitionToPreviousVersion=getPreviousVersionsOfProcessDefinitions(parsedDeployment);setProcessDefinitionVersionsAndIds(parsedDeployment,mapOfNewProcessDefinitionToPreviousVersion);setProcessDefinitionAppVersion(parsedDeployment);persistProcessDefinitionsAndAuthorizations(parsedDeployment);updateTimersAndEvents(parsedDeployment,mapOfNewProcessDefinitionToPreviousVersion);dispatchProcessDefinitionEntityInitializedEvent(parsedDeployment);}else{makeProcessDefinitionsConsistentWithPersistedVersions(parsedDeployment);}// 流程定义更新到缓存中cachingAndArtifactsManager.updateCachingAndArtifacts(parsedDeployment);// 这里不是主要逻辑,可以不用关心for(ProcessDefinitionEntityprocessDefinition:parsedDeployment.getAllProcessDefinitions()){BpmnModelbpmnModel=parsedDeployment.getBpmnModelForProcessDefinition(processDefinition);createLocalizationValues(processDefinition.getId(),bpmnModel.getProcessById(processDefinition.getKey()));}}