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.commons.io.FilenameUtils;
6   import org.apache.maven.plugin.MojoExecutionException;
7   
8   /**
9    * Builds archived model xml files.
10   *
11   * @author Bob Fields
12   * @version $Id: $
13   * @goal xml
14   * @phase package
15   * @description builds a versioned xml
16   */
17  public class XmlArchiverMojo
18          extends BaseArchiveMojo
19  {
20      private static final String ARTIFACT_TYPE = "xml";
21  
22      /**
23       * The pattern of the model file(s) that should be versioned.
24       *
25       * @parameter expression=".*(\\.xml)"
26       * @required
27       * @readonly
28       */
29      private String modelFilePattern;
30  
31      /**
32       * <p>execute</p>
33       *
34       * @throws org.apache.maven.plugin.MojoExecutionException
35       *          if any.
36       * @see org.apache.maven.plugin.Mojo#execute()
37       */
38      public void execute()
39              throws MojoExecutionException
40      {
41          if (getLog().isDebugEnabled())
42          {
43              getLog().debug(" ======= XmlArchiverMojo settings =======");
44              getLog().debug("modelSourceDirectory[" + modelSourceDirectory + ']');
45              getLog().debug("workDirectory[" + workDirectory + ']');
46              getLog().debug("outputDirectory[" + outputDirectory + ']');
47              getLog().debug("finalName[" + finalName + ']');
48          }
49  
50          try
51          {
52              final File buildDirectory = this.workDirectory;
53              if (buildDirectory.exists())
54              {   // old files in directory are not automatically deleted.
55                  deleteFiles(buildDirectory.getAbsolutePath(), ARTIFACT_TYPE);
56              }
57              else
58              {
59                  buildDirectory.mkdirs();
60              }
61  
62              //final String[] replacementExtensions =
63              //    this.replacementExtensions != null ? this.replacementExtensions.split(",\\s*") : new String[0];
64              if (modelSourceDirectory.exists())
65              {
66                  final File[] modelFiles = modelSourceDirectory.listFiles();
67                  for (final File file : modelFiles)
68                  {
69                      if (file.isFile() && file.toString().matches(this.modelFilePattern))
70                      {
71                          final File newFile =
72                                  new File(buildDirectory,
73                                          this.finalName + '.' + FilenameUtils.getExtension(file.getName()));
74                          getLog().info("File " + file + " copied  to " + newFile);
75                          FileUtils.copyFile(file, newFile);
76  
77                          String contents = FileUtils.readFileToString(newFile);
78                          final String version = escapePattern(this.project.getVersion());
79                          /*if (replaceExtensions)
80                         {
81                             for (int ctr3 = 0; ctr3 < replacementExtensions.length; ctr3++)
82                             {
83                                 final String extension = escapePattern(replacementExtensions[ctr3]);
84                                 final String extensionPattern = "((\\-" + version + ")?)" + extension;
85                                 final String newExtension = "\\-" + version + extension;
86                                 contents = contents.replaceAll(
87                                         extensionPattern,
88                                         newExtension); */
89                          // Fix replacement error for standard UML profiles which follow the _Profile. naming convention.
90                          contents =
91                                  contents.replaceAll(
92                                          "_Profile\\-" + version,
93                                          "_Profile");
94                          /*}
95                          }*/
96                          FileUtils.writeStringToFile(newFile, contents);
97  
98                          setArtifactFile(newFile);
99                      }
100                 }
101             }
102         }
103         catch (final Throwable throwable)
104         {
105             throw new MojoExecutionException("Error assembling model", throwable);
106         }
107     }
108 }