
org.jboss.maven.plugins.retro.WeaveMojo 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.Collection;
import java.util.List;
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.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.jboss.maven.plugins.retro.util.JarUtil;
/**
* Maven plugin for JBoss Retro Weaver. This can be used to
* do byte code weaving on classes in jars and directories.
* By default the weaver will weave the main classes
* and output the result to a jar with the specified classifier.
*
* @phase process-classes
* @goal weave
*
*/
public class WeaveMojo extends AbstractWeaveMojo
{
/**
* The plugin dependencies.
*
* @parameter expression="${plugin.artifacts}"
* @required
* @readonly
*/
protected List pluginArtifacts;
/**
* The directory for compiled classes.
*
* @parameter expression="${project.build.outputDirectory}"
* @required
*/
protected File classesDirectory;
/**
* The directory where the weaved classes
* should be written.
* @parameter expression="${project.build.directory}/classes-weaved"
*/
protected File outputDirectory;
/**
* Attach a jar of the weaved classes to the build lifecycle. This will
* allow the weaved jar to be deployed with the other build artifacts.
* @parameter
*/
protected boolean attachJar = false;
/**
* Project classpath.
*
* @parameter expression="${project.compileClasspathElements}"
* @required
*/
protected List classpathElements;
/**
* Main plugin execution method
*/
public void execute() throws MojoFailureException, MojoExecutionException
{
if ( ( ! this.getClassesDirecotry().exists())
|| ( ! this.getClassesDirecotry().isDirectory()))
{
getLog().info("No classes found to weave.");
return;
}
this.getLog().info("Weaving classes in: " + this.getClassesDirecotry());
try
{
doWeave(this.buildWeaveClasspath(), this.generateWeaverArgs());
}
catch (Exception e)
{
throw new MojoExecutionException("Error during weave: " + e);
}
this.getLog().info("Weaving complete.");
if (this.attachJar)
{
String jarFilePath = project.getBuild().getDirectory()
+ File.separator + project.getArtifactId() + "-" + this.getJarClassifier() + ".jar";
try
{
File jarFile = JarUtil.createJarFile(this.getOutputPath(), jarFilePath);
projectHelper.attachArtifact(project, jarFile, this.getJarClassifier());
getLog().info("Weaved jar file created: " + jarFile.getAbsolutePath());
}
catch (IOException ioe)
{
getLog().warn("Unable to create Jar file: " + ioe);
}
}
}
protected String [] generateWeaverArgs()
{
ArrayList argsList = new ArrayList ();
if (this.verbose)
{
argsList.add("-verbose");
}
if (this.suppress)
{
argsList.add("-suppress");
}
if (this.useSystemClasspath)
{
argsList.add("-useSystemClasspath");
argsList.add(Boolean.toString(useSystemClasspath));
}
argsList.add("-cp");
argsList.add(buildWeaveClasspath());
if (this.weaverClass !=null)
{
argsList.add("-weaverClass");
argsList.add(this.weaverClass);
}
argsList.add("-outputDir");
argsList.add(this.getOutputPath());
argsList.add(this.getClassesDirecotry().getAbsolutePath());
String [] args = new String[argsList.size()];
return argsList.toArray(args);
}
/**
* Generates a classpath string based on the compile class path
* and the plugin dependencies.
* @return
*/
protected String buildWeaveClasspath() {
StringBuilder classpath = new StringBuilder();
List cpElements = this.getClasspathElements();
for (Object element : cpElements)
{
classpath.append(element);
classpath.append(pathSep);
}
// If the weaver classes can be found in the source directory
// (i.e. we are weaving jboss retro), then we don't want the
// retro jar from the plugin dependency to be pulled in from the repo.
boolean useRetroDep = true;
NameFileFilter nameFilter = new NameFileFilter("Weaver.class");
Collection weaverClassFiles = FileUtils.listFiles(this.getClassesDirecotry(), nameFilter, TrueFileFilter.INSTANCE);
if (weaverClassFiles.size() > 0)
{
useRetroDep = false;
}
for (Object artifactObj : pluginArtifacts)
{
try
{
Artifact artifact = (Artifact) artifactObj;
if (artifact.getFile() != null)
{
if ( useRetroDep || ( ! artifact.getArtifactId().equals(JBOSS_RETRO_ARTIFACTID)))
{
classpath.append(artifact.getFile().getCanonicalPath());
classpath.append(pathSep);
}
}
}
catch (IOException ioe)
{
this.getLog().warn("Could not get filename");
}
}
return classpath.toString();
}
protected String getOutputPath()
{
return this.outputDirectory.getAbsolutePath();
}
public File getClassesDirecotry()
{
return this.classesDirectory;
}
/**
* Get the list of classpath elements to use for weaving.
* @return
*/
public List getClasspathElements()
{
return this.classpathElements;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy