001: package org.andromda.maven.plugin.cartridge;
002:
003: import java.io.File;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: import org.apache.commons.lang.StringUtils;
008: import org.apache.maven.archiver.MavenArchiveConfiguration;
009: import org.apache.maven.archiver.MavenArchiver;
010: import org.apache.maven.artifact.Artifact;
011: import org.apache.maven.plugin.AbstractMojo;
012: import org.apache.maven.plugin.MojoExecutionException;
013: import org.apache.maven.plugin.MojoFailureException;
014: import org.apache.maven.project.MavenProject;
015: import org.codehaus.plexus.archiver.jar.JarArchiver;
016: import org.codehaus.plexus.util.FileUtils;
017:
018: /**
019: * Provides the archiving of AndroMDA cartridges.
020: *
021: * @author Chad Brandon
022: * @goal andromda-cartridge
023: * @phase package
024: * @requiresProject
025: * @description builds an AndroMDA cartridge.
026: */
027: public class CartridgeArchiverMojo extends AbstractMojo {
028: /**
029: * Directory that resources are copied to during the build.
030: *
031: * @parameter expression="${project.build.directory}"
032: * @required
033: */
034: private String workDirectory;
035:
036: /**
037: * The directory for the generated cartridge.
038: *
039: * @parameter expression="${project.build.outputDirectory}"
040: * @required
041: */
042: private String outputDirectory;
043:
044: /**
045: * The name of the cartridge file to generate.
046: *
047: * @parameter alias="modelName" expression="${project.build.finalName}"
048: * @required
049: * @readonly
050: */
051: private String finalName;
052:
053: /**
054: * The maven project.
055: *
056: * @parameter expression="${project}"
057: * @required
058: * @readonly
059: * @description "the maven project to use"
060: */
061: private MavenProject project;
062:
063: /**
064: * The Jar archiver.
065: *
066: * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
067: * @required
068: */
069: private JarArchiver jarArchiver;
070:
071: /**
072: * The maven archiver to use.
073: *
074: * @parameter
075: */
076: private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
077:
078: /**
079: * The libraries to include in the cartridge.
080: *
081: * @parameter
082: */
083: private CartridgeArtifact[] artifacts;
084:
085: /**
086: * The patterns indicating what will be included in the cartridge.
087: */
088: private static final String[] OUTPUT_INCLUDES = new String[] { "**/*" };
089:
090: /**
091: * @see org.apache.maven.plugin.Mojo#execute()
092: */
093: public void execute() throws MojoExecutionException,
094: MojoFailureException {
095: getLog().debug(
096: " ======= CartridgeArchiverMojo settings =======");
097: getLog().debug("workDirectory[" + this .workDirectory + "]");
098: getLog().debug("outputDirectory[" + this .outputDirectory + "]");
099: getLog().debug("finalName[" + finalName + "]");
100: final File buildDirectory = this .getBuildDirectory();
101: final File outputDirectory = new File(this .outputDirectory);
102: try {
103: if (artifacts != null) {
104: for (int ctr = 0; ctr < artifacts.length; ctr++) {
105: final CartridgeArtifact cartridgeArtifact = artifacts[ctr];
106: final Artifact artifact = this
107: .resolveArtifact(cartridgeArtifact);
108: if (artifact != null) {
109: final String path = cartridgeArtifact.getPath();
110: if (path == null || path.trim().length() == 0) {
111: throw new MojoFailureException(
112: "Please specify the 'path' for the cartridge artifact ["
113: + cartridgeArtifact
114: .getGroupId()
115: + ":"
116: + cartridgeArtifact
117: .getArtifactId()
118: + ":"
119: + cartridgeArtifact
120: .getType() + "]");
121: }
122:
123: final File destinationDirectory = new File(
124: outputDirectory, cartridgeArtifact
125: .getPath());
126: final File artifactFile = artifact.getFile();
127: final String artifactPath = artifactFile != null ? StringUtils
128: .replace(artifact.getFile().toString()
129: .replaceAll(".*(\\\\|/)", ""),
130: '-' + artifact.getVersion(), "")
131: : null;
132: if (artifactPath != null) {
133: FileUtils
134: .copyFile(artifactFile, new File(
135: destinationDirectory,
136: artifactPath));
137: }
138: }
139: }
140: }
141: final File cartridgeFile = new File(buildDirectory,
142: this .finalName + ".jar");
143: final Artifact artifact = this .project.getArtifact();
144: final MavenArchiver archiver = new MavenArchiver();
145: archiver.setArchiver(this .jarArchiver);
146: archiver.setOutputFile(cartridgeFile);
147: archiver.getArchiver().addDirectory(outputDirectory,
148: OUTPUT_INCLUDES, null);
149: archiver.createArchive(this .project, this .archive);
150: artifact.setFile(cartridgeFile);
151: } catch (final Throwable throwable) {
152: throw new MojoExecutionException(
153: "An error occured while packaging this cartridge",
154: throwable);
155: }
156: }
157:
158: /**
159: * Resolves the actual Maven artifact for the given cartridge artifact.
160: * @param cartridgeArtifact the cartridge artifact.
161: * @return
162: * @throws MojoFailureException
163: */
164: public Artifact resolveArtifact(
165: final CartridgeArtifact cartridgeArtifact)
166: throws MojoFailureException {
167: Artifact resolvedArtifact = null;
168: if (cartridgeArtifact != null) {
169: final Collection artifacts = this .project.getArtifacts();
170: final String groupId = cartridgeArtifact.getGroupId();
171: final String artifactId = cartridgeArtifact.getArtifactId();
172: final String type = cartridgeArtifact.getType();
173: if (groupId == null || artifactId == null) {
174: throw new MojoFailureException(
175: "Could not resolve cartridge artifact ["
176: + groupId + ":" + artifactId + ":"
177: + type + "]");
178: }
179:
180: for (final Iterator iterator = artifacts.iterator(); iterator
181: .hasNext();) {
182: final Artifact artifact = (Artifact) iterator.next();
183: if (artifact.getGroupId().equals(groupId)
184: && artifact.getArtifactId().equals(artifactId)
185: && (type == null || artifact.getType().equals(
186: type))) {
187: resolvedArtifact = artifact;
188: break;
189: }
190: }
191:
192: if (resolvedArtifact == null) {
193: // Artifact has not been found
194: throw new MojoFailureException("Artifact[" + groupId
195: + ":" + artifactId + ":" + type + "] "
196: + "is not a dependency of the project.");
197: }
198: return resolvedArtifact;
199: }
200: return resolvedArtifact;
201: }
202:
203: /**
204: * The build directory.
205: */
206: private File buildDirectory;
207:
208: /**
209: * Gets the current build directory as a file instance.
210: *
211: * @return the build directory as a file.
212: */
213: protected File getBuildDirectory() {
214: if (this .buildDirectory == null) {
215: this .buildDirectory = new File(workDirectory);
216: }
217: return this.buildDirectory;
218: }
219: }
|