001: package org.andromda.maven.plugin;
002:
003: import java.io.FileNotFoundException;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.util.List;
007:
008: import org.andromda.core.common.AndroMDALogger;
009: import org.andromda.core.common.ExceptionUtils;
010: import org.andromda.core.common.ResourceUtils;
011: import org.andromda.core.configuration.Configuration;
012: import org.andromda.maven.plugin.configuration.AbstractConfigurationMojo;
013: import org.apache.maven.artifact.Artifact;
014: import org.apache.maven.artifact.factory.ArtifactFactory;
015: import org.apache.maven.artifact.repository.ArtifactRepository;
016: import org.apache.maven.plugin.MojoExecutionException;
017: import org.apache.maven.plugin.MojoFailureException;
018: import org.apache.maven.project.MavenProject;
019: import org.apache.maven.settings.Settings;
020:
021: /**
022: * The abstract AndroMDA Mojo. This should be extended
023: * by the Mojos that are used to run AndroMDA.
024: *
025: * @author Chad Brandon
026: */
027: public abstract class AbstractAndroMDAMojo extends
028: AbstractConfigurationMojo {
029: /**
030: * This is the URI to the AndroMDA configuration file.
031: *
032: * @parameter expression="file:${project.basedir}/conf/andromda.xml"
033: * @required
034: */
035: protected String configurationUri;
036:
037: /**
038: * @parameter expression="${project}"
039: * @required
040: * @readonly
041: */
042: protected MavenProject project;
043:
044: /**
045: * @parameter expression="${project.build.filters}"
046: */
047: protected List propertyFiles;
048:
049: /**
050: * The current user system settings for use in Maven. (allows us to pass the user
051: * settings to the AndroMDA configuration).
052: *
053: * @parameter expression="${settings}"
054: * @required
055: * @readonly
056: */
057: protected Settings settings;
058:
059: /**
060: * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
061: * @required
062: * @readonly
063: */
064: protected ArtifactFactory factory;
065:
066: /**
067: * The registered plugin implementations.
068: *
069: * @parameter expression="${project.build.plugins}"
070: * @required
071: * @readonlya
072: */
073: protected List plugins;
074:
075: /**
076: * @parameter expression="${localRepository}"
077: * @required
078: * @readonly
079: */
080: protected ArtifactRepository localRepository;
081:
082: /**
083: * @see org.apache.maven.plugin.Mojo#execute()
084: */
085: public void execute() throws MojoExecutionException,
086: MojoFailureException {
087: try {
088: AndroMDALogger.initialize();
089: final URL configurationUri = ResourceUtils
090: .toURL(this .configurationUri);
091: if (configurationUri == null) {
092: throw new MojoExecutionException(
093: "Configuration could not be loaded from '"
094: + this .configurationUri + "'");
095: }
096:
097: // - setup the classpath
098: this .addPluginDependencies(Constants.ARTIFACT_ID,
099: Artifact.SCOPE_RUNTIME);
100: this .initializeClasspathFromClassPathElements(this .project
101: .getRuntimeClasspathElements());
102: final Configuration configuration = this
103: .getConfiguration(configurationUri);
104: this .execute(configuration);
105: } catch (Throwable throwable) {
106: String message = "Error running AndroMDA";
107: throwable = ExceptionUtils.getRootCause(throwable);
108: if (throwable instanceof MalformedURLException
109: || throwable instanceof FileNotFoundException) {
110: message = "Configuration is not valid '"
111: + this .configurationUri + "'";
112: }
113: throw new MojoExecutionException(message, throwable);
114: }
115: }
116:
117: /**
118: * Performs the execution of an AndroMDA service with the given
119: * <code>configuration</code>.
120: *
121: * @param configuration the configuration to use for AndroMDA service execution
122: */
123: protected abstract void execute(final Configuration configuration)
124: throws Exception;
125:
126: /**
127: * @param configurationUri The configurationUri to set.
128: */
129: public void setConfigurationUri(String configurationUri) {
130: this .configurationUri = configurationUri;
131: }
132:
133: /**
134: * @param project The project to set.
135: */
136: public void setProject(MavenProject project) {
137: this .project = project;
138: }
139:
140: /**
141: * Sets the current settings for this Mojo.
142: *
143: * @param settings The settings to set.
144: */
145: public void setSettings(Settings settings) {
146: this .settings = settings;
147: }
148:
149: /**
150: * Sets the property files for this project.
151: *
152: * @param propertyFiles
153: */
154: public void setPropertyFiles(List propertyFiles) {
155: this .propertyFiles = propertyFiles;
156: }
157:
158: /**
159: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getProject()
160: */
161: protected MavenProject getProject() {
162: return this .project;
163: }
164:
165: /**
166: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPropertyFiles()
167: */
168: protected List getPropertyFiles() {
169: return this .propertyFiles;
170: }
171:
172: /**
173: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getSettings()
174: */
175: protected Settings getSettings() {
176: return this .settings;
177: }
178:
179: /**
180: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getFactory()
181: */
182: protected ArtifactFactory getFactory() {
183: return this .factory;
184: }
185:
186: /**
187: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPlugins()
188: */
189: protected List getPlugins() {
190: return this .plugins;
191: }
192:
193: /**
194: * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getLocalRepository()
195: */
196: protected ArtifactRepository getLocalRepository() {
197: return this.localRepository;
198: }
199: }
|