From: <jbo...@li...> - 2006-06-28 20:37:08
|
Author: rl...@jb... Date: 2006-06-28 16:37:05 -0400 (Wed, 28 Jun 2006) New Revision: 4853 Added: labs/jbossbuild/trunk/projects/maven-plugins/maven-builddist-plugin/src/main/java/org/apache/maven/plugin/builddist/BuildDistMojo.java Log: add to version control Added: labs/jbossbuild/trunk/projects/maven-plugins/maven-builddist-plugin/src/main/java/org/apache/maven/plugin/builddist/BuildDistMojo.java =================================================================== --- labs/jbossbuild/trunk/projects/maven-plugins/maven-builddist-plugin/src/main/java/org/apache/maven/plugin/builddist/BuildDistMojo.java 2006-06-28 20:36:45 UTC (rev 4852) +++ labs/jbossbuild/trunk/projects/maven-plugins/maven-builddist-plugin/src/main/java/org/apache/maven/plugin/builddist/BuildDistMojo.java 2006-06-28 20:37:05 UTC (rev 4853) @@ -0,0 +1,436 @@ +package org.apache.maven.plugin.builddist; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.apache.maven.shared.model.fileset.FileSet; +import org.apache.maven.shared.model.fileset.util.FileSetManager; +import org.codehaus.plexus.archiver.ArchiverException; +import org.codehaus.plexus.archiver.UnArchiver; +import org.codehaus.plexus.archiver.manager.ArchiverManager; +import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; +import org.codehaus.plexus.util.FileUtils; + +/** + * Assemble an application bundle or distribution from a prescribed list + * + * @aggregator + * @version $Id: BuildDistMojo.java + * @goal builddist + */ +public class BuildDistMojo extends AbstractMojo +{ + /** + * To look up Archiver/UnArchiver implementations + * + * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}" + * @required + */ + + protected ArchiverManager archiverManager; + + /** + * @parameter expression="${project.remoteArtifactRepositories}" + * @readonly + * @required + */ + private java.util.List remoteRepos; + + /** + * @component + */ + private ArtifactFactory factory; + + /** + * @component + */ + private ArtifactResolver resolver; + + /** + * @parameter expression="${localRepository}" + * @readonly + * @required + */ + private ArtifactRepository local; + + /** + * Contains the full list of projects in the reactor. + * + * @parameter expression="${reactorProjects}" + * @required + * @readonly + */ + private List reactorProjects; + + /** + * The Maven Project. + * + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + /** + * A list of directories. + * + * @parameter + * @required + */ + private ArrayList dirs; + + /** + * A list of directories. + * + * @parameter expression="${basedir}" + * @readonly + */ + private File basedir; + + public void execute() throws MojoExecutionException, MojoFailureException + { + + //for each directory defined, attempt to create the directory + //and then fill it with the proper contents + Iterator iter = dirs.iterator(); + while (iter.hasNext()) + { + Dir dir = (Dir) iter.next(); + + try + { + populateDir(dir, basedir); + } + catch (MojoFailureException e) + { + throw e; + } + catch (FileNotFoundException e) + { + throw new MojoFailureException(e.getMessage()); + + } + + } + + } + + /** + * Create a directory and populate it's contents + * + * @param dir + */ + private void populateDir(Dir dir, File currDir) throws MojoFailureException, FileNotFoundException + { + + // try { + dir.setLocation(FileUtils.resolveFile(currDir, dir.getName())); + + this.getLog().debug("Creating directory " + dir.getLocation()); + + FileUtils.mkdir(dir.getLocation().getAbsolutePath()); + + this.getLog().debug("Populating directory " + dir.getLocation().getAbsolutePath() + " with artifacts"); + + if (dir.getArtifacts() != null) + populateArtifacts(dir); + + if (dir.getReposArtifacts() != null) + populateReposArtifacts(dir); + + if (dir.getFilesets() != null) + populateFilesets(dir); + + //if the new directory we just made needs to contain sub + // directories + //then we create and populate them + if (dir.getDirs() != null) + { + + //for each directory defined, attempt to create the directory + //and then fill it with the proper contents + Iterator iter = dir.getDirs().iterator(); + + while (iter.hasNext()) + { + Dir subDir = (Dir) iter.next(); + + populateDir(subDir, dir.getLocation()); + + } + } + /* + * catch ( e) { throw new MojoFailureException( "Error occured while + * populating directory"); + * } + */ + } + + private void populateFilesets(Dir dir) throws MojoFailureException + { + + FileSetManager fileSetManager; + fileSetManager = new FileSetManager(getLog(), true); + + Iterator iter = dir.getFilesets().iterator(); + + while (iter.hasNext()) + { + FileSet fileset = (FileSet) iter.next(); + + //copy included files; + String includedFiles[] = fileSetManager.getIncludedFiles(fileset); + + for (int i = 0; i < includedFiles.length; i++) + { + File srcFile = FileUtils.resolveFile(new File(fileset.getDirectory()), includedFiles[i]); + + if (!srcFile.exists()) + throw new MojoFailureException("File: " + srcFile.getAbsolutePath() + " does not exist" + + "\nPlease check the artifact definition"); + + File destFile = FileUtils.resolveFile(dir.getLocation(), includedFiles[i]); + + try + { + getLog().info("Copying " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); + + FileUtils.copyFile(srcFile, destFile); + + } + catch (IOException e) + { + throw new MojoFailureException("Error copying file: " + e.getMessage()); + + } + + } + + } + + } + + /** + * Populate a directory with artifacts from a predefined module + * + * @param dir + * The dir object to populate + */ + private void populateArtifacts(Dir dir) throws MojoFailureException, FileNotFoundException + { + //for each artifact defined, attempt to copy the + //the artifact to the directory location + Iterator iter = dir.getArtifacts().iterator(); + + while (iter.hasNext()) + { + org.apache.maven.plugin.builddist.Artifact artifact = (org.apache.maven.plugin.builddist.Artifact) iter.next(); + + //determine the src file + File moduleDir = FileUtils.resolveFile(this.getBasedir().getParentFile(), artifact.getModule()); + File artifactDir = FileUtils.resolveFile(moduleDir, artifact.getArtifactLocation()); + File srcFile = FileUtils.resolveFile(artifactDir, artifact.getArtifactName()); + + if (!srcFile.exists()) + throw new FileNotFoundException("File: " + srcFile.getAbsolutePath() + " does not exist" + + "\nPlease check the artifact definition"); + + File destFile; + if (artifact.isUnpack()) + { + try + { + this.unpack(srcFile, dir.getLocation(), true); + } + catch (Exception e) + { + throw new MojoFailureException("Error unpacking archive: " + e.getMessage()); + } + + } + else + { + + //determine the destination file + if (artifact.getToName() == null) + destFile = FileUtils.resolveFile(dir.getLocation(), artifact.getArtifactName()); + else + destFile = FileUtils.resolveFile(dir.getLocation(), artifact.getToName()); + + try + { + getLog().info("Copying " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath()); + + if (!srcFile.isDirectory()) + FileUtils.copyFile(srcFile, destFile); + else + FileUtils.copyDirectoryStructure(srcFile, FileUtils.resolveFile(dir.getLocation(), srcFile.getName())); + } + catch (IOException e) + { + System.out.println(e.getStackTrace()); + System.out.println(e.getLocalizedMessage()); + throw new MojoFailureException("Error copying file: " + e.getMessage()); + + } + + } + } + } + + /** + * Populate a directory with thirdparty libraries + * + * @param dir + * The dir object to populate + */ + private void populateReposArtifacts(Dir dir) throws MojoFailureException + { + //for each artifact defined, attempt to copy the + //the artifact to the directory location + Iterator iter = dir.getReposArtifacts().iterator(); + + while (iter.hasNext()) + { + ReposArtifact reposArtifact = (ReposArtifact) iter.next(); + + if (reposArtifact.getPackaging() == null) + reposArtifact.setPackaging("jar"); + if (reposArtifact.getVersion() == null) + determineReposArtifactVersion(reposArtifact); + + //get a reference to the the repository version of the artifact + Artifact artifact = factory.createArtifact(reposArtifact.getGroupId(), reposArtifact.getArtifactId(), + reposArtifact.getVersion(), Artifact.SCOPE_PROVIDED, reposArtifact.getPackaging()); + + try + { + resolver.resolve(artifact, remoteRepos, local); + + String destFileName; + if (reposArtifact.getToName() != null) + destFileName = reposArtifact.getToName(); + else if (reposArtifact.isStripVersion()) + destFileName = reposArtifact.getArtifactId() + "." + reposArtifact.getPackaging(); + else + destFileName = artifact.getFile().getName(); + + /* + if (artifact.getToName() == null) + destFile = FileUtils.resolveFile(dir.getLocation(), artifact + .getArtifactName()); + else + destFile = FileUtils.resolveFile(dir.getLocation(), artifact + .getToName()); + */ + getLog().info( + "Copying " + artifact.getFile().getPath() + " to " + dir.getLocation().getAbsolutePath() + "/" + + destFileName); + + FileUtils.copyFile(artifact.getFile(), FileUtils.resolveFile(dir.getLocation(), destFileName)); + + } + catch (Exception e) + { + throw new MojoFailureException("Error when copying file from repository"); + } + + } + + } + + /* Tries to find missing version from dependancy management. If found, the artifact is updated with + * the correct version. + * @param artifact representing configured file. + */ + private void determineReposArtifactVersion(ReposArtifact artifact) + { + this.getLog().debug("Attempting to find missing version from dependency management."); + + List list = this.project.getDependencyManagement().getDependencies(); + + for (int i = 0; i < list.size(); ++i) + { + Dependency dependency = (Dependency) list.get(i); + + if (dependency.getGroupId().equals(artifact.getGroupId()) + && dependency.getArtifactId().equals(artifact.getArtifactId()) + && dependency.getType().equals(artifact.getPackaging())) + { + this.getLog().debug("Found missing version: " + dependency.getVersion()); + + artifact.setVersion(dependency.getVersion()); + } + } + } + + /** + * @return Returns the project. + */ + public MavenProject getProject() + { + return project; + } + + /** + * @return Returns the basedir. + */ + public File getBasedir() + { + return basedir; + } + + /** + * @param basedir + * The basedir to set. + */ + public void setBasedir(File basedir) + { + this.basedir = basedir; + } + + protected void unpack(File file, File location, boolean overWrite) throws MojoExecutionException, + NoSuchArchiverException + { + + String archiveExt = FileUtils.getExtension(file.getAbsolutePath()).toLowerCase(); + + if (archiveExt.equalsIgnoreCase("sar") || archiveExt.equalsIgnoreCase("deployer")) + archiveExt = "jar"; + try + { + UnArchiver unArchiver; + + unArchiver = this.archiverManager.getUnArchiver(archiveExt); + + unArchiver.setOverwrite(overWrite); + unArchiver.setSourceFile(file); + + unArchiver.setDestDirectory(location); + + unArchiver.extract(); + } + catch (IOException e) + { + throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e); + } + catch (ArchiverException e) + { + throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e); + } + } + +} \ No newline at end of file |