001: package org.andromda.maven.plugin.initialize;
002:
003: import java.io.File;
004:
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.ListIterator;
010:
011: import org.apache.commons.io.FilenameUtils;
012: import org.apache.commons.lang.StringUtils;
013: import org.apache.maven.artifact.repository.ArtifactRepository;
014: import org.apache.maven.plugin.AbstractMojo;
015: import org.apache.maven.plugin.MojoExecutionException;
016: import org.apache.maven.plugin.MojoFailureException;
017: import org.apache.maven.project.MavenProject;
018: import org.codehaus.plexus.util.FileUtils;
019:
020: /**
021: * Provides any initialization required by the AndroMDA build.
022: *
023: * @author Chad Brandon
024: * @goal initialize
025: * @phase generate-resources
026: */
027: public class InitializeMojo extends AbstractMojo {
028: /**
029: * The maven project.
030: *
031: * @parameter expression="${project}"
032: * @required
033: * @readonly
034: * @description "the maven project to use"
035: */
036: protected MavenProject project;
037:
038: /**
039: * @parameter expression="${localRepository}"
040: * @required
041: * @readonly
042: */
043: protected ArtifactRepository localRepository;
044:
045: /**
046: * @parameter
047: * @required
048: * @description the directory where any local (those not kept in a remote repository) artifacts are kept.
049: */
050: protected String localArtifactDirectory;
051:
052: /**
053: * @parameter expression="${bootstrap.artifacts}"
054: * @description Whether or not bootstrap artifacts should be installed, by default they are not. If this is
055: * set to true this means the andromda bootstrap plugin will be updating the bootstaps, therefore
056: * this plugin will not deploy any of the current bootstraps (since that would create an inconsistent state).
057: */
058: protected boolean installBootstraps;
059:
060: /**
061: * @parameter expression="org/andromda/bootstrap"
062: * @required
063: * @readonly
064: * @description the name root directory of the bootstrap artifacts.
065: */
066: protected String bootstrapDirectoryName;
067:
068: /**
069: * @see org.apache.maven.plugin.Mojo#execute()
070: */
071: public void execute() throws MojoExecutionException,
072: MojoFailureException {
073: if (!this .installBootstraps) {
074: try {
075: final File bootstrapDirectory = new File(
076: this .localArtifactDirectory);
077: final Collection contents = this
078: .getDirectoryContents(bootstrapDirectory);
079: for (final Iterator iterator = contents.iterator(); iterator
080: .hasNext();) {
081: String path = (String) iterator.next();
082: final String extension = FileUtils
083: .getExtension(path);
084: String fileName = this .getFileName(path);
085: final String version = this .project.getVersion();
086: final String versionedFileName = StringUtils
087: .replace(fileName, '.' + extension, '-'
088: + version + '.' + extension);
089: final String artifactId = this .getArtifactId(path);
090: final String newPath = StringUtils.replace(path,
091: fileName, artifactId + '/' + version + '/'
092: + versionedFileName);
093: final File bootstrapFile = new File(
094: this .localArtifactDirectory, path);
095: final File repositoryFile = new File(
096: this .localRepository.getBasedir(), newPath);
097:
098: if (bootstrapFile.lastModified() > repositoryFile
099: .lastModified()) {
100: this .getLog().info(
101: "Installing bootstrap artifact "
102: + bootstrapFile + " to "
103: + repositoryFile);
104: FileUtils.copyFile(bootstrapFile,
105: repositoryFile);
106: }
107: }
108: } catch (final Throwable throwable) {
109: throw new MojoExecutionException(
110: "Error installing bootstrap artifact(s)",
111: throwable);
112: }
113: }
114: }
115:
116: /**
117: * Retrieves the artifact id from the given <code>path</code>.
118: * @param path the path from which to retrieve the artifactId.
119: * @return the artifactId.
120: */
121: private String getArtifactId(final String path) {
122: return this .getFileName(path).replaceAll("\\..*", "");
123: }
124:
125: /**
126: * Retrieves the file name from the given <code>path</code>.
127: * @param path the path from which to retrieve the file name
128: * @return the path.
129: */
130: private String getFileName(final String path) {
131: return path.replaceAll(".*(\\\\|/)", "");
132: }
133:
134: /**
135: * Retrieves the contents of this directory as a list of relative
136: * paths.
137: * @param the directory from which to retrieve the contents.
138: * @return the contents of the directory as a list of relative paths.
139: */
140: private List getDirectoryContents(final File directory) {
141: final List files = new ArrayList();
142: this .loadFiles(directory, files);
143: for (final ListIterator iterator = files.listIterator(); iterator
144: .hasNext();) {
145: final File file = (File) iterator.next();
146: final String filePath = file.toString().replace('\\', '/');
147: final String directoryPath = directory.toString().replace(
148: '\\', '/');
149: final String relativePath = StringUtils.replace(filePath,
150: directoryPath + '/', "");
151: if (!FilenameUtils.wildcardMatch(relativePath,
152: this .bootstrapDirectoryName + "**/*")) {
153: iterator.remove();
154: } else {
155: iterator.set(relativePath);
156: }
157: }
158: return files;
159: }
160:
161: /**
162: * Loads all files find in the <code>directory</code> and adds them to the <code>fileList</code>.
163: *
164: * @param directory the directory from which to load all files.
165: * @param fileList the List of files to which we'll add the found files.
166: * @param includeSubdirectories whether or not to include sub directories when loading the files.
167: */
168: private void loadFiles(final File directory, final List fileList) {
169: if (directory != null) {
170: final File[] files = directory.listFiles();
171: if (files != null) {
172: for (int ctr = 0; ctr < files.length; ctr++) {
173: File file = files[ctr];
174: if (!file.isDirectory()) {
175: fileList.add(file);
176: } else {
177: loadFiles(file, fileList);
178: }
179: }
180: }
181: }
182: }
183: }
|