View Javadoc
1   package org.andromda.maven.plugin.bootstrap;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.net.MalformedURLException;
6   import java.net.URL;
7   import java.util.List;
8   import org.andromda.core.AndroMDA;
9   import org.andromda.core.common.ExceptionUtils;
10  import org.andromda.core.common.ResourceUtils;
11  import org.andromda.core.configuration.Configuration;
12  import org.andromda.core.configuration.Model;
13  import org.andromda.core.configuration.Repository;
14  import org.andromda.maven.plugin.configuration.AbstractConfigurationMojo;
15  import org.apache.maven.artifact.factory.ArtifactFactory;
16  import org.apache.maven.artifact.repository.ArtifactRepository;
17  import org.apache.maven.model.Plugin;
18  import org.apache.maven.plugin.MojoExecutionException;
19  import org.apache.maven.project.MavenProject;
20  import org.apache.maven.settings.Settings;
21  
22  
23  /**
24   * This is exactly the same as the regular AndroMDAMojo in the
25   * andromda-maven-plugin, however this is the <em>bootstrap</em> plugin which
26   * is used to run AndroMDA in bootstrap mode (with the bootstrap artifacts).
27   *
28   * @author Chad Brandon
29   * @goal run
30   * @phase generate-sources
31   * @requiresDependencyResolution runtime
32   */
33  public class AndroMDAMojo
34      extends AbstractConfigurationMojo
35  {
36      /**
37       * This is the URI to the AndroMDA configuration file.
38       *
39       * @parameter expression="file:${basedir}/conf/andromda.xml"
40       * @required
41       */
42      private String configurationUri;
43  
44      /**
45       * @parameter expression="${project}"
46       * @required
47       * @readonly
48       */
49      private MavenProject project;
50  
51      /**
52       * @parameter expression="${project.build.filters}"
53       */
54      private List<String> propertyFiles;
55  
56      /**
57       * Whether or not a last modified check should be performed before running
58       * AndroMDA again.
59       *
60       * @parameter expression="false"
61       * @required
62       */
63      private boolean lastModifiedCheck;
64  
65      /**
66       * The directory to which the build source is located (any generated
67       * source).
68       *
69       * @parameter expression="${project.build.directory}/src/main/java"
70       */
71      private File buildSourceDirectory;
72  
73      /**
74       * The directory where the model generation output history is located
75       * (Modelname file containing a list of files generated by that model).
76       *
77       * @parameter expression="${project.build.directory}/history"
78       */
79      private File modelOutputHistory;
80  
81      /**
82       * The current user system settings for use in Maven. (allows us to pass the
83       * user settings to the AndroMDA configuration).
84       *
85       * @parameter expression="${settings}"
86       * @required
87       * @readonly
88       */
89      private Settings settings;
90  
91      /**
92       * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
93       * @required
94       * @readonly
95       */
96      private ArtifactFactory factory;
97  
98      /**
99       * The registered plugin implementations.
100      *
101      * @parameter expression="${project.build.plugins}"
102      * @required
103      * @readonly
104      */
105     protected List<Plugin> plugins;
106 
107     /**
108      * @parameter expression="${localRepository}"
109      * @required
110      * @readonly
111      */
112     protected ArtifactRepository localRepository;
113 
114     /**
115      * @see org.apache.maven.plugin.Mojo#execute()
116      */
117     public void execute()
118         throws MojoExecutionException
119     {
120         try
121         {
122             final URL configurationUri = ResourceUtils.toURL(this.configurationUri);
123             if (configurationUri == null)
124             {
125                 throw new MojoExecutionException("Configuration could not be loaded from '" + this.configurationUri +
126                         '\'');
127             }
128             final Configuration configuration = this.getConfiguration(configurationUri);
129             boolean execute = true;
130             if (this.lastModifiedCheck)
131             {
132                 execute = ResourceUtils.modifiedAfter(
133                         ResourceUtils.getLastModifiedTime(configurationUri),
134                         this.buildSourceDirectory);
135                 if (!execute)
136                 {
137                     //TODO This duplicates functionality in ModelProcessor. It also doesn't work if files are generated to multiple different project locations.
138                     final Repository[] repositories = configuration.getRepositories();
139                     int repositoryCount = repositories.length;
140                     for (int ctr = 0; ctr < repositoryCount; ctr++)
141                     {
142                         final Repository repository = repositories[ctr];
143                         if (repository != null)
144                         {
145                             final Model[] models = repository.getModels();
146                             final int modelCount = models.length;
147                             for (int ctr2 = 0; ctr2 < modelCount; ctr2++)
148                             {
149                                 final Model model = models[ctr2];
150                                 execute = ResourceUtils.modifiedAfter(
151                                         model.getLastModified(),
152                                         this.buildSourceDirectory);
153                                 if (execute)
154                                 {
155                                     break;
156                                 }
157                             }
158                         }
159                     }
160                 }
161             }
162             if (execute)
163             {
164                 this.initializeClasspathFromClassPathElements(this.project.getRuntimeClasspathElements());
165                 final AndroMDA andromda = AndroMDA.newInstance();
166                 andromda.run(configuration, lastModifiedCheck, this.modelOutputHistory.getAbsolutePath());
167                 andromda.shutdown();
168             }
169             else
170             {
171                 this.getLog().info("Files are up-to-date, skipping AndroMDA execution");
172             }
173             if (this.buildSourceDirectory != null)
174             {
175                 this.getProject().addCompileSourceRoot(this.buildSourceDirectory.toString());
176             }
177         }
178         catch (Throwable throwable)
179         {
180             String message = "Error running AndroMDA";
181             throwable = ExceptionUtils.getRootCause(throwable);
182             if (throwable instanceof MalformedURLException || throwable instanceof FileNotFoundException)
183             {
184                 message = "Configuration is not valid '" + this.configurationUri + '\'';
185             }
186             throw new MojoExecutionException(message, throwable);
187         }
188     }
189 
190     /**
191      * @param configurationUri The configurationUri to set.
192      */
193     public void setConfigurationUri(String configurationUri)
194     {
195         this.configurationUri = configurationUri;
196     }
197 
198     /**
199      * @param project The project to set.
200      */
201     public void setProject(MavenProject project)
202     {
203         this.project = project;
204     }
205 
206     /**
207      * Sets the current settings for this Mojo.
208      *
209      * @param settings The settings to set.
210      */
211     public void setSettings(Settings settings)
212     {
213         this.settings = settings;
214     }
215 
216     /**
217      * Sets the property files for this project.
218      *
219      * @param propertyFiles
220      */
221     public void setPropertyFiles(List<String> propertyFiles)
222     {
223         this.propertyFiles = propertyFiles;
224     }
225 
226     /**
227      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getProject()
228      */
229     protected MavenProject getProject()
230     {
231         return this.project;
232     }
233 
234     /**
235      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPropertyFiles()
236      */
237     protected List<String> getPropertyFiles()
238     {
239         return this.propertyFiles;
240     }
241 
242     /**
243      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getSettings()
244      */
245     protected Settings getSettings()
246     {
247         return this.settings;
248     }
249 
250     /**
251      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getFactory()
252      */
253     protected ArtifactFactory getFactory()
254     {
255         return this.factory;
256     }
257 
258     /**
259      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPlugins()
260      */
261     protected List<Plugin> getPlugins()
262     {
263         return this.plugins;
264     }
265 
266     /**
267      * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getLocalRepository()
268      */
269     protected ArtifactRepository getLocalRepository()
270     {
271         return this.localRepository;
272     }
273 }