From da58bced254ca1381453b50779de98701a891c28 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Sun, 20 May 2012 22:19:41 +0000 Subject: [PATCH] [MPLUGIN-200] move maven-script to maven-plugin-tools. git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1340859 13f79535-47bb-0310-9956-ffa450edef68 --- maven-script/maven-script-ant/pom.xml | 79 ++++ .../script/ant/AntMojoComponentFactory.java | 39 ++ .../maven/script/ant/AntMojoWrapper.java | 378 ++++++++++++++++++ .../maven/script/ant/AntPropertyHelper.java | 207 ++++++++++ .../resources/META-INF/plexus/components.xml | 28 ++ .../maven-script-ant/src/site/site.xml | 36 ++ .../maven/script/ant/AntMojoWrapperTest.java | 287 +++++++++++++ .../resources/META-INF/maven/plugin-2.0.xml | 43 ++ .../resources/META-INF/maven/plugin-2.1.xml | 69 ++++ .../src/test/resources/test.build.xml | 15 + maven-script/maven-script-beanshell/pom.xml | 43 ++ .../beanshell/BeanshellMojoAdapter.java | 73 ++++ .../maven-script-beanshell/src/site/site.xml | 36 ++ maven-script/pom.xml | 37 ++ maven-script/src/site/site.xml | 30 ++ 15 files changed, 1400 insertions(+) create mode 100644 maven-script/maven-script-ant/pom.xml create mode 100644 maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoComponentFactory.java create mode 100644 maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java create mode 100644 maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java create mode 100644 maven-script/maven-script-ant/src/main/resources/META-INF/plexus/components.xml create mode 100644 maven-script/maven-script-ant/src/site/site.xml create mode 100644 maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java create mode 100644 maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml create mode 100644 maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml create mode 100644 maven-script/maven-script-ant/src/test/resources/test.build.xml create mode 100644 maven-script/maven-script-beanshell/pom.xml create mode 100644 maven-script/maven-script-beanshell/src/main/java/org/apache/maven/script/beanshell/BeanshellMojoAdapter.java create mode 100644 maven-script/maven-script-beanshell/src/site/site.xml create mode 100644 maven-script/pom.xml create mode 100644 maven-script/src/site/site.xml diff --git a/maven-script/maven-script-ant/pom.xml b/maven-script/maven-script-ant/pom.xml new file mode 100644 index 0000000..1b79ca8 --- /dev/null +++ b/maven-script/maven-script-ant/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + + org.apache.maven + maven-script + 2.2.2-RC1-SNAPSHOT + + + maven-script-ant + + Maven Ant Mojo Support + + + + org.apache.ant + ant + 1.7.1 + + + org.apache.ant + ant-launcher + 1.7.1 + + + org.apache.maven + maven-plugin-api + + + org.codehaus.plexus + plexus-ant-factory + 1.0-alpha-2.1 + + + org.codehaus.plexus + plexus-container-default + + + org.apache.maven + maven-project + + + org.apache.maven + maven-plugin-descriptor + + + org.codehaus.plexus + plexus-archiver + 1.0-alpha-11 + + + org.apache.maven + maven-core + + + easymock + easymock + test + + + diff --git a/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoComponentFactory.java b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoComponentFactory.java new file mode 100644 index 0000000..159bc16 --- /dev/null +++ b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoComponentFactory.java @@ -0,0 +1,39 @@ +package org.apache.maven.script.ant; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.codehaus.classworlds.ClassRealm; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.factory.ComponentInstantiationException; +import org.codehaus.plexus.component.factory.ant.AntComponentFactory; +import org.codehaus.plexus.component.factory.ant.AntScriptInvoker; +import org.codehaus.plexus.component.repository.ComponentDescriptor; + +public class AntMojoComponentFactory + extends AntComponentFactory +{ + + public Object newInstance( ComponentDescriptor descriptor, ClassRealm realm, PlexusContainer container ) + throws ComponentInstantiationException + { + return new AntMojoWrapper( (AntScriptInvoker) super.newInstance( descriptor, realm, container ) ); + } + +} diff --git a/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java new file mode 100644 index 0000000..1e42829 --- /dev/null +++ b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java @@ -0,0 +1,378 @@ +package org.apache.maven.script.ant; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.ContextEnabled; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.PluginParameterExpressionEvaluator; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.path.PathTranslator; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.PropertyHelper; +import org.apache.tools.ant.types.Path; +import org.codehaus.plexus.archiver.ArchiverException; +import org.codehaus.plexus.archiver.UnArchiver; +import org.codehaus.plexus.archiver.zip.ZipUnArchiver; +import org.codehaus.plexus.component.MapOrientedComponent; +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; +import org.codehaus.plexus.component.factory.ant.AntComponentExecutionException; +import org.codehaus.plexus.component.factory.ant.AntScriptInvoker; +import org.codehaus.plexus.component.repository.ComponentRequirement; +import org.codehaus.plexus.logging.LogEnabled; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + + +public class AntMojoWrapper + extends AbstractMojo + implements ContextEnabled, MapOrientedComponent, LogEnabled +{ + + private Map pluginContext; + + private final AntScriptInvoker scriptInvoker; + + private Project antProject; + + private MavenProject mavenProject; + + private MojoExecution mojoExecution; + + private MavenSession session; + + private PathTranslator pathTranslator; + + private Logger logger; + + private transient List unconstructedParts = new ArrayList(); + + public AntMojoWrapper( AntScriptInvoker scriptInvoker ) + { + this.scriptInvoker = scriptInvoker; + } + + public void execute() + throws MojoExecutionException + { + if ( antProject == null ) + { + antProject = scriptInvoker.getProject(); + } + + Map allConfig = new HashMap(); + if ( pluginContext != null && !pluginContext.isEmpty() ) + { + allConfig.putAll( pluginContext ); + } + + Map refs = scriptInvoker.getReferences(); + if ( refs != null ) + { + allConfig.putAll( refs ); + + for ( Iterator it = refs.entrySet().iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + String key = (String) entry.getKey(); + if ( key.startsWith( PathTranslator.class.getName() ) ) + { + pathTranslator = (PathTranslator) entry.getValue(); + } + } + } + + mavenProject = (MavenProject) allConfig.get( "project" ); + + mojoExecution = (MojoExecution) allConfig.get( "mojoExecution" ); + + session = (MavenSession) allConfig.get( "session" ); + + unpackFileBasedResources(); + + addClasspathReferences(); + + if ( logger.isDebugEnabled() && !unconstructedParts.isEmpty() ) + { + StringBuffer buffer = new StringBuffer(); + + buffer.append( "The following standard Maven Ant-mojo support objects could not be created:\n\n" ); + + for ( Iterator it = unconstructedParts.iterator(); it.hasNext(); ) + { + String part = (String) it.next(); + buffer.append( "\n- " ).append( part ); + } + + buffer.append( "\n\nMaven project, session, mojo-execution, or path-translation parameter information is " ); + buffer.append( "\nmissing from this mojo's plugin descriptor." ); + buffer.append( "\n\nPerhaps this Ant-based mojo depends on maven-script-ant < 2.1.0, " ); + buffer.append( "or used maven-plugin-tools-ant < 2.2 during release?\n\n" ); + + logger.debug( buffer.toString() ); + } + + try + { + scriptInvoker.invoke(); + } + catch ( AntComponentExecutionException e ) + { + throw new MojoExecutionException( "Failed to execute: " + e.getMessage(), e ); + } + + unconstructedParts.clear(); + } + + public void setPluginContext( Map pluginContext ) + { + this.pluginContext = pluginContext; + } + + public Map getPluginContext() + { + return pluginContext; + } + + public void addComponentRequirement( ComponentRequirement requirementDescriptor, Object requirementValue ) + throws ComponentConfigurationException + { + scriptInvoker.addComponentRequirement( requirementDescriptor, requirementValue ); + } + + public void setComponentConfiguration( Map componentConfiguration ) + throws ComponentConfigurationException + { + scriptInvoker.setComponentConfiguration( componentConfiguration ); + antProject = scriptInvoker.getProject(); + } + + private void unpackFileBasedResources() + throws MojoExecutionException + { + if ( mojoExecution == null || mavenProject == null ) + { + unconstructedParts.add( "Unpacked Ant build scripts (in Maven build directory)." ); + + return; + } + + // What we need to write out any resources in the plugin to the target directory of the + // mavenProject using the Ant-based plugin: + // + // 1. Need a reference to the plugin JAR itself + // 2. Need a reference to the ${basedir} of the mavenProject + + PluginDescriptor pluginDescriptor = mojoExecution.getMojoDescriptor().getPluginDescriptor(); + + File pluginJar = pluginDescriptor.getPluginArtifact().getFile(); + + String resourcesPath = pluginDescriptor.getArtifactId(); + + File outputDirectory = new File( mavenProject.getBuild().getDirectory() ); + + try + { + UnArchiver ua = new ZipUnArchiver( pluginJar ); + + ua.extract( resourcesPath, outputDirectory ); + } + catch ( ArchiverException e ) + { + throw new MojoExecutionException( "Error extracting resources from your Ant-based plugin.", e ); + } + } + + private void addClasspathReferences() + throws MojoExecutionException + { + try + { + if ( mavenProject != null && session != null && pathTranslator != null ) + { + ExpressionEvaluator exprEvaluator = + new PluginParameterExpressionEvaluator( session, mojoExecution, pathTranslator, logger, mavenProject, + mavenProject.getProperties() ); + + PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper( antProject ); + propertyHelper.setNext( new AntPropertyHelper( exprEvaluator, mavenProject.getArtifacts(), getLog() ) ); + } + else + { + unconstructedParts.add( "Maven parameter expression evaluator for Ant properties." ); + } + + if ( mavenProject != null ) + { + // Compile classpath + Path p = new Path( antProject ); + + p.setPath( StringUtils.join( mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator ) ); + + /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */ + scriptInvoker.getReferences().put( "maven.dependency.classpath", p ); + antProject.addReference( "maven.dependency.classpath", p ); + + scriptInvoker.getReferences().put( "maven.compile.classpath", p ); + antProject.addReference( "maven.compile.classpath", p ); + + // Runtime classpath + p = new Path( antProject ); + + p.setPath( StringUtils.join( mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator ) ); + + scriptInvoker.getReferences().put( "maven.runtime.classpath", p ); + antProject.addReference( "maven.runtime.classpath", p ); + + // Test classpath + p = new Path( antProject ); + + p.setPath( StringUtils.join( mavenProject.getTestClasspathElements().iterator(), File.pathSeparator ) ); + + scriptInvoker.getReferences().put( "maven.test.classpath", p ); + antProject.addReference( "maven.test.classpath", p ); + + } + else + { + unconstructedParts.add( "Maven standard project-based classpath references." ); + } + + if ( mojoExecution != null ) + { + // Plugin dependency classpath + + Path p = getPathFromArtifacts( mojoExecution.getMojoDescriptor().getPluginDescriptor().getArtifacts(), antProject ); + + scriptInvoker.getReferences().put( "maven.plugin.classpath", p ); + antProject.addReference( "maven.plugin.classpath", p ); + } + else + { + unconstructedParts.add( "Maven standard plugin-based classpath references." ); + } + } + catch ( DependencyResolutionRequiredException e ) + { + throw new MojoExecutionException( "Error creating classpath references for Ant-based plugin scripts.", e ); + } + } + + public Path getPathFromArtifacts( Collection artifacts, + Project antProject ) + throws DependencyResolutionRequiredException + { + List list = new ArrayList( artifacts.size() ); + + for ( Iterator i = artifacts.iterator(); i.hasNext(); ) + { + Artifact a = (Artifact) i.next(); + + File file = a.getFile(); + + if ( file == null ) + { + throw new DependencyResolutionRequiredException( a ); + } + + list.add( file.getPath() ); + } + + Path p = new Path( antProject ); + + p.setPath( StringUtils.join( list.iterator(), File.pathSeparator ) ); + + return p; + } + + public Project getAntProject() + { + return antProject; + } + + public void setAntProject( Project antProject ) + { + this.antProject = antProject; + } + + public MavenProject getMavenProject() + { + return mavenProject; + } + + public void setMavenProject( MavenProject mavenProject ) + { + this.mavenProject = mavenProject; + } + + public MojoExecution getMojoExecution() + { + return mojoExecution; + } + + public void setMojoExecution( MojoExecution mojoExecution ) + { + this.mojoExecution = mojoExecution; + } + + public MavenSession getSession() + { + return session; + } + + public void setSession( MavenSession session ) + { + this.session = session; + } + + public PathTranslator getPathTranslator() + { + return pathTranslator; + } + + public void setPathTranslator( PathTranslator pathTranslator ) + { + this.pathTranslator = pathTranslator; + } + + public AntScriptInvoker getScriptInvoker() + { + return scriptInvoker; + } + + public void enableLogging( Logger logger ) + { + this.logger = logger; + } +} diff --git a/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java new file mode 100644 index 0000000..ef7aba7 --- /dev/null +++ b/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java @@ -0,0 +1,207 @@ +package org.apache.maven.script.ant; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.Collections; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import org.apache.tools.ant.PropertyHelper; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; +import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; + +/** + * Makes the ${expressions} used in Maven available to Ant as properties. + * + * @author Kenney Westerhof + */ +public class AntPropertyHelper + extends PropertyHelper +{ + private static final String DEPENDENCY_PREFIX = "maven.dependency."; + private Log log; + private ExpressionEvaluator exprEvaluator; + private MavenProject mavenProject; + private Map artifactMap = new Hashtable(); + + /** + * @deprecated use the other constructor + * @param project + * @param l + */ + public AntPropertyHelper( MavenProject project, Log l ) + { + mavenProject = project; + log = l; + } + + /** + * @deprecated use {@link #AntPropertyHelper(ExpressionEvaluator, Set, Log)} to resolve maven.dependency.* properties + * @param exprEvaluator + * @param l + */ + public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l ) + { + this( exprEvaluator, Collections.EMPTY_SET, l ); + } + + /** + * @param exprEvaluator + * @param artifacts + * @param l + */ + public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set artifacts, Log l ) + { + this.mavenProject = null; + this.exprEvaluator = exprEvaluator; + this.log = l; + + for ( Iterator it = artifacts.iterator(); it.hasNext(); ) + { + Artifact artifact = (Artifact) it.next(); + + String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId() + + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" ) + + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path"; + + log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() ); + + artifactMap.put( key, artifact.getFile().getPath() ); + } + } + + /** + * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean) + */ + public synchronized Object getPropertyHook( String ns, String name, boolean user ) + { + if ( log.isDebugEnabled() ) + { + log.debug( "getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")" ); + } + + /* keep old behaviour */ + if ( mavenProject != null ) + { + return getPropertyHook( ns, name, user, mavenProject ); + } + + + Object val = null; + + if ( name.startsWith( DEPENDENCY_PREFIX ) ) + { + val = (String) artifactMap.get( name ); + } + + if ( val == null ) + { + try + { + val = exprEvaluator.evaluate( "${" + name + "}" ); + } + catch ( ExpressionEvaluationException e ) + { + if ( log.isErrorEnabled() ) + { + log.error( "Failed to evaluate expression", e ); + } + } + } + + if ( val == null ) + { + val = super.getPropertyHook( ns, name, user ); + + if ( val == null ) + { + val = System.getProperty( name.toString() ); + } + } + + return val; + } + + /** + * @deprecated added to keep backwards compatibility + * @param ns + * @param name + * @param user + * @param mavenProject + * @return The property value. + */ + private Object getPropertyHook( String ns, String name, boolean user, MavenProject mavenProject ) + { + Object val = null; + try + { + if ( name.startsWith( DEPENDENCY_PREFIX ) ) + { + val = (String) artifactMap.get( name ); + } + else if ( name.startsWith( "project." ) ) + { + val = ReflectionValueExtractor.evaluate( + name, + mavenProject, + true + ); + } + else if ( name.equals( "basedir" ) ) + { + val = ReflectionValueExtractor.evaluate( + "basedir.path", + mavenProject, + false + ); + } + } + catch ( Exception e ) + { + if ( log.isWarnEnabled() ) + { + log.warn( "Error evaluating expression '" + name + "'", e ); + } + } + + if ( val == null ) + { + val = super.getPropertyHook( ns, name, user ); + if ( val == null ) + { + val = System.getProperty( name.toString() ); + } + } + + if ( val instanceof File ) + { + val = ( (File) val ).getAbsoluteFile(); + } + + return val; + } +} diff --git a/maven-script/maven-script-ant/src/main/resources/META-INF/plexus/components.xml b/maven-script/maven-script-ant/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000..cadddea --- /dev/null +++ b/maven-script/maven-script-ant/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,28 @@ + + + + + + org.codehaus.plexus.component.factory.ComponentFactory + org.apache.maven.script.ant.AntMojoComponentFactory + ant-mojo + + + diff --git a/maven-script/maven-script-ant/src/site/site.xml b/maven-script/maven-script-ant/src/site/site.xml new file mode 100644 index 0000000..3a16bf9 --- /dev/null +++ b/maven-script/maven-script-ant/src/site/site.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java b/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java new file mode 100644 index 0000000..17135f7 --- /dev/null +++ b/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java @@ -0,0 +1,287 @@ +package org.apache.maven.script.ant; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Build; +import org.apache.maven.model.Model; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.path.PathTranslator; +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.BuildListener; +import org.codehaus.plexus.archiver.ArchiverException; +import org.codehaus.plexus.archiver.jar.JarArchiver; +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; +import org.codehaus.plexus.component.factory.ComponentInstantiationException; +import org.codehaus.plexus.component.factory.ant.AntScriptInvoker; +import org.codehaus.plexus.component.repository.ComponentRequirement; +import org.codehaus.plexus.configuration.PlexusConfigurationException; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.logging.console.ConsoleLogger; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; +import org.easymock.MockControl; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.Reader; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +public class AntMojoWrapperTest + extends TestCase +{ + + public void test2xStylePlugin() + throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException, + ComponentConfigurationException, ArchiverException + { + String pluginXml = "META-INF/maven/plugin-2.1.xml"; + + List messages = run( pluginXml, true ); + + assertPresence( messages, "Unpacked Ant build scripts (in Maven build directory).", false ); + assertPresence( messages, "Maven parameter expression evaluator for Ant properties.", false ); + assertPresence( messages, "Maven standard project-based classpath references.", false ); + assertPresence( messages, "Maven standard plugin-based classpath references.", false ); + assertPresence( messages, + "Maven project, session, mojo-execution, or path-translation parameter information is", false ); + assertPresence( messages, "maven-script-ant < 2.1.0, or used maven-plugin-tools-ant < 2.2 during release", + false ); + + assertPresence( messages, "path-is-missing", false ); + } + + public void test20StylePlugin() + throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException, + ComponentConfigurationException, ArchiverException + { + String pluginXml = "META-INF/maven/plugin-2.0.xml"; + + List messages = run( pluginXml, false ); + + assertPresence( messages, "Unpacked Ant build scripts (in Maven build directory).", true ); + assertPresence( messages, "Maven parameter expression evaluator for Ant properties.", true ); + assertPresence( messages, "Maven standard project-based classpath references.", true ); + assertPresence( messages, "Maven standard plugin-based classpath references.", true ); + assertPresence( messages, + "Maven project, session, mojo-execution, or path-translation parameter information is", true ); + assertPresence( messages, "maven-script-ant < 2.1.0, or used maven-plugin-tools-ant < 2.2 during release", true ); + + assertPresence( messages, "path-is-missing", true ); + } + + private void assertPresence( List messages, String test, boolean shouldBePresent ) + { + boolean found = false; + + for ( Iterator it = messages.iterator(); it.hasNext(); ) + { + String message = (String) it.next(); + if ( message.indexOf( test ) > -1 ) + { + found = true; + break; + } + } + + if ( !shouldBePresent && found ) + { + fail( "Test string: '" + test + "' was found in output, but SHOULD NOT BE THERE." ); + } + else if ( shouldBePresent && !found ) + { + fail( "Test string: '" + test + "' was NOT found in output, but SHOULD BE THERE." ); + } + } + + private List run( String pluginXml, boolean includeImplied ) + throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException, + ComponentConfigurationException, ArchiverException + { + StackTraceElement stack = new Throwable().getStackTrace()[1]; + System.out.println( "\n\nRunning: " + stack.getMethodName() + "\n\n" ); + + URL resource = Thread.currentThread().getContextClassLoader().getResource( pluginXml ); + + if ( resource == null ) + { + fail( "plugin descriptor not found: '" + pluginXml + "'." ); + } + + Reader reader = null; + PluginDescriptor pd; + try + { + reader = new InputStreamReader( resource.openStream() ); + pd = new PluginDescriptorBuilder().build( reader, pluginXml ); + } + finally + { + IOUtil.close( reader ); + } + + Map config = new HashMap(); + config.put( "basedir", new File( "." ).getAbsoluteFile() ); + config.put( "messageLevel", "info" ); + + MojoDescriptor md = pd.getMojo( "test" ); + + AntMojoWrapper wrapper = + new AntMojoWrapper( new AntScriptInvoker( md, Thread.currentThread().getContextClassLoader() ) ); + + wrapper.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) ); + + MockControl artifactCtl = null; + MockControl pathTranslatorCtl = null; + if ( includeImplied ) + { + File pluginXmlFile = new File( StringUtils.replace( resource.getPath(), "%20", " " ) ); + + File jarFile = File.createTempFile( "AntMojoWrapperTest.", ".test.jar" ); + jarFile.deleteOnExit(); + + JarArchiver archiver = new JarArchiver(); + archiver.enableLogging( new ConsoleLogger( Logger.LEVEL_ERROR, "archiver" ) ); + archiver.setDestFile( jarFile ); + archiver.addFile( pluginXmlFile, pluginXml ); + archiver.createArchive(); + + artifactCtl = MockControl.createControl( Artifact.class ); + Artifact artifact = (Artifact) artifactCtl.getMock(); + + artifact.getFile(); + artifactCtl.setReturnValue( jarFile, MockControl.ZERO_OR_MORE ); + + artifact.getGroupId(); + artifactCtl.setReturnValue( "groupId", MockControl.ZERO_OR_MORE ); + + artifact.getArtifactId(); + artifactCtl.setReturnValue( "artifactId", MockControl.ZERO_OR_MORE ); + + artifact.getVersion(); + artifactCtl.setReturnValue( "1", MockControl.ZERO_OR_MORE ); + + artifact.getId(); + artifactCtl.setReturnValue( "groupId:artifactId:jar:1", MockControl.ZERO_OR_MORE ); + + artifact.getClassifier(); + artifactCtl.setReturnValue( null, MockControl.ZERO_OR_MORE ); + + pathTranslatorCtl = MockControl.createControl( PathTranslator.class ); + PathTranslator pt = (PathTranslator) pathTranslatorCtl.getMock(); + + Model model = new Model(); + + Build build = new Build(); + build.setDirectory( "target" ); + + model.setBuild( build ); + + MavenProject project = new MavenProject( model ); + project.setFile( new File( "pom.xml" ).getAbsoluteFile() ); + + artifactCtl.replay(); + pathTranslatorCtl.replay(); + + pd.setPluginArtifact( artifact ); + pd.setArtifacts( Collections.singletonList( artifact ) ); + + config.put( "project", project ); + config.put( "session", new MavenSession( null, null, null, null, null, null, null, null, null, null ) ); + config.put( "mojoExecution", new MojoExecution( md ) ); + + ComponentRequirement cr = new ComponentRequirement(); + cr.setRole( PathTranslator.class.getName() ); + + wrapper.addComponentRequirement( cr, pt ); + } + + wrapper.setComponentConfiguration( config ); + + TestBuildListener tbl = new TestBuildListener(); + wrapper.getAntProject().addBuildListener( tbl ); + + PrintStream oldOut = System.out; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try + { + System.setOut( new PrintStream( baos ) ); + + wrapper.execute(); + } + finally + { + System.setOut( oldOut ); + } + + System.out.println( "\n\n" + stack.getMethodName() + " executed; verifying...\n\n" ); + + if ( includeImplied ) + { + artifactCtl.verify(); + pathTranslatorCtl.verify(); + } + + List messages = new ArrayList(); + if ( !tbl.messages.isEmpty() ) + { + messages.addAll( tbl.messages ); + } + + messages.add( new String( baos.toByteArray() ) ); + + return messages; + } + + private static final class TestBuildListener + implements BuildListener + { + private List messages = new ArrayList(); + + public void buildFinished( BuildEvent arg0 ) + { + } + + public void buildStarted( BuildEvent arg0 ) + { + } + + public void messageLogged( BuildEvent event ) + { + messages.add( event.getMessage() ); + } + + public void targetFinished( BuildEvent arg0 ) + { + } + + public void targetStarted( BuildEvent arg0 ) + { + } + + public void taskFinished( BuildEvent arg0 ) + { + } + + public void taskStarted( BuildEvent arg0 ) + { + } + }; + +} diff --git a/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml b/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml new file mode 100644 index 0000000..c66cf12 --- /dev/null +++ b/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml @@ -0,0 +1,43 @@ + + Test Plugin + org.myplugin + my-plugin + 1 + myplugin + false + true + + + test + Build a JAR from the current project. + false + false + false + false + false + true + test.build.xml:test + ant + per-lookup + once-per-session + + + basedir + true + false + java.io.File + + + messageLevel + true + false + java.lang.String + + + + ${basedir} + ${messageLevel} + + + + \ No newline at end of file diff --git a/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml b/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml new file mode 100644 index 0000000..b4ebf00 --- /dev/null +++ b/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml @@ -0,0 +1,69 @@ + + Test Plugin + org.myplugin + my-plugin + 1 + myplugin + false + true + + + test + Build a JAR from the current project. + false + false + false + false + false + true + test.build.xml:test + ant + per-lookup + once-per-session + + + basedir + true + false + java.io.File + + + messageLevel + true + false + java.lang.String + + + project + true + false + org.apache.maven.project.MavenProject + + + session + true + false + org.apache.maven.execution.MavenSession + + + mojoExecution + true + false + org.apache.maven.plugin.MojoExecution + + + + ${project} + ${session} + ${mojoExecution} + ${basedir} + ${messageLevel} + + + + org.apache.maven.project.path.PathTranslator + + + + + \ No newline at end of file diff --git a/maven-script/maven-script-ant/src/test/resources/test.build.xml b/maven-script/maven-script-ant/src/test/resources/test.build.xml new file mode 100644 index 0000000..5f47054 --- /dev/null +++ b/maven-script/maven-script-ant/src/test/resources/test.build.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + plugin classpath is: ${cp} + + \ No newline at end of file diff --git a/maven-script/maven-script-beanshell/pom.xml b/maven-script/maven-script-beanshell/pom.xml new file mode 100644 index 0000000..9d684fe --- /dev/null +++ b/maven-script/maven-script-beanshell/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + + org.apache.maven + maven-script + 2.2.2-RC1-SNAPSHOT + + + maven-script-beanshell + + Maven Beanshell Mojo Support + + + + org.apache.maven + maven-plugin-api + + + org.codehaus.plexus + plexus-bsh-factory + 1.0-alpha-7 + + + diff --git a/maven-script/maven-script-beanshell/src/main/java/org/apache/maven/script/beanshell/BeanshellMojoAdapter.java b/maven-script/maven-script-beanshell/src/main/java/org/apache/maven/script/beanshell/BeanshellMojoAdapter.java new file mode 100644 index 0000000..8ec8cad --- /dev/null +++ b/maven-script/maven-script-beanshell/src/main/java/org/apache/maven/script/beanshell/BeanshellMojoAdapter.java @@ -0,0 +1,73 @@ +package org.apache.maven.script.beanshell; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import bsh.EvalError; +import bsh.Interpreter; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.Mojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.component.factory.bsh.BshComponent; + +/** + * Mojo adapter for a Beanshell Mojo. + * + * @todo should log be passed in, or rely on getLog() ? + * + * @author Brett Porter + * @version $Id$ + */ +public class BeanshellMojoAdapter + extends AbstractMojo + implements BshComponent +{ + private Mojo mojo; + + private Interpreter interpreter; + + public BeanshellMojoAdapter( Mojo mojo, Interpreter interpreter ) + { + this.mojo = mojo; + this.interpreter = interpreter; + } + + public void execute() + throws MojoExecutionException, MojoFailureException + { + try + { + interpreter.set( "logger", getLog() ); + + // TODO: set out, err to a print stream that will log at info, error respectively + } + catch ( EvalError evalError ) + { + throw new MojoExecutionException( "Unable to establish mojo", evalError ); + } + + mojo.execute(); + } + + public Interpreter getInterpreter() + { + return interpreter; + } +} diff --git a/maven-script/maven-script-beanshell/src/site/site.xml b/maven-script/maven-script-beanshell/src/site/site.xml new file mode 100644 index 0000000..3a16bf9 --- /dev/null +++ b/maven-script/maven-script-beanshell/src/site/site.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/maven-script/pom.xml b/maven-script/pom.xml new file mode 100644 index 0000000..d672e9e --- /dev/null +++ b/maven-script/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + + org.apache.maven + maven + 2.2.2-RC1-SNAPSHOT + + + maven-script + pom + + Maven Script Support Root + + + maven-script-ant + maven-script-beanshell + + diff --git a/maven-script/src/site/site.xml b/maven-script/src/site/site.xml new file mode 100644 index 0000000..67aa11c --- /dev/null +++ b/maven-script/src/site/site.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file