View Javadoc
1   package org.andromda.maven.plugin.modelarchiver;
2   
3   import java.io.File;
4   import org.apache.commons.io.FileUtils;
5   import org.apache.maven.plugin.MojoExecutionException;
6   
7   /**
8    * Builds archived model uml files.
9    *
10   * @author Bob Fields
11   * @version $Id: $
12   * @goal profile.uml
13   * @phase package
14   * @description builds a versioned uml profile
15   */
16  public class UmlProfileArchiverMojo
17          extends BaseArchiveMojo
18  {
19      private static final String ARTIFACT_TYPE = "profile.uml";
20  
21      /**
22       * The extensions to search for when doing replacement of embedded model HREF references
23       * within the archived model file from non-versioned to versioned references.
24       *
25       * @parameter expression=".profile.uml"
26       * @required
27       */
28      protected String replacementExtensions;
29  
30      /**
31       * The pattern of the model file(s) that should be versioned.
32       *
33       * @parameter expression=".*(\\.profile\\.uml)"
34       * @required
35       * @readonly
36       */
37      private String modelFilePattern;
38  
39      /**
40       * <p>execute</p>
41       *
42       * @throws org.apache.maven.plugin.MojoExecutionException
43       *          if any.
44       * @see org.apache.maven.plugin.Mojo#execute()
45       */
46      public void execute()
47              throws MojoExecutionException
48      {
49          if (getLog().isDebugEnabled())
50          {
51              getLog().debug(" ======= UmlProfileArchiverMojo settings =======");
52              getLog().debug("modelSourceDirectory[" + modelSourceDirectory + ']');
53              getLog().debug("workDirectory[" + workDirectory + ']');
54              getLog().debug("outputDirectory[" + outputDirectory + ']');
55              getLog().debug("finalName[" + finalName + ']');
56              getLog().debug("replaceExtensions[" + replaceExtensions + ']');
57          }
58  
59          try
60          {
61              final File buildDirectory = this.workDirectory;
62              if (buildDirectory.exists())
63              {   // old files in directory are not automatically deleted.
64                  deleteFiles(buildDirectory.getAbsolutePath(), ARTIFACT_TYPE);
65              }
66              else
67              {
68                  buildDirectory.mkdirs();
69              }
70  
71              if (modelSourceDirectory.exists())
72              {
73                  getLog().info("Copy " + ARTIFACT_TYPE + " resources to " + buildDirectory.getAbsolutePath());
74                  final File[] modelFiles = modelSourceDirectory.listFiles();
75                  for (final File file : modelFiles)
76                  {
77                      if (file.isFile() && file.toString().matches(this.modelFilePattern))
78                      {
79                          final File newFile =
80                                  new File(buildDirectory,
81                                          this.finalName + '.' + ARTIFACT_TYPE);
82                          FileUtils.copyFile(file, newFile);
83  
84                          if (replaceExtensions)
85                          {
86                              getLog().info("Replace extensions in " + newFile);
87                              replaceExtensions(this.replacementExtensions, newFile);
88                          }
89  
90                          setArtifactFile(newFile);
91                      }
92                  }
93              }
94          }
95          catch (final Throwable throwable)
96          {
97              throw new MojoExecutionException("Error assembling model", throwable);
98          }
99      }
100 }