All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.maven.plugins.retro.AbstractWeaveMojo Maven / Gradle / Ivy

The newest version!
package org.jboss.maven.plugins.retro;

import java.io.File;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.StreamConsumer;
import org.jboss.weaver.Main;

/**
 * Abstract parent of weaver mojos.
 * @author pgier
 *
 */
public abstract class AbstractWeaveMojo 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;

   /**
    * The plugin dependencies.
    *
    * @parameter expression="${plugin.artifacts}"
    * @required
    * @readonly
    */
   protected List pluginArtifacts;

   /**
    * Include verbose output.
    * @parameter default-value="false"
    */
   protected boolean verbose = false;

   /**
    * Use the system classpath.
    * @parameter default-value="false"
    */
   protected boolean useSystemClasspath = false;
   
   /**
    * Suppress output information.
    * @parameter default-value="false"
    */
   protected boolean suppress = false;
   
   /**
    * 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.
    * To compile from jdk1.5 to jdk1.4, set this to "org.jboss.weaver.retro.WeaverRetroJdk14".
    * 
    * @parameter default-value="org.jboss.weaver.Weaver"
    */
   protected String weaverClass = "org.jboss.weaver.Weaver";

   /**
    * Classifier to append to the weaved output file (artifact).
    * Defaults to null.  If the classifier is null, then the jar
    * of the weaved classes will replace the main artifact.
    * 
    * @parameter
    */
   protected String classifier;
   
   /**
    * Returns the path to which weaved files should be written.
    * @return The output path.
    */
   protected abstract String getOutputPath();
      
   /**
    * Fork the process to a separate jvm
    * @parameter
    */
   protected boolean fork = true;
   
   /**
    * Path to Java virtual machine to use when forking
    * @parameter
    */
   protected String jvm;
   
   
   protected String getJarClassifier() 
   {
      return this.classifier;
   }

   /**
    * Consume and log command output from the retro process
    * @author pgier
    */
   public class MojoLogStreamConsumer implements StreamConsumer
   {
      public void consumeLine(String line)
      {
         getLog().info(line);
      }
   }
   
   public void doWeave(String classpath, String [] args) throws Exception
   {
      if (fork)
      {
         Commandline cli = new Commandline();
         
         if ( jvm == null || jvm.equals( "" ) )
         {
             // use the same JVM as the one used to run Maven (the "java.home" one)
             jvm = System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java";
             getLog().debug( "Using JVM: " + jvm );
         }

         cli.setExecutable( jvm );
         String [] jvmArgs = new String[3];
         jvmArgs[0] = "-cp";
         jvmArgs[1] = classpath;
         jvmArgs[2] = Main.class.getName();
         cli.addArguments(jvmArgs);
         
         cli.addArguments(args);
         
         StreamConsumer out = new MojoLogStreamConsumer();
         StreamConsumer err = new MojoLogStreamConsumer();

         getLog().debug("Forking Command Line: ");
         getLog().debug(cli.toString());
         getLog().debug("");
         
         try
         {
            int returnCode = CommandLineUtils.executeCommandLine( cli, out, err );
            if ( returnCode != 0)
            {
               throw new MojoExecutionException("There were errors during the weave");
            }
         }
         catch ( CommandLineException e )
         {
            throw new MojoExecutionException( "Error while executing forked tests.", e );
         }
         
      }
      else
      {
         Main.main(args);
      }
      
   }     
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy