001: package org.andromda.maven.plugin.modelarchiver;
002:
003: import java.io.File;
004: import java.io.FileReader;
005: import java.io.FileWriter;
006:
007: import org.apache.commons.io.IOUtils;
008: import org.apache.commons.lang.StringUtils;
009: import org.apache.maven.artifact.Artifact;
010: import org.apache.maven.artifact.repository.ArtifactRepository;
011: import org.apache.maven.plugin.AbstractMojo;
012: import org.apache.maven.plugin.MojoExecutionException;
013: import org.apache.maven.project.MavenProject;
014: import org.codehaus.plexus.archiver.manager.ArchiverManager;
015: import org.codehaus.plexus.util.FileUtils;
016:
017: /**
018: * Builds archived model uml2 files.
019: *
020: * @author Chad Brandon
021: * @goal uml2
022: * @phase package
023: * @requiresProject
024: * @description builds a versioned uml2
025: */
026: public class Uml2ArchiverMojo extends AbstractMojo {
027: /**
028: * Single directory that contains the model
029: *
030: * @parameter expression="${basedir}/src/main/uml"
031: * @required
032: */
033: private File modelSourceDirectory;
034:
035: /**
036: * Directory that resources are copied to during the build.
037: *
038: * @parameter expression="${project.build.directory}"
039: * @required
040: */
041: private String workDirectory;
042:
043: /**
044: * The directory for the generated uml2.
045: *
046: * @parameter expression="${project.build.outputDirectory}"
047: * @required
048: */
049: private String outputDirectory;
050:
051: /**
052: * The name of the uml2 file to generate.
053: *
054: * @parameter alias="modelName" expression="${project.build.finalName}"
055: * @required
056: * @readonly
057: */
058: private String finalName;
059:
060: /**
061: * The maven project.
062: *
063: * @parameter expression="${project}"
064: * @required
065: * @readonly
066: * @description "the maven project to use"
067: */
068: private MavenProject project;
069:
070: /**
071: * To look up Archiver/UnArchiver implementations
072: *
073: * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
074: * @required
075: */
076: protected ArchiverManager archiverManager;
077:
078: /**
079: * The extensions to search for when doing replacement of embedded model HREF references
080: * within the archived model file from non-versioned to versioned references.
081: *
082: * @parameter expression=".uml2"
083: * @required
084: */
085: protected String replacementExtensions;
086:
087: /**
088: * Whether or not to do replacement of embedded model HREF reference extensions.
089: *
090: * @parameter expression=false
091: * @required
092: */
093: protected boolean replaceExtensions;
094:
095: /**
096: * The pattern of the model file(s) that should be versioned.
097: *
098: * @parameter expression=".*(\\.uml2)"
099: * @required
100: * @readonly
101: */
102: private String modelFilePattern;
103:
104: /**
105: * @parameter expression="${localRepository}"
106: * @required
107: * @readonly
108: */
109: protected ArtifactRepository localRepository;
110:
111: public void execute() throws MojoExecutionException {
112: getLog().debug(" ======= Uml2ArchiverMojo settings =======");
113: getLog().debug(
114: "modelSourceDirectory[" + modelSourceDirectory + "]");
115: getLog().debug("workDirectory[" + workDirectory + "]");
116: getLog().debug("outputDirectory[" + outputDirectory + "]");
117: getLog().debug("finalName[" + finalName + "]");
118: getLog().debug("replaceExtensions[" + replaceExtensions + "]");
119:
120: try {
121: // - the directory which to extract the model file
122: final File modelExtractDirectory = new File(
123: this .workDirectory, "models/xmi");
124: modelExtractDirectory.mkdirs();
125: final File buildDirectory = new File(this .workDirectory);
126:
127: final File modelSourceDir = modelSourceDirectory;
128: final String[] replacementExtensions = this .replacementExtensions != null ? this .replacementExtensions
129: .split(",\\s*")
130: : new String[0];
131: if (modelSourceDir.exists()) {
132: getLog().info(
133: "Copy uml2 resources to "
134: + buildDirectory.getAbsolutePath());
135: final File[] modelFiles = modelSourceDir.listFiles();
136: for (int ctr = 0; ctr < modelFiles.length; ctr++) {
137: final File file = modelFiles[ctr];
138: if (file.isFile()
139: && file.toString().matches(
140: this .modelFilePattern)) {
141: FileUtils.copyFileToDirectory(file,
142: modelExtractDirectory);
143:
144: // - copy the model to the extract directory
145: final File[] extractedModelFiles = modelExtractDirectory
146: .listFiles();
147: for (int ctr2 = 0; ctr2 < extractedModelFiles.length; ctr2++) {
148: final File extractedFile = extractedModelFiles[ctr2];
149: final String extractedFilePath = extractedFile
150: .toString();
151: if (extractedFile.isFile()
152: && extractedFilePath
153: .matches(this .modelFilePattern)) {
154: final File newFile = new File(
155: buildDirectory,
156: this .finalName
157: + '.'
158: + FileUtils
159: .getExtension(extractedFile
160: .toString()));
161: extractedFile.renameTo(newFile);
162: String contents = IOUtils
163: .toString(new FileReader(
164: newFile));
165: if (replaceExtensions) {
166: for (int ctr3 = 0; ctr3 < replacementExtensions.length; ctr3++) {
167: final String version = escapePattern(this .project
168: .getVersion());
169: final String extension = escapePattern(replacementExtensions[ctr3]);
170: final String extensionPattern = "((\\-"
171: + version
172: + ")?)"
173: + extension;
174: final String newExtension = "\\-"
175: + version + extension;
176: contents = contents.replaceAll(
177: extensionPattern,
178: newExtension);
179: }
180: }
181: final FileWriter fileWriter = new FileWriter(
182: newFile);
183: fileWriter.write(contents);
184: fileWriter.flush();
185: }
186: }
187: }
188: }
189: }
190:
191: final File uml2File = new File(buildDirectory,
192: this .finalName + ".uml2");
193:
194: final Artifact artifact = this .project.getArtifact();
195:
196: // - set the artifact file back to the correct file (since we've installed modelJar already)
197: artifact.setFile(uml2File);
198: } catch (final Throwable throwable) {
199: throw new MojoExecutionException("Error assembling model",
200: throwable);
201: }
202: }
203:
204: /**
205: * Escapes the pattern so that the reserved regular expression
206: * characters are used literally.
207: * @param pattern the pattern to replace.
208: * @return the resulting pattern.
209: */
210: private static String escapePattern(String pattern) {
211: pattern = StringUtils.replace(pattern, ".", "\\.");
212: pattern = StringUtils.replace(pattern, "-", "\\-");
213: return pattern;
214: }
215: }
|