Donating maven-antrun-plugin to the Maven2 project, as discussed with Brett.

Enjoy! :-)

TODO:

o make <echo>...</echo> output visible.

o devise a way to pass on maven2 properties to <ant/>-called build.xml files.
  The ant code just copies all properties from the default PropertyHandler,
  however with m2 that's not possible since they are resolved/evaluated at
  runtime.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@231230 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kenney Westerhof 2005-08-10 13:38:21 +00:00
parent 53865c867f
commit 7fa0cc72c3
9 changed files with 620 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<model
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/maven-v4_0_0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>maven-plugin-parent</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.0-beta-1-SNAPSHOT</version>
</parent>
<artifactId>maven-antrun-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-alpha-1-SNAPSHOT</version>
<name>Maven AntRun Plugin</name>
<description>Runs ant scripts embedded in the POM</description>
<developers>
<developer>
<id>kenney</id>
<name>Kenney Westerhof</name>
<email>kenney@apache.org</email>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.6.5</version>
</dependency>
<!-- needed for launching a build.xml file -->
<dependency>
<groupId>ant</groupId>
<artifactId>ant-launcher</artifactId>
<version>1.6.5</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</model>

View File

@ -0,0 +1,77 @@
package org.apache.maven.plugin.antrun;
/*
* Copyright 2004-2005 The Apache Software Foundation.
*
* Licensed 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.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.PropertyHelper;
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 Log log;
private MavenProject mavenProject;
public AntPropertyHelper( MavenProject project, Log l )
{
mavenProject = project;
log = l;
}
public synchronized Object getPropertyHook( String ns, String name,
boolean user
)
{
log.debug( "getProperty(ns="+ns+", name="+name+", user="+user+")" );
try
{
if ( name.startsWith( "project." ) || name.equals( "basedir" ) )
{
Object val = ReflectionValueExtractor.evaluate(
name.substring( "project.".length() ),
mavenProject
);
if ( val != null )
{
return val;
}
}
}
catch ( Exception e )
{
log.warn( "Error evaluating expression '" + name + "'", e );
e.printStackTrace();
}
Object val = super.getPropertyHook( ns, name, user );
if ( val == null )
{
val = System.getProperty( name.toString() );
}
return val;
}
}

View File

@ -0,0 +1,89 @@
package org.apache.maven.plugin.antrun;
/*
* Copyright 2005 The Apache Software Foundation.
*
* Licensed 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.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.Target;
/**
* Maven AntRun Mojo.
*
* This plugin provides the capability of calling ant tasks
* from a POM. It is encouraged to move the actual tasks to
* a separate build.xml file and call that file with an
* &lt;ant/&gt; task.
*
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
*
* @configurator override
*
* @goal run
*
* @description Runs the nested ant tasks
*
*/
public class AntRunMojo
extends AbstractMojo
{
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* @parameter expression="${tasks}"
*/
private Target tasks;
/**
*/
public void execute()
throws MojoExecutionException
{
try
{
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(
tasks.getProject()
);
propertyHelper.setNext(
new AntPropertyHelper( project, getLog() )
);
DefaultLogger antLogger = new DefaultLogger();
antLogger.setOutputPrintStream( System.out );
antLogger.setErrorPrintStream( System.err );
tasks.getProject().addBuildListener( antLogger );
getLog().info( "Executing tasks" );
tasks.execute();
getLog().info( "Executed tasks" );
}
catch ( Exception e )
{
throw new MojoExecutionException( "Error executing ant tasks", e );
}
}
}

View File

@ -0,0 +1,202 @@
package org.apache.maven.plugin.antrun.components;
/*
* Copyright 2004-2005 The Apache Software Foundation.
*
* Licensed 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.tools.ant.ComponentHelper;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.RuntimeConfigurable;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.UnknownElement;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.PlexusConfigurationException;
/**
* Plexus ConfigurationConverter to set up Ant Target component fields.
*
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
*/
public class AntTargetConverter
extends AbstractConfigurationConverter
{
public static final String ROLE = ConfigurationConverter.class.getName();
public boolean canConvert( Class type )
{
return Target.class.isAssignableFrom( type );
}
public Object fromConfiguration(
ConverterLookup converterLookup, PlexusConfiguration configuration,
Class type, Class baseType, ClassLoader classLoader,
ExpressionEvaluator expressionEvaluator
)
throws ComponentConfigurationException
{
Object retValue = fromExpression( configuration, expressionEvaluator, type );
if ( retValue != null )
{
return retValue;
}
Class implementation = getClassForImplementationHint( type, configuration, classLoader );
retValue = instantiateObject( implementation );
processConfiguration(
(Target) retValue, classLoader, configuration, expressionEvaluator
);
return retValue;
}
private void processConfiguration(
Target target, ClassLoader classLoader,
PlexusConfiguration configuration,
ExpressionEvaluator expressionEvaluator
)
throws ComponentConfigurationException
{
Project project = new Project();
project.setName( "DummyProject" );
target.setName( "" );
target.setProject( project );
project.addTarget( target );
initDefinitions( project, target );
processConfiguration(
null, project, target, configuration, expressionEvaluator
);
}
private void processConfiguration(
RuntimeConfigurable parentWrapper, Project project, Target target,
PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator
)
throws ComponentConfigurationException
{
int items = configuration.getChildCount();
Object parent = parentWrapper == null ? null : parentWrapper.getProxy();
for ( int i = 0; i < items; i++ )
{
PlexusConfiguration childConfiguration = configuration.getChild( i );
UnknownElement task = new UnknownElement(
childConfiguration.getName()
);
task.setProject( project );
task.setNamespace( "" );
task.setQName( childConfiguration.getName() );
task.setTaskType( childConfiguration.getName() );
task.setTaskName( childConfiguration.getName() );
task.setOwningTarget( target );
if ( parent != null )
{
( (UnknownElement) parent ).addChild( task );
}
else
{
target.addTask( task );
}
RuntimeConfigurable wrapper = new RuntimeConfigurable(
task, task.getTaskName()
);
try
{
if ( childConfiguration.getValue() != null )
{
wrapper.addText( childConfiguration.getValue() );
}
}
catch ( PlexusConfigurationException e )
{
throw new ComponentConfigurationException(
"Error reading text value from element '" +
childConfiguration.getName() + "'", e
);
}
String [] attrNames = childConfiguration.getAttributeNames();
for ( int a = 0; a < attrNames.length; a++ )
{
try
{
String v = childConfiguration.getAttribute( attrNames[a] );
try
{
Object evaluatedExpr = expressionEvaluator.evaluate( v );
v = evaluatedExpr == null ? v : evaluatedExpr.toString();
}
catch ( ExpressionEvaluationException e )
{
throw new ComponentConfigurationException
(
"Error evaluating value '" + v + "' of attribute '" + attrNames[a] +
"' of tag '" + childConfiguration.getName() + "'", e
);
}
wrapper.setAttribute( attrNames[a], v );
}
catch ( PlexusConfigurationException e )
{
throw new ComponentConfigurationException(
"Error getting attribute '" + attrNames[a] +
"' of tag '" + childConfiguration.getName() + "'", e
);
}
}
if ( parentWrapper != null )
{
parentWrapper.addChild( wrapper );
}
processConfiguration(
wrapper, project, target,
childConfiguration, expressionEvaluator
);
}
}
protected void initDefinitions( Project project, Target target )
{
ComponentHelper componentHelper = ComponentHelper.getComponentHelper(
project
);
componentHelper.initDefaultDefinitions();
}
}

View File

@ -0,0 +1,37 @@
<plexus>
<components>
<component>
<role>org.codehaus.plexus.component.configurator.ComponentConfigurator</role>
<role-hint>override</role-hint>
<implementation>org.codehaus.plexus.component.configurator.BasicComponentConfigurator</implementation>
<requirements>
<requirement>
<role>org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup</role>
<role-hint>override</role-hint>
</requirement>
</requirements>
</component>
<component>
<role>org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup</role>
<role-hint>override</role-hint>
<implementation>org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup</implementation>
<requirements>
<requirement>
<role>org.codehaus.plexus.component.configurator.converters.ConfigurationConverter</role>
<role-hint>AntTarget</role-hint>
<field-name>customConverters</field-name>
</requirement>
</requirements>
</component>
<component>
<role>org.codehaus.plexus.component.configurator.converters.ConfigurationConverter</role>
<role-hint>AntTarget</role-hint>
<implementation>org.apache.maven.plugin.antrun.components.AntTargetConverter</implementation>
</component>
</components>
</plexus>

View File

@ -0,0 +1,42 @@
<?xml version="1.0"?>
<!--
| Copyright 2001-2005 The Apache Software Foundation.
|
| Licensed 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 name="Maven AntRun Plugin">
<bannerLeft>
<name>Maven AntRun Plugin</name>
<src>http://maven.apache.org/images/apache-maven-project.png</src>
<href>http://maven.apache.org/maven2/</href>
</bannerLeft>
<bannerRight>
<src>http://maven.apache.org/images/maven-small.gif</src>
</bannerRight>
<body>
<links>
<item name="Maven 2" href="http://maven.apache.org/maven2/"/>
<item name="Ant" href="http://ant.apache.org/"/>
</links>
<menu name="Overview">
<item name="Introduction" href="introduction.html"/>
<item name="Usage" href="usage.html"/>
</menu>
${reports}
</body>
</project>

View File

@ -0,0 +1,39 @@
<?xml version="1.0"?>
<document>
<properties>
<author email="kenney@apache.org">Kenney Westerhof</author>
<title>Introduction</title>
</properties>
<head/>
<body>
<section name="Introduction">
<p>
This plugin provides the ability to run Ant tasks from
within Maven2. You can even embed your ant scripts in the POM!
</p>
<p>
It is <emph>not</emph> the intention of this plugin to provide a means
of polluting the POM, so it's encouraged to move all your
Ant tasks to a build.xml file and just call it from the POM
using Ant's <a href="http://ant.apache.org/manual/CoreTasks/ant.html">&lt;ant/&gt; task</a>.
</p>
<p>
One of the main goals of this plugin is to ease the migration from
Ant based projects to Maven 2. A lot of projects are currently unable
to migrate simply because they depend on some custom build functionality
that Maven2 doesn't provide yet.
</p>
<p>
Even with plugins widely available there will always be simple, small
tasks that can be done using one or two lines of Ant script (like
deploying an EAR), until ofcourse a plugin comes available that does
the job.
</p>
</section>
</body>
</document>

View File

@ -0,0 +1,65 @@
<?xml version="1.0"?>
<document>
<properties>
<author email="kenney@apache.org">Kenney Westerhof</author>
<title>Introduction</title>
</properties>
<head/>
<body>
<section name="Usage">
<p>
For those of you unfamiliar with configuring a plugin see the example
below:
<source>
<![CDATA[
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-test-app</artifactId>
<groupId>my-test-group</groupId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<!--
Place any ant task here. You can add anything
you can add between <target> and </target> in a
build.xml.
-->
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
]]>
</source>
</p>
<p>
The example above illustrates how to bind an ant script to a
lifecycle phase. You can add a script to each lifecycle phase,
by duplicating the <![CDATA[<execution/>]]> section and specifying
a new phase.
</p>
</section>
</body>
</document>

View File

@ -52,6 +52,15 @@
<role>Java Developer</role>
</roles>
</developer>
<developer>
<id>kenney</id>
<name>Kenney Westerhof</name>
<email>kenney@apache.org</email>
<organization>Neonics</organization>
<roles>
<role>Java Developer</role>
</roles>
</developer>
</developers>
<dependencies>
<dependency>
@ -108,6 +117,11 @@
</pluginRepositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
@ -122,6 +136,7 @@
</dependencyManagement>
<modules>
<module>maven-ant-plugin</module>
<module>maven-antrun-plugin</module>
<module>maven-assembly-plugin</module>
<module>maven-checkstyle-plugin</module>
<module>maven-clean-plugin</module>