View Javadoc
1   package org.andromda.maven.plugin.modelarchiver;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.maven.artifact.Artifact;
6   import org.apache.maven.plugin.AbstractMojo;
7   import org.apache.maven.project.MavenProject;
8   import org.apache.maven.project.MavenProjectHelper;
9   import org.codehaus.plexus.archiver.manager.ArchiverManager;
10  
11  import java.io.File;
12  import java.io.IOException;
13  import java.util.Iterator;
14  
15  /**
16   * Base class for all ArchiverMojos
17   *
18   * @author Plushnikov Michail
19   * @version $Id: $
20   * @since 3.4
21   */
22  public abstract class BaseArchiveMojo extends AbstractMojo
23  {
24      /**
25       * Single directory that contains the model
26       *
27       * @parameter expression="${basedir}/src/main/uml"
28       * @required
29       */
30      protected File modelSourceDirectory;
31      /**
32       * Directory that resources are copied to during the build.
33       *
34       * @parameter expression="${project.build.directory}"
35       * @required
36       */
37      protected File workDirectory;
38      /**
39       * The directory for the generated emx.
40       *
41       * @parameter expression="${project.build.outputDirectory}"
42       * @required
43       */
44      protected File outputDirectory;
45      /**
46       * The name of the emx file to generate.
47       *
48       * @parameter alias="modelName" expression="${project.build.finalName}"
49       * @required
50       * @readonly
51       */
52      protected String finalName;
53      /**
54       * The maven project.
55       *
56       * @parameter expression="${project}"
57       * @required
58       * @readonly
59       * @description "the maven project to use"
60       */
61      protected MavenProject project;
62  
63      /**
64       * To look up Archiver/UnArchiver implementations
65       *
66       * @component role="org.codehaus.plexus.archiver.manager.ArchiverManager"
67       * @required
68       */
69      protected ArchiverManager archiverManager;
70  
71      /**
72       * The maven project's helper.
73       *
74       * @component role="org.apache.maven.project.MavenProjectHelper"
75       * @required
76       * @readonly
77       */
78      protected MavenProjectHelper projectHelper;
79  
80      /**
81       * Whether or not to do replacement of embedded model HREF reference extensions.
82       *
83       * @parameter expression=false
84       * @required
85       */
86      protected boolean replaceExtensions;
87  
88      /**
89       * Deletes all files with given extension in the given directory
90       *
91       * @param pPath      path to directory
92       * @param pExtension extension of files
93       */
94      protected void deleteFiles(String pPath, String pExtension)
95      {
96          Iterator<File> lFileIter = FileUtils.iterateFiles(new File(pPath), new String[]{pExtension}, false);
97          while (lFileIter.hasNext())
98          {
99              FileUtils.deleteQuietly(lFileIter.next());
100         }
101     }
102 
103     /**
104      * Escapes the pattern so that the reserved regular expression
105      * characters are used literally.
106      *
107      * @param pattern the pattern to replace.
108      * @return the resulting pattern.
109      */
110     protected String escapePattern(String pattern)
111     {
112         pattern = StringUtils.replace(
113                 pattern,
114                 ".",
115                 "\\.");
116         pattern = StringUtils.replace(
117                 pattern,
118                 "-",
119                 "\\-");
120         return pattern;
121     }
122 
123     /**
124      * Replace all extensions in the file
125      *
126      * @param pReplacementExtensions Extensions to replace
127      * @param pFile                  File where all Extensions should be replaced
128      * @throws java.io.IOException Exception on IO-Error
129      */
130     protected void replaceExtensions(String pReplacementExtensions, File pFile) throws IOException
131     {
132         String[] replacementExtensions = pReplacementExtensions != null ? pReplacementExtensions.split(",\\s*") : new String[0];
133 
134         final String version = escapePattern(this.project.getVersion());
135         String contents = FileUtils.readFileToString(pFile);
136         for (String replacementExtension : replacementExtensions)
137         {
138             final String extension = escapePattern(replacementExtension);
139             // add ' to extension, to match only href elements (example: href='abcdefg.xml')
140             // and not abc.xml.efg substrings
141             final String extensionPattern = "((\\-" + version + ")?)" + extension + "(['\"|])";
142             final String newExtension = "\\-" + version + extension + "$3";
143             contents = contents.replaceAll(
144                     extensionPattern,
145                     newExtension);
146             // Fix replacement error for standard UML profiles which follow the _Profile. naming convention.
147             contents =
148                     contents.replaceAll(
149                             "_Profile\\-" + version,
150                             "_Profile");
151         }
152         FileUtils.writeStringToFile(pFile, contents);
153     }
154 
155     /**
156      * Sets File for current Artifact
157      * @param newFile File to set for current Artifact
158      */
159     protected void setArtifactFile(File newFile)
160     {
161         final Artifact artifact = this.project.getArtifact();
162         // - set the artifact file to the correct file
163         artifact.setFile(newFile);
164         getLog().debug("File artifact set " + newFile.getAbsolutePath() + " packaging " + project.getPackaging());
165     }
166 }