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
25
26
27
28
29
30
31
32
33 public class AndroMDAMojo
34 extends AbstractConfigurationMojo
35 {
36
37
38
39
40
41
42 private String configurationUri;
43
44
45
46
47
48
49 private MavenProject project;
50
51
52
53
54 private List<String> propertyFiles;
55
56
57
58
59
60
61
62
63 private boolean lastModifiedCheck;
64
65
66
67
68
69
70
71 private File buildSourceDirectory;
72
73
74
75
76
77
78
79 private File modelOutputHistory;
80
81
82
83
84
85
86
87
88
89 private Settings settings;
90
91
92
93
94
95
96 private ArtifactFactory factory;
97
98
99
100
101
102
103
104
105 protected List<Plugin> plugins;
106
107
108
109
110
111
112 protected ArtifactRepository localRepository;
113
114
115
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
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
192
193 public void setConfigurationUri(String configurationUri)
194 {
195 this.configurationUri = configurationUri;
196 }
197
198
199
200
201 public void setProject(MavenProject project)
202 {
203 this.project = project;
204 }
205
206
207
208
209
210
211 public void setSettings(Settings settings)
212 {
213 this.settings = settings;
214 }
215
216
217
218
219
220
221 public void setPropertyFiles(List<String> propertyFiles)
222 {
223 this.propertyFiles = propertyFiles;
224 }
225
226
227
228
229 protected MavenProject getProject()
230 {
231 return this.project;
232 }
233
234
235
236
237 protected List<String> getPropertyFiles()
238 {
239 return this.propertyFiles;
240 }
241
242
243
244
245 protected Settings getSettings()
246 {
247 return this.settings;
248 }
249
250
251
252
253 protected ArtifactFactory getFactory()
254 {
255 return this.factory;
256 }
257
258
259
260
261 protected List<Plugin> getPlugins()
262 {
263 return this.plugins;
264 }
265
266
267
268
269 protected ArtifactRepository getLocalRepository()
270 {
271 return this.localRepository;
272 }
273 }