001/* 002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved. 003 * 004 * This program and the accompanying materials are made available under the terms of the 005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at 006 * http://www.eclipse.org/legal/epl-v20.html 007 */ 008package net.mdatools.modelant.uml13.maven.plugin.compare; 009 010import java.io.File; 011import java.util.ArrayList; 012import java.util.List; 013 014import javax.jmi.reflect.RefPackage; 015 016import org.apache.maven.plugin.AbstractMojo; 017import org.apache.maven.plugin.MojoExecutionException; 018import org.apache.maven.plugins.annotations.Execute; 019import org.apache.maven.plugins.annotations.LifecyclePhase; 020import org.apache.maven.plugins.annotations.Mojo; 021import org.apache.maven.plugins.annotations.Parameter; 022 023import net.mdatools.modelant.core.api.diff.Export; 024import net.mdatools.modelant.core.api.diff.ModelComparisonResult; 025import net.mdatools.modelant.core.api.match.ConsideredEqual; 026import net.mdatools.modelant.core.operation.model.CompareModels; 027import net.mdatools.modelant.repository.api.ModelFactory; 028import net.mdatools.modelant.repository.api.ModelRepository; 029import net.mdatools.modelant.repository.api.ModelRepositoryFactory; 030import net.mdatools.modelant.uml13.metamodel.CompareUml13Models; 031 032/** 033 * Compare two UML 1.3 models and report the differences, that are needed to convert the source model 034 * into the target one. 035 * @author Rusi Popov (popovr@mdatools.net) 036 */ 037@Mojo(name="compare-uml13-models", 038defaultPhase=LifecyclePhase.COMPILE 039) 040@Execute(phase=LifecyclePhase.COMPILE) 041public class CompareModelsMojo extends AbstractMojo { 042 043 /** 044 * The name of the file with the source (original) model 045 */ 046 @Parameter(property="project.build.sourceDirectory", required=true) 047 private File sourceModel; 048 049 /** 050 * The name of the file with the target (changed) model 051 */ 052 @Parameter(required=true) 053 private File targetModel; 054 055 /** 056 * The directory where the temporary internal files are located 057 */ 058 @Parameter(property="project.build.directory", required=true) 059 private File workDirectory; 060 061 /** 062 * Pairs of <metaclass>, <metapackage> as pairs of source and target metamodel classes, 063 * that should be considered equal. 064 */ 065 @Parameter 066 private List<ConsideredEqual> equals; 067 068 /** 069 * The mechanism to export the result of models comparison. Default: print the string representation 070 */ 071 @Parameter 072 private Export export = Export.DEFAULT; 073 074 public void execute() throws MojoExecutionException { 075 ModelRepository repository; 076 ModelFactory metamodelFactory; 077 CompareModels compare; 078 RefPackage sourceExtent; 079 RefPackage targetExtent; 080 ModelComparisonResult result; 081 List<ConsideredEqual> bindings; 082 083 // lookup the implementation in the plugin's classpath, as it should be a dependency of this plugin 084 repository = ModelRepositoryFactory.construct(workDirectory); 085 try { 086 metamodelFactory = repository.loadMetamodel("UML13"); 087 sourceExtent = metamodelFactory.instantiate("SOURCE"); 088 targetExtent = metamodelFactory.instantiate("TARGET"); 089 090 repository.readIntoExtent( sourceExtent, sourceModel ); 091 repository.readIntoExtent( targetExtent, targetModel ); 092 093 if (equals==null) { 094 bindings = new ArrayList<>(); 095 } else { 096 bindings = equals; 097 } 098 compare = new CompareUml13Models(bindings, sourceExtent); 099 100 result = compare.execute( targetExtent ); 101 102 export.export( result ); 103 104 } catch (Exception ex) { 105 throw new MojoExecutionException( "The target failed with:", ex); 106 107 } finally { 108 repository.shutdown(); 109 } 110 } 111}