001: package org.andromda.maven.plugin.bootstrap.install;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006:
007: import java.util.ArrayList;
008: import java.util.Collections;
009: import java.util.List;
010:
011: import org.apache.commons.lang.StringUtils;
012: import org.apache.maven.artifact.Artifact;
013: import org.apache.maven.artifact.repository.ArtifactRepository;
014: import org.apache.maven.model.Build;
015: import org.apache.maven.model.Model;
016: import org.apache.maven.model.Parent;
017: import org.apache.maven.plugin.AbstractMojo;
018: import org.apache.maven.plugin.MojoExecutionException;
019: import org.apache.maven.plugin.MojoFailureException;
020: import org.apache.maven.project.MavenProject;
021: import org.codehaus.plexus.util.FileUtils;
022:
023: /**
024: * Provides the installation of bootstrap artifacts.
025: *
026: * @author Chad Brandon
027: *
028: * @goal install
029: * @phase install
030: */
031: public class BootstrapInstallMojo extends AbstractMojo {
032: /**
033: * The maven project.
034: *
035: * @parameter expression="${project}"
036: * @required
037: * @readonly
038: * @description "the maven project to use"
039: */
040: protected MavenProject project;
041:
042: /**
043: * @parameter expression="${localRepository}"
044: * @required
045: * @readonly
046: */
047: protected ArtifactRepository localRepository;
048:
049: /**
050: * @parameter expression="${bootstrap.artifacts}"
051: * @description whether or not bootstrap artifacts should be installed, by default they are not.
052: */
053: protected boolean installBootstraps;
054:
055: /**
056: * @parameter
057: * @required
058: * @description the directory to which the bootstrap artifact should be installed.
059: */
060: protected String installDirectory;
061:
062: /**
063: * @parameter expression="org.andromda"
064: * @required
065: * @readonly
066: * @description the name of the project groupId.
067: */
068: protected String projectGroupId;
069:
070: /**
071: * @parameter expression="org.andromda.bootstrap"
072: * @required
073: * @readonly
074: * @description the name of the project bootstrap groupId.
075: */
076: protected String projectBootstrapGroupId;
077:
078: /**
079: * The extension for "JAR" files.
080: */
081: private static final String JAR_EXTENSION = "jar";
082:
083: /**
084: * The extension for "POM" files.
085: */
086: private static final String POM_EXTENSION = "pom";
087:
088: /**
089: * @see org.apache.maven.plugin.Mojo#execute()
090: */
091: public void execute() throws MojoExecutionException,
092: MojoFailureException {
093: if (this .installBootstraps) {
094: try {
095: final File installDirectory = new File(
096: this .installDirectory);
097: if (!installDirectory.exists()
098: || !installDirectory.isDirectory()) {
099: throw new MojoExecutionException("'"
100: + installDirectory
101: + "' is not a valid install directory");
102: }
103: Artifact artifact = this .project.getArtifact();
104:
105: final String path = this .replaceExtension(artifact,
106: JAR_EXTENSION);
107: final String localRepositoryDirectory = this .localRepository
108: .getBasedir();
109: final File existingFile = new File(
110: localRepositoryDirectory, path);
111: final String bootstrapGroupId = this
112: .getBootstrapGroupId(artifact);
113: final String bootstrapPath = bootstrapGroupId.replace(
114: '.', '/')
115: + '/' + artifact.getArtifactId();
116: final File bootstrapFile = new File(installDirectory,
117: bootstrapPath + '.' + JAR_EXTENSION);
118: this .getLog().info(
119: "Installing bootstrap artifact: "
120: + bootstrapFile);
121: FileUtils.copyFile(existingFile, bootstrapFile);
122: final File bootstrapPomFile = new File(
123: installDirectory, bootstrapPath + '.'
124: + POM_EXTENSION);
125: this .writeMinimalPom(bootstrapPomFile);
126: } catch (final Throwable throwable) {
127: throw new MojoExecutionException(
128: "Error creating bootstrap artifact", throwable);
129: }
130: }
131: }
132:
133: /**
134: * Clears the POM's model of its parent or any dependencies
135: * it may have so that we can write a POM that isn't dependant on anything
136: * (which we need for bootstrap artifacts).
137: *
138: * @param bootstrapPomFile the bootstrap POM file to write.
139: */
140: private void writeMinimalPom(final File bootstrapPomFile)
141: throws MojoExecutionException, IOException {
142: if (this .project != null) {
143: Model model = this .project.getModel();
144: if (model == null) {
145: throw new MojoExecutionException(
146: "Model could not be retrieved from current project");
147: }
148:
149: // - remove the parent
150: final Parent parent = model.getParent();
151: final List dependencies = new ArrayList(model
152: .getDependencies());
153: final String groupId = model.getGroupId();
154: final Artifact artifact = this .project.getArtifact();
155: final Build build = this .project.getBuild();
156: final List developers = new ArrayList(model.getDevelopers());
157: final List contributors = new ArrayList(model
158: .getContributors());
159: model.setGroupId(this .getBootstrapGroupId(artifact));
160: model.setParent(null);
161: model.setBuild(null);
162: model.setDependencies(Collections.EMPTY_LIST);
163: model.setDevelopers(Collections.EMPTY_LIST);
164: model.setContributors(Collections.EMPTY_LIST);
165: final FileWriter fileWriter = new FileWriter(
166: bootstrapPomFile);
167: this .project.writeModel(fileWriter);
168: fileWriter.flush();
169:
170: // - set any removed items back to the way it was since we've written the POM
171: model.setParent(parent);
172: model.setGroupId(groupId);
173: model.setDependencies(dependencies);
174: model.setBuild(build);
175: model.setDevelopers(developers);
176: model.setContributors(contributors);
177: }
178: }
179:
180: /**
181: * Retrieves the project's bootstrap groupId from the given <code>artifact</code>.
182: *
183: * @param artifact the artfact from which to retrieve the group Id.
184: * @return the bootstrap groupId.
185: */
186: private String getBootstrapGroupId(final Artifact artifact) {
187: return StringUtils.replaceOnce(artifact.getGroupId(),
188: this .projectGroupId, this .projectBootstrapGroupId);
189: }
190:
191: /**
192: * Retrieves the extension from the given path.
193: * @param artifact the artifact from which to retrieve the version information.
194: * @param path the path of the file
195: * @return the extension.
196: */
197: private String replaceExtension(final Artifact artifact,
198: final String newExtension) {
199: String path = this .localRepository.pathOf(artifact);
200: final String version = artifact.getVersion() != null ? artifact
201: .getVersion().trim() : "";
202: int versionIndex = path.lastIndexOf(artifact.getVersion());
203: final String extension = path.substring(versionIndex
204: + version.length() + 1, path.length());
205: if (!newExtension.equals(extension)) {
206: int extensionIndex = path.lastIndexOf(extension);
207: path = path.substring(0, extensionIndex) + newExtension;
208: }
209: return path;
210: }
211: }
|