[jboss-svn-commits] JBL Code SVN: r13973 - labs/jbossbuild/maven-plugins/trunk/maven-jboss-retro-plugin/src/main/java/org/jboss/maven/plugins/retro.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Aug 2 18:12:42 EDT 2007


Author: pgier
Date: 2007-08-02 18:12:42 -0400 (Thu, 02 Aug 2007)
New Revision: 13973

Added:
   labs/jbossbuild/maven-plugins/trunk/maven-jboss-retro-plugin/src/main/java/org/jboss/maven/plugins/retro/WeaveDependenciesMojo.java
Log:
Initial checkin of weave dependencies mojo.
Issue: JBBUILD-360

Added: labs/jbossbuild/maven-plugins/trunk/maven-jboss-retro-plugin/src/main/java/org/jboss/maven/plugins/retro/WeaveDependenciesMojo.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-jboss-retro-plugin/src/main/java/org/jboss/maven/plugins/retro/WeaveDependenciesMojo.java	                        (rev 0)
+++ labs/jbossbuild/maven-plugins/trunk/maven-jboss-retro-plugin/src/main/java/org/jboss/maven/plugins/retro/WeaveDependenciesMojo.java	2007-08-02 22:12:42 UTC (rev 13973)
@@ -0,0 +1,266 @@
+package org.jboss.maven.plugins.retro;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.Deflater;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.NameFileFilter;
+import org.apache.commons.io.filefilter.TrueFileFilter;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.resolver.ResolutionNode;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.jboss.weaver.WeaveRunner;
+import org.jboss.weaver.Weaver;
+
+/**
+ * This Mojo can be used to weave project dependencies.
+ * WARNING: Implementation of this mojo is not complete!
+ * 
+ * @phase initialize
+ * @goal weave-dependencies
+ * @requiresDependencyResolution
+ */
+public class WeaveDependenciesMojo extends AbstractMojo
+{
+   public static final String JBOSS_RETRO_ARTIFACTID = "jboss-retro";
+   
+   protected final String fileSep = File.separator;
+
+   protected final String pathSep = File.pathSeparator;
+
+   /**
+    * The Maven Project Object
+    *
+    * @parameter expression="${project}"
+    * @required
+    * @readonly
+    */
+   protected MavenProject project;
+
+   /**
+    * The Maven Project Helper Object
+    *
+    * @component
+    * @required
+    * @readonly
+    */
+   protected org.apache.maven.project.MavenProjectHelper projectHelper;
+
+   /**
+    * INTERNAL : Artifact resolver, needed to download dependencies
+    *
+    * @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
+    * @required
+    * @readonly
+    */
+   protected ArtifactResolver artifactResolver;
+
+   /**
+    * INTERNAL : Artifact resolver, needed to download dependencies
+    *
+    * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
+    * @required
+    * @readonly
+    */
+   protected ArtifactMetadataSource artifactMetadataSource;
+   
+   /**
+    * INTERNAL : Local maven repository.
+    *
+    * @parameter expression="${localRepository}"
+    * @required
+    * @readonly
+    */
+   protected ArtifactRepository localRepository;
+   
+   /**
+    * The plugin dependencies.
+    *
+    * @parameter expression="${plugin.artifacts}"
+    * @required
+    * @readonly
+    */
+   protected List pluginArtifacts;
+
+   /**
+    * Include verbose output.
+    * @parameter
+    */
+   protected boolean verbose = false;
+   
+   /**
+    * Suppress output information.
+    * @parameter
+    */
+   protected boolean suppress = true;
+
+   /**
+    * The Weaver class to use for weaving the classes.
+    * Defaults to org.jboss.weaver.Weaver
+    * Any subclass of org.jboss.weaver.Weaver can be used
+    * 
+    * @parameter
+    */
+   protected String weaverClass = "org.jboss.weaver.Weaver";
+
+   /**
+    * The jar file or directory where the weaved classes
+    * should be written.
+    *
+    * @parameter expression="${project.build.directory}/weaved-dependencies"
+    */
+   protected String outputDirectory;
+
+   /**
+    * Main plugin execution method
+    */   
+   public void execute() throws MojoFailureException
+   {
+      this.getLog().info("Weaving dependencies");
+      
+      long start = System.currentTimeMillis();
+      
+      weaveDependencies();
+      
+      long end = System.currentTimeMillis();
+      this.getLog().info("Weaving complete: " + (end - start) + "ms");
+   }
+   
+   protected void weaveDependencies() throws MojoFailureException 
+   {      
+      Set dependencyArtifacts = project.getDependencyArtifacts();
+
+      try 
+      {
+         ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts, 
+               project.getArtifact(), project.getRemoteArtifactRepositories(), localRepository, artifactMetadataSource);
+         Set artifacts = result.getArtifacts();
+         
+         File parentDir = new File(outputDirectory);
+         if ( ! parentDir.exists()) {
+            parentDir.mkdirs();
+         }
+         
+         for (Object artifactObj : artifacts) 
+         {
+            Artifact artifact = (Artifact)artifactObj;
+            
+            WeaveRunner weaveRunner = this.createWeaveRunner(artifacts);
+            weaveRunner.setOutputPath(outputDirectory + File.separator + artifact.getFile().getName());
+            
+            try
+            {
+               getLog().info("Weaving: " + artifact);
+               weaveRunner.addSourcePath(artifact.getFile().getAbsolutePath());
+               System.out.println("Path: " + weaveRunner.getSourcePaths().get(0));
+               weaveRunner.weave();
+            }
+            catch (Exception e)
+            {
+               e.printStackTrace();
+            }
+            
+            weaveRunner.setSourcePaths(new ArrayList<String>());
+            
+         }
+      }
+      catch (ArtifactResolutionException are) {
+         are.printStackTrace();
+      }
+      catch (ArtifactNotFoundException anfe) {
+         anfe.printStackTrace();
+      }
+      
+   }
+   
+   public WeaveRunner createWeaveRunner(Set artifacts) throws MojoFailureException
+   {
+      
+      Weaver weaver = this.instantiateWeaver();
+      String classpath = this.buildClasspath(artifacts);
+      weaver.setClasspath(classpath);
+      weaver.init();
+      
+      WeaveRunner weaveRunner = new WeaveRunner(weaver);
+      weaveRunner.setVerbose(verbose);
+      weaveRunner.setSuppress(suppress);
+      weaveRunner.setUsingSystemClasspath(false);
+      weaveRunner.setOutputToJar(true);
+      
+      return weaveRunner;
+   }
+      
+   /**
+    * Generates a classpath string based on the resolved dependencies.
+    * @return The classpath string
+    */
+   protected String buildClasspath(Set artifacts) {
+      StringBuilder classpath = new StringBuilder();
+
+
+      for (Object artifactObj : artifacts)
+      {
+         Artifact artifact = (Artifact)artifactObj;
+         classpath.append(artifact.getFile().getAbsolutePath());
+         classpath.append(pathSep);
+      }
+      
+      for (Object artifactObj : pluginArtifacts)
+      {
+         try
+         {
+            Artifact artifact = (Artifact) artifactObj;
+            if (artifact.getFile() != null)
+            {
+               classpath.append(artifact.getFile().getCanonicalPath());
+               classpath.append(pathSep);
+            }
+         }
+         catch (IOException ioe)
+         {
+            this.getLog().warn("Could not get filename");
+         }
+      }
+      return classpath.toString();
+   }
+      
+   /**
+    * This method creates the weaver instance.  Subclass mojos
+    * can use this class to override the default weaver configuration.
+    * @return The weaver instance
+    */
+   protected Weaver instantiateWeaver() throws MojoFailureException {
+      // We have to set the classpath first because the initialization
+      // may require access to the classpath.
+      Weaver weaver = null;
+
+      try {
+         weaver = (Weaver)Class.forName(weaverClass).newInstance();
+      } catch (Exception e) {
+         getLog().error("Unable to instantiate weaver class: " + this.weaverClass);
+         getLog().error(e.getMessage());
+         throw new MojoFailureException(e.getMessage());
+      }
+      
+      return weaver;
+   }
+         
+}




More information about the jboss-svn-commits mailing list