[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
master
Olivier Lamy 2012-05-20 22:19:41 +00:00
parent eeb238a4f3
commit da58bced25
15 changed files with 1400 additions and 0 deletions

View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven-script</artifactId>
<version>2.2.2-RC1-SNAPSHOT</version>
</parent>
<artifactId>maven-script-ant</artifactId>
<name>Maven Ant Mojo Support</name>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-launcher</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-ant-factory</artifactId>
<version>1.0-alpha-2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-descriptor</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>1.0-alpha-11</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</dependency>
<dependency>
<groupId>easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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 ) );
}
}

View File

@ -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;
}
}

View File

@ -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 <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
*/
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;
}
}

View File

@ -0,0 +1,28 @@
<!--
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.
-->
<component-set>
<components>
<component>
<role>org.codehaus.plexus.component.factory.ComponentFactory</role>
<implementation>org.apache.maven.script.ant.AntMojoComponentFactory</implementation>
<role-hint>ant-mojo</role-hint>
</component>
</components>
</component-set>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/DECORATION/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd">
<body>
<menu name="Overview">
<item name="Introduction" href="index.html"/>
<item name="JavaDocs" href="apidocs/index.html"/>
<item name="Source Xref" href="xref/index.html"/>
<!--item name="FAQ" href="faq.html"/-->
</menu>
<menu ref="parent"/>
<menu ref="reports"/>
</body>
</project>

View File

@ -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 )
{
}
};
}

View File

@ -0,0 +1,43 @@
<plugin>
<description>Test Plugin</description>
<groupId>org.myplugin</groupId>
<artifactId>my-plugin</artifactId>
<version>1</version>
<goalPrefix>myplugin</goalPrefix>
<isolatedRealm>false</isolatedRealm>
<inheritedByDefault>true</inheritedByDefault>
<mojos>
<mojo>
<goal>test</goal>
<description>Build a JAR from the current project.</description>
<requiresDirectInvocation>false</requiresDirectInvocation>
<requiresProject>false</requiresProject>
<requiresReports>false</requiresReports>
<aggregator>false</aggregator>
<requiresOnline>false</requiresOnline>
<inheritedByDefault>true</inheritedByDefault>
<implementation>test.build.xml:test</implementation>
<language>ant</language>
<instantiationStrategy>per-lookup</instantiationStrategy>
<executionStrategy>once-per-session</executionStrategy>
<parameters>
<parameter>
<name>basedir</name>
<required>true</required>
<editable>false</editable>
<type>java.io.File</type>
</parameter>
<parameter>
<name>messageLevel</name>
<required>true</required>
<editable>false</editable>
<type>java.lang.String</type>
</parameter>
</parameters>
<configuration>
<basedir implementation="java.io.File">${basedir}</basedir>
<messageLevel implementation="java.lang.String">${messageLevel}</messageLevel>
</configuration>
</mojo>
</mojos>
</plugin>

View File

@ -0,0 +1,69 @@
<plugin>
<description>Test Plugin</description>
<groupId>org.myplugin</groupId>
<artifactId>my-plugin</artifactId>
<version>1</version>
<goalPrefix>myplugin</goalPrefix>
<isolatedRealm>false</isolatedRealm>
<inheritedByDefault>true</inheritedByDefault>
<mojos>
<mojo>
<goal>test</goal>
<description>Build a JAR from the current project.</description>
<requiresDirectInvocation>false</requiresDirectInvocation>
<requiresProject>false</requiresProject>
<requiresReports>false</requiresReports>
<aggregator>false</aggregator>
<requiresOnline>false</requiresOnline>
<inheritedByDefault>true</inheritedByDefault>
<implementation>test.build.xml:test</implementation>
<language>ant</language>
<instantiationStrategy>per-lookup</instantiationStrategy>
<executionStrategy>once-per-session</executionStrategy>
<parameters>
<parameter>
<name>basedir</name>
<required>true</required>
<editable>false</editable>
<type>java.io.File</type>
</parameter>
<parameter>
<name>messageLevel</name>
<required>true</required>
<editable>false</editable>
<type>java.lang.String</type>
</parameter>
<parameter>
<name>project</name>
<required>true</required>
<editable>false</editable>
<type>org.apache.maven.project.MavenProject</type>
</parameter>
<parameter>
<name>session</name>
<required>true</required>
<editable>false</editable>
<type>org.apache.maven.execution.MavenSession</type>
</parameter>
<parameter>
<name>mojoExecution</name>
<required>true</required>
<editable>false</editable>
<type>org.apache.maven.plugin.MojoExecution</type>
</parameter>
</parameters>
<configuration>
<project implementation="org.apache.maven.project.MavenProject">${project}</project>
<session implementation="org.apache.maven.execution.MavenSession">${session}</session>
<mojoExecution implementation="org.apache.maven.plugin.MojoExecution">${mojoExecution}</mojoExecution>
<basedir implementation="java.io.File">${basedir}</basedir>
<messageLevel implementation="java.lang.String">${messageLevel}</messageLevel>
</configuration>
<requirements>
<requirement>
<role>org.apache.maven.project.path.PathTranslator</role>
</requirement>
</requirements>
</mojo>
</mojos>
</plugin>

View File

@ -0,0 +1,15 @@
<project>
<target name="init-cp" unless="cp-exists">
<path id="maven.plugin.classpath" location="path-is-missing"/>
</target>
<target name="test">
<condition property="cp-exists">
<isreference refid="maven.plugin.classpath"/>
</condition>
<antcall target="init-cp" inheritall="true"/>
<property name="cp" refId="maven.plugin.classpath"/>
<echo>plugin classpath is: ${cp}</echo>
</target>
</project>

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven-script</artifactId>
<version>2.2.2-RC1-SNAPSHOT</version>
</parent>
<artifactId>maven-script-beanshell</artifactId>
<name>Maven Beanshell Mojo Support</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-bsh-factory</artifactId>
<version>1.0-alpha-7</version>
</dependency>
</dependencies>
</project>

View File

@ -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 <a href="mailto:brett@apache.org">Brett Porter</a>
* @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;
}
}

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/DECORATION/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd">
<body>
<menu name="Overview">
<item name="Introduction" href="index.html"/>
<item name="JavaDocs" href="apidocs/index.html"/>
<item name="Source Xref" href="xref/index.html"/>
<!--item name="FAQ" href="faq.html"/-->
</menu>
<menu ref="parent"/>
<menu ref="reports"/>
</body>
</project>

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven</artifactId>
<version>2.2.2-RC1-SNAPSHOT</version>
</parent>
<artifactId>maven-script</artifactId>
<packaging>pom</packaging>
<name>Maven Script Support Root</name>
<modules>
<module>maven-script-ant</module>
<module>maven-script-beanshell</module>
</modules>
</project>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/DECORATION/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd">
<body>
<menu ref="parent"/>
<menu ref="modules"/>
<menu ref="reports"/>
</body>
</project>