001: package org.andromda.maven.plugin.bootstrap;
002:
003: import java.io.File;
004: import java.io.FileNotFoundException;
005:
006: import java.net.MalformedURLException;
007: import java.net.URL;
008:
009: import java.util.List;
010:
011: import org.andromda.core.AndroMDA;
012: import org.andromda.core.common.ExceptionUtils;
013: import org.andromda.core.common.ResourceUtils;
014: import org.andromda.core.configuration.Configuration;
015: import org.andromda.core.configuration.Model;
016: import org.andromda.core.configuration.Repository;
017: import org.andromda.maven.plugin.configuration.AbstractConfigurationMojo;
018: import org.apache.maven.artifact.factory.ArtifactFactory;
019: import org.apache.maven.artifact.repository.ArtifactRepository;
020: import org.apache.maven.plugin.MojoExecutionException;
021: import org.apache.maven.project.MavenProject;
022: import org.apache.maven.settings.Settings;
023:
024: /**
025: * This is exactly the same as the regular AndroMDAMojo in the
026: * andromda-maven-plugin, however this is the <em>bootstrap</em> plugin which
027: * is used to run AndroMDA in bootstrap mode (with the bootstrap artifacts).
028: *
029: * @author Chad Brandon
030: * @goal run
031: * @phase generate-sources
032: * @requiresDependencyResolution runtime
033: */
034: public class AndroMDAMojo extends AbstractConfigurationMojo {
035: /**
036: * This is the URI to the AndroMDA configuration file.
037: *
038: * @parameter expression="file:${basedir}/conf/andromda.xml"
039: * @required
040: */
041: private String configurationUri;
042:
043: /**
044: * @parameter expression="${project}"
045: * @required
046: * @readonly
047: */
048: private MavenProject project;
049:
050: /**
051: * @parameter expression="${project.build.filters}"
052: */
053: private List propertyFiles;
054:
055: /**
056: * Whether or not a last modified check should be performed before running
057: * AndroMDA again.
058: *
059: * @parameter expression="false"
060: * @required
061: */
062: private boolean lastModifiedCheck;
063:
064: /**
065: * The directory to which the build source is located (any generated
066: * source).
067: *
068: * @parameter expression="${project.build.directory}/src/main/java"
069: */
070: private String buildSourceDirectory;
071:
072: /**
073: * The current user system settings for use in Maven. (allows us to pass the
074: * user settings to the AndroMDA configuration).
075: *
076: * @parameter expression="${settings}"
077: * @required
078: * @readonly
079: */
080: private Settings settings;
081:
082: /**
083: * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
084: * @required
085: * @readonly
086: */
087: private ArtifactFactory factory;
088:
089: /**
090: * The registered plugin implementations.
091: *
092: * @parameter expression="${project.build.plugins}"
093: * @required
094: * @readonlya
095: */
096: protected List plugins;
097:
098: /**
099: * @parameter expression="${localRepository}"
100: * @required
101: * @readonly
102: */
103: protected ArtifactRepository localRepository;
104:
105: public void execute() throws MojoExecutionException {
106: try {
107: final URL configurationUri = ResourceUtils
108: .toURL(this .configurationUri);
109: if (configurationUri == null) {
110: throw new MojoExecutionException(
111: "Configuration could not be loaded from '"
112: + this .configurationUri + "'");
113: }
114: final Configuration configuration = this
115: .getConfiguration(configurationUri);
116: boolean execute = true;
117: if (this .lastModifiedCheck) {
118: final File directory = new File(
119: this .buildSourceDirectory);
120: execute = ResourceUtils.modifiedAfter(ResourceUtils
121: .getLastModifiedTime(configurationUri),
122: directory);
123: if (!execute) {
124: final Repository[] repositories = configuration
125: .getRepositories();
126: int repositoryCount = repositories.length;
127: for (int ctr = 0; ctr < repositoryCount; ctr++) {
128: final Repository repository = repositories[ctr];
129: if (repository != null) {
130: final Model[] models = repository
131: .getModels();
132: final int modelCount = models.length;
133: for (int ctr2 = 0; ctr2 < modelCount; ctr2++) {
134: final Model model = models[ctr2];
135: execute = ResourceUtils.modifiedAfter(
136: model.getLastModified(),
137: directory);
138: if (execute) {
139: break;
140: }
141: }
142: }
143: }
144: }
145: }
146: if (execute) {
147: this
148: .initializeClasspathFromClassPathElements(this .project
149: .getRuntimeClasspathElements());
150: final AndroMDA andromda = AndroMDA.newInstance();
151: andromda.run(configuration);
152: andromda.shutdown();
153: } else {
154: this
155: .getLog()
156: .info(
157: "Files are up-to-date, skipping AndroMDA execution");
158: }
159: final File buildSourceDirectory = this .buildSourceDirectory != null ? new File(
160: this .buildSourceDirectory)
161: : null;
162: if (buildSourceDirectory != null) {
163: this .getProject().addCompileSourceRoot(
164: buildSourceDirectory.toString());
165: }
166: } catch (Throwable throwable) {
167: String message = "Error running AndroMDA";
168: throwable = ExceptionUtils.getRootCause(throwable);
169: if (throwable instanceof MalformedURLException
170: || throwable instanceof FileNotFoundException) {
171: message = "Configuration is not valid '"
172: + this .configurationUri + "'";
173: }
174: throw new MojoExecutionException(message, throwable);
175: }
176: }
177:
178: /**
179: * @param configurationUri The configurationUri to set.
180: */
181: public void setConfigurationUri(String configurationUri) {
182: this .configurationUri = configurationUri;
183: }
184:
185: /**
186: * @param project The project to set.
187: */
188: public void setProject(MavenProject project) {
189: this .project = project;
190: }
191:
192: /**
193: * Sets the current settings for this Mojo.
194: *
195: * @param settings The settings to set.
196: */
197: public void setSettings(Settings settings) {
198: this .settings = settings;
199: }
200:
201: /**
202: * Sets the property files for this project.
203: *
204: * @param propertyFiles
205: */
206: public void setPropertyFiles(List propertyFiles) {
207: this .propertyFiles = propertyFiles;
208: }
209:
210: /**
211: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getProject()
212: */
213: protected MavenProject getProject() {
214: return this .project;
215: }
216:
217: /**
218: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPropertyFiles()
219: */
220: protected List getPropertyFiles() {
221: return this .propertyFiles;
222: }
223:
224: /**
225: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getSettings()
226: */
227: protected Settings getSettings() {
228: return this .settings;
229: }
230:
231: /**
232: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getFactory()
233: */
234: protected ArtifactFactory getFactory() {
235: return this .factory;
236: }
237:
238: /**
239: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPlugins()
240: */
241: protected List getPlugins() {
242: return this .plugins;
243: }
244:
245: /**
246: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getLocalRepository()
247: */
248: protected ArtifactRepository getLocalRepository() {
249: return this.localRepository;
250: }
251: }
|