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