Author: jdcasey Date: Mon Jan 9 17:11:31 2006 New Revision: 367448 URL: http://svn.apache.org/viewcvs?rev=367448&view=rev Log: Adding mojos for loading classes, finding resources, and running BSH scripts, all according to the plugin's classpath.
Added: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java (with props) maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java (with props) maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java (with props) Modified: maven/components/trunk/integration-tests/maven-core-it-plugin/pom.xml Modified: maven/components/trunk/integration-tests/maven-core-it-plugin/pom.xml URL: http://svn.apache.org/viewcvs/maven/components/trunk/integration-tests/maven-core-it-plugin/pom.xml?rev=367448&r1=367447&r2=367448&view=diff ============================================================================== --- maven/components/trunk/integration-tests/maven-core-it-plugin/pom.xml (original) +++ maven/components/trunk/integration-tests/maven-core-it-plugin/pom.xml Mon Jan 9 17:11:31 2006 @@ -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> Added: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java?rev=367448&view=auto ============================================================================== --- maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java (added) +++ maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java Mon Jan 9 17:11:31 2006 @@ -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; + } + } +} Propchange: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/LoadableMojo.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java?rev=367448&view=auto ============================================================================== --- maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java (added) +++ maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java Mon Jan 9 17:11:31 2006 @@ -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; + } +} Propchange: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ReachableMojo.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java?rev=367448&view=auto ============================================================================== --- maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java (added) +++ maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java Mon Jan 9 17:11:31 2006 @@ -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() ); + } + } +} Propchange: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/RunnableMojo.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision"