1 package org.andromda.maven.plugin;
2
3 import java.io.FileNotFoundException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.List;
8 import org.andromda.core.common.AndroMDALogger;
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.maven.plugin.configuration.AbstractConfigurationMojo;
13 import org.apache.maven.artifact.Artifact;
14 import org.apache.maven.artifact.factory.ArtifactFactory;
15 import org.apache.maven.artifact.repository.ArtifactRepository;
16 import org.apache.maven.model.Plugin;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.plugin.MojoFailureException;
19 import org.apache.maven.project.MavenProject;
20 import org.apache.maven.settings.Settings;
21
22
23
24
25
26
27
28 public abstract class AbstractAndroMDAMojo
29 extends AbstractConfigurationMojo
30 {
31
32
33
34
35
36
37 protected String configurationUri;
38
39
40
41
42
43
44
45
46
47
48 protected boolean allowMultipleRuns;
49
50
51
52
53
54
55 protected MavenProject project;
56
57
58
59
60 protected List<String> propertyFiles;
61
62
63
64
65
66
67
68
69
70 protected Settings settings;
71
72
73
74
75
76
77 protected ArtifactFactory factory;
78
79
80
81
82
83
84
85
86 protected List<Plugin> plugins;
87
88
89
90
91
92
93 protected ArtifactRepository localRepository;
94
95 private static List<String> configurations = new ArrayList<String>();
96
97
98
99
100
101 public void execute()
102 throws MojoExecutionException, MojoFailureException
103 {
104 if (this.allowMultipleRuns || !configurations.contains(this.configurationUri))
105 {
106 try
107 {
108 if (!this.allowMultipleRuns)
109 {
110 configurations.add(this.configurationUri);
111 }
112 AndroMDALogger.initialize();
113 final URL configurationUri = ResourceUtils.toURL(this.configurationUri);
114 if (configurationUri == null)
115 {
116 throw new MojoExecutionException("Configuration could not be loaded from '" + this.configurationUri +
117 '\'');
118 }
119
120 this.addPluginDependencies(
121 Constants.ARTIFACT_ID,
122 Artifact.SCOPE_RUNTIME);
123
124
125
126
127 this.initializeClasspathFromClassPathElements(this.project.getRuntimeClasspathElements());
128 final Configuration configuration = this.getConfiguration(configurationUri);
129 this.execute(configuration);
130 }
131 catch (Throwable throwable)
132 {
133 String message = "Error running AndroMDA";
134 throwable = ExceptionUtils.getRootCause(throwable);
135 if (throwable instanceof MalformedURLException || throwable instanceof FileNotFoundException)
136 {
137 message = "Configuration is not valid '" + this.configurationUri + '\'';
138 }
139 throw new MojoExecutionException(message, throwable);
140 }
141 }
142 }
143
144
145
146
147
148
149
150
151 protected abstract void execute(final Configuration configuration) throws Exception;
152
153
154
155
156 public void setConfigurationUri(String configurationUri)
157 {
158 this.configurationUri = configurationUri;
159 }
160
161
162
163
164 public void setProject(MavenProject project)
165 {
166 this.project = project;
167 }
168
169
170
171
172
173
174 public void setSettings(Settings settings)
175 {
176 this.settings = settings;
177 }
178
179
180
181
182
183
184 public void setPropertyFiles(List<String> propertyFiles)
185 {
186 this.propertyFiles = propertyFiles;
187 }
188
189
190
191
192 protected MavenProject getProject()
193 {
194 return this.project;
195 }
196
197
198
199
200 protected List<String> getPropertyFiles()
201 {
202 return this.propertyFiles;
203 }
204
205
206
207
208 protected Settings getSettings()
209 {
210 return this.settings;
211 }
212
213
214
215
216 protected ArtifactFactory getFactory()
217 {
218 return this.factory;
219 }
220
221
222
223
224 protected List<Plugin> getPlugins()
225 {
226 return this.plugins;
227 }
228
229
230
231
232 protected ArtifactRepository getLocalRepository()
233 {
234 return this.localRepository;
235 }
236 }