mirror of https://github.com/apache/maven.git
Adding mojos for loading classes, finding resources, and running BSH scripts, all according to the plugin's classpath.
git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@367448 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6ab6d2c0b5
commit
00e6c04044
|
@ -32,5 +32,10 @@
|
|||
<artifactId>jline</artifactId>
|
||||
<version>0.9.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>bsh</groupId>
|
||||
<artifactId>bsh</artifactId>
|
||||
<version>1.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</model>
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package org.apache.maven.plugin.coreit;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
|
||||
/**
|
||||
* @goal loadable
|
||||
* @requiresDependencyResolution test
|
||||
*/
|
||||
public class LoadableMojo
|
||||
extends AbstractMojo
|
||||
{
|
||||
/**
|
||||
* @parameter
|
||||
* @required
|
||||
*/
|
||||
private String className;
|
||||
|
||||
public void execute() throws MojoFailureException
|
||||
{
|
||||
if ( !load( true ) || !load( false ) )
|
||||
{
|
||||
throw new MojoFailureException( this, "Class-loading test failed..", "Failed to load class: " + className + " using one or more methods." );
|
||||
}
|
||||
}
|
||||
|
||||
private boolean load( boolean useContextClassloader ) throws MojoFailureException
|
||||
{
|
||||
getLog().info( "Executing in java version: " + System.getProperty( "java.version" ) );
|
||||
|
||||
ClassLoader cl;
|
||||
if ( useContextClassloader )
|
||||
{
|
||||
cl = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
else
|
||||
{
|
||||
cl = this.getClass().getClassLoader();
|
||||
}
|
||||
|
||||
getLog().info( "Attepting to load: " + className + " from: " + cl + (useContextClassloader ? " (context classloader)" : "" ) );
|
||||
|
||||
try
|
||||
{
|
||||
Class result = cl.loadClass( className );
|
||||
|
||||
getLog().info( "Load succeeded." );
|
||||
|
||||
return true;
|
||||
}
|
||||
catch ( ClassNotFoundException e )
|
||||
{
|
||||
getLog().info( "Failed to load class: " + className
|
||||
+ (useContextClassloader ? " using context classloader" : "") );
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package org.apache.maven.plugin.coreit;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* @goal reachable
|
||||
* @requiresDependencyResolution test
|
||||
*/
|
||||
public class ReachableMojo extends AbstractMojo
|
||||
{
|
||||
/**
|
||||
* @parameter
|
||||
* @required
|
||||
*/
|
||||
private String resource;
|
||||
|
||||
public void execute()
|
||||
throws MojoFailureException
|
||||
{
|
||||
if ( !reach( true ) || !reach( false ) )
|
||||
{
|
||||
throw new MojoFailureException( this, "Resource reachability test failed..", "Failed to reach resource: " + resource + " using one or more methods." );
|
||||
}
|
||||
}
|
||||
|
||||
public boolean reach( boolean useContextClassloader ) throws MojoFailureException
|
||||
{
|
||||
ClassLoader cl;
|
||||
if ( useContextClassloader )
|
||||
{
|
||||
cl = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
else
|
||||
{
|
||||
cl = this.getClass().getClassLoader();
|
||||
}
|
||||
|
||||
URL result = cl.getResource( resource );
|
||||
|
||||
getLog().info( "Attepting to reach: " + resource + " from: " + cl + (useContextClassloader ? " (context classloader)" : "" ) + ( result == null ? ": FAILED" : ":SUCCEEDED" ) );
|
||||
|
||||
if ( result == null )
|
||||
{
|
||||
getLog().info( "Cannot find resource: " + resource + (useContextClassloader?" in context classloader":"") );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package org.apache.maven.plugin.coreit;
|
||||
|
||||
import bsh.EvalError;
|
||||
import bsh.Interpreter;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
|
||||
/**
|
||||
* @goal runnable
|
||||
* @requiresDependencyResolution test
|
||||
*/
|
||||
public class RunnableMojo
|
||||
extends AbstractMojo
|
||||
{
|
||||
/**
|
||||
* @parameter
|
||||
* @required
|
||||
*/
|
||||
private String script;
|
||||
|
||||
public void execute() throws MojoFailureException
|
||||
{
|
||||
Interpreter terp = new Interpreter();
|
||||
|
||||
try
|
||||
{
|
||||
getLog().info( "Executing in java version: " + System.getProperty( "java.version" ) );
|
||||
|
||||
Class result = (Class) terp.eval( script );
|
||||
|
||||
getLog().info( "Result of script evaluation was: " + result + "\nLoaded from: " + result.getClassLoader() );
|
||||
}
|
||||
catch ( EvalError e )
|
||||
{
|
||||
throw new MojoFailureException( this, "Failed to evaluate script.", "Script: \n\n" + script
|
||||
+ "\n\nfailed to evaluate. Error: " + e.getMessage() + "\nLine: " + e.getErrorLineNumber() );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue