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

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

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

import java.io.File;
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.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
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;

/**
 * Mojo for running the retro check test.  
 * NOTE: The retro check functionality is meant to be used with
 * the jdk1.5 to 1.4 conversion.  Other types of class weaving do
 * not need to use this mojo.  This mojo should be run using 
 * a java1.4 jvm, which can be set using the jvm configuration 
 * parameter.
 * 
 * @phase test
 * @goal retro-check
 * 
 */
public class RetroCheckMojo extends AbstractMojo
{

   public static final String JBOSS_RETRO_ARTIFACTID = "jboss-retro";
   
   /**
    * The Maven Project Object
    *
    * @parameter expression="${project}"
    * @required
    */
   protected MavenProject project;

   /**
    * The Maven Project Helper Object
    *
    * @component
    * @required
    */
   protected org.apache.maven.project.MavenProjectHelper projectHelper;

   /**
    * INTERNAL : Artifact factory, needed to download dependencies
    *
    * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
    * @required
    * @readonly
    */
   protected ArtifactFactory artifactFactory;

   /**
    * INTERNAL : Artifact resolver, needed to download dependencies
    *
    * @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
    * @required
    * @readonly
    */
   protected ArtifactResolver artifactResolver;
   
   /**
    * INTERNAL : Local maven repository.
    *
    * @parameter expression="${localRepository}"
    * @required
    * @readonly
    */
   protected ArtifactRepository localRepository;
   
   /**
    * Option to specify the jvm (or path to the java executable) to use with the forking options. For the default, the
    * jvm will be the same as the one used to run Maven.
    * 
    * @parameter
    * @required
    */
   private String jvm;

   /**
    * The Maven Plugin components
    *
    * @parameter expression="${plugin.components}"
    * @required
    * @readonly
    */
   protected List pluginComponents;

   /**
    * The plugin artifacts.
    *
    * @parameter expression="${plugin.artifacts}"
    * @required
    * @readonly
    */
   private List pluginArtifacts;
   
   /**
    * The path to the classes to be checked, relative to
    * the build directory.  Defaults to a value of
    * "classes-weaved" which means that classes in the direcotry
    * target/classes-weaved will be checked.
    * @parameter
    * @required
    */
   private String checkDirectory;
   
   /**
    * Enable verbose output
    * @parameter
    */
   private boolean verbose = false;

   /**
    * Suppress output
    * @parameter
    */
   private boolean suppress = false;

   /**
    * Location of the retro check jar to include in the classpath
    */
   private File jbossRetroCheckJar = null;
   
   /**
    * Location of the retro runtime jar to include in the classpath
    */
   private File jbossRetroRtJar = null;
   
   /**
    * Ignore check errors.  If set to true, the build will continue
    * regardless of whether there are check errors.
    * Default is false.
    * 
    * @parameter
    * 
    */
   private boolean ignoreErrors = false;
   
   public void execute() throws MojoExecutionException
   {
      if ( jvm == null || jvm.equals("") || ! new File(jvm).exists())
      {
          getLog().error("Unable to locate jvm: " + jvm);
          getLog().error("Skipping retro check");
          return;
      }

      // Make sure checker and runtime available on the classpath
      this.resolveCheckerLocation();
      
      this.getLog().info("Checking classes for jdk14");
      
      ArrayList argsList = new ArrayList();

      argsList.add("-cp");
      
      String classpath = this.createCheckerClasspath();
      getLog().debug("Using checker classpath: " + classpath);
      argsList.add(classpath);

      argsList.add("org.jboss.ant.tasks.retrocheck.Checker");
      
      if (verbose)
      {
         argsList.add("-verbose");
      }
      
      if (suppress)
      {
         argsList.add("-suppress");
      }

      String retroClassesDir = project.getBuild().getDirectory() + File.separator + checkDirectory;
      argsList.add(retroClassesDir);

      String[] args = new String[argsList.size()];
      args = argsList.toArray(args);

      Commandline cli = new Commandline();
   
      cli.setExecutable( jvm );
      cli.addArguments(args);
      
      StreamConsumer out = new MojoLogStreamConsumer();
      StreamConsumer err = new MojoLogStreamConsumer();

      try
      {
         int returnCode = CommandLineUtils.executeCommandLine( cli, out, err );
         if ( ( ! ignoreErrors ) && returnCode != 0)
         {
            throw new MojoExecutionException("There were errors during the retro check");
         }
      }
      catch ( CommandLineException e )
      {
         throw new MojoExecutionException( "Error while executing forked tests.", e );
      }
   }

   public String createCheckerClasspath() 
   {
      StringBuilder classpath = new StringBuilder();

      String fileSep = System.getProperty("file.separator");
      String weavedClassesDir = project.getBuild().getDirectory() + fileSep + checkDirectory;
      classpath.append(weavedClassesDir);
      classpath.append(File.pathSeparator);
            
      for (Object artifactObj : pluginArtifacts)
      {
         Artifact artifact = (Artifact)artifactObj;
         if (artifact.getArtifactId().equals(JBOSS_RETRO_ARTIFACTID) && artifact.getClassifier() == null)
         {
            // We don't want to include the jboss-retro dependency for the checks
            // because the jboss-retro jar is jdk1.5 and we should be running under 1.4
            // and the checker and rt jars will have everything needed for the checks
            continue;
         }
         try
         {
            File artifactFile = artifact.getFile();
            if (artifactFile != null)
            {
               classpath.append(artifactFile.getCanonicalPath());
               classpath.append(File.pathSeparator);
            }
         }
         catch (IOException ioe)
         {
            this.getLog().warn("Could not get filename");
         }
      }
      classpath.append(this.jbossRetroCheckJar.getAbsolutePath());
      classpath.append(File.pathSeparator);
      classpath.append(this.jbossRetroRtJar.getAbsolutePath());
      classpath.append(File.pathSeparator);
      
      return classpath.toString();
   }
   
   /** 
    * Resolve the location of the Checker class, and the retro runtime classes  
    *
    */
   public void resolveCheckerLocation() throws MojoExecutionException
   {
      // Figure out which verion of retro should be used
      String jbossRetroVersion = null;
      
      // If we are checking jboss retro itself, we have to do this to make sure 
      // the local files are used instead of repository files.
      if (project.getArtifactId().equals(JBOSS_RETRO_ARTIFACTID))
      {
         jbossRetroVersion = project.getVersion();
         
         // Determine if the checker and runtime are attached to the project 
         List attachedArtifacts = project.getAttachedArtifacts();
         for (Object artifactObj : attachedArtifacts) {
            Artifact artifact = (Artifact)artifactObj;
            if (artifact.getArtifactId().equals(JBOSS_RETRO_ARTIFACTID))
            {
               if (artifact.getClassifier().equals("retrocheck")) 
               {
                  this.jbossRetroCheckJar = artifact.getFile();
               }
               else if(artifact.getClassifier().equals("rt")) 
               {
                  this.jbossRetroRtJar = artifact.getFile();
               }
            }
         }
      }
      
      if (this.jbossRetroCheckJar != null && this.jbossRetroRtJar != null) 
      {
         // We found the checker and runtime attached, so just return
         return;
      }
      
      Iterator iter = this.pluginArtifacts.iterator();
      while(iter.hasNext() && jbossRetroVersion==null) {
         Artifact nextArtifact = (Artifact)iter.next();
         if (nextArtifact.getArtifactId().equals(JBOSS_RETRO_ARTIFACTID) 
               && nextArtifact.getClassifier() == null)
         {
            jbossRetroVersion = nextArtifact.getVersion();          
         }
      }

      // Get the checker and runtime from the repository
      Artifact checkerArtifact = artifactFactory.createArtifactWithClassifier("org.jboss", JBOSS_RETRO_ARTIFACTID, jbossRetroVersion, "jar", "retrocheck");            
      Artifact runtimeArtifact = artifactFactory.createArtifactWithClassifier("org.jboss", JBOSS_RETRO_ARTIFACTID, jbossRetroVersion, "jar", "rt");            
      try 
      {
         artifactResolver.resolve(checkerArtifact, project.getRemoteArtifactRepositories(), localRepository);
         artifactResolver.resolve(runtimeArtifact, project.getRemoteArtifactRepositories(), localRepository);
      }
      catch (ArtifactResolutionException are) 
      {
         throw new MojoExecutionException("Problem resolving artifact: " + are);
      }
      catch (ArtifactNotFoundException are) 
      {
         throw new MojoExecutionException("Problem resolving artifact: " + are);
      }
      
      this.jbossRetroCheckJar = checkerArtifact.getFile();
      this.jbossRetroRtJar = runtimeArtifact.getFile();
   }
   

   /**
    * Consume and log command output from the Checker
    * @author pgier
    *
    */
   public class MojoLogStreamConsumer implements StreamConsumer
   {
      public void consumeLine(String line)
      {
         getLog().info(line);
      }
   }

}







© 2015 - 2025 Weber Informatics LLC | Privacy Policy