You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2009/02/12 02:32:51 UTC
svn commit: r743587 - in /maven/components/branches/maven-2.1.x:
maven-core/src/main/java/org/apache/maven/plugin/
maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/
maven-script/maven-script-ant/ maven-script/maven-script-ant/sr...
Author: jdcasey
Date: Thu Feb 12 01:32:50 2009
New Revision: 743587
URL: http://svn.apache.org/viewvc?rev=743587&view=rev
Log:
[MNG-3971] Updating Ant-mojo support to provide the standard Maven classpath refs and plugin-style expression evaluation available in the antrun plugin. This depends on the newest (as-yet unreleased) maven-plugin-tools-ant to enable the new functionality, though it is still backward compatible. Ant-based plugins build using the plugin-tools-ant versions that are older will work, but won't have access to maven.compile.classpath, etc.
Added:
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java (with props)
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java (with props)
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml (with props)
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml (with props)
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/test.build.xml (with props)
Modified:
maven/components/branches/maven-2.1.x/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java
maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java
maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/pom.xml
maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java
Modified: maven/components/branches/maven-2.1.x/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java?rev=743587&r1=743586&r2=743587&view=diff
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java (original)
+++ maven/components/branches/maven-2.1.x/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java Thu Feb 12 01:32:50 2009
@@ -80,14 +80,8 @@
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
-import org.codehaus.plexus.util.xml.Xpp3DomWriter;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
-import java.io.IOException;
-import java.io.StringReader;
-import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
@@ -338,6 +332,7 @@
// later when the plugin is first invoked. Retrieving this artifact will in turn allow us to
// transitively resolve its dependencies, and add them to the plugin container...
addedPlugin.setArtifacts( Collections.singletonList( pluginArtifact ) );
+ addedPlugin.setPluginArtifact( pluginArtifact );
try
{
Modified: maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java?rev=743587&r1=743586&r2=743587&view=diff
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java (original)
+++ maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java Thu Feb 12 01:32:50 2009
@@ -203,6 +203,8 @@
}
parameters.add( parameter );
+
+ parameterMap = null;
}
/**
Modified: maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java?rev=743587&r1=743586&r2=743587&view=diff
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java (original)
+++ maven/components/branches/maven-2.1.x/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java Thu Feb 12 01:32:50 2009
@@ -19,6 +19,7 @@
* under the License.
*/
+import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.plugin.lifecycle.Lifecycle;
import org.apache.maven.plugin.lifecycle.LifecycleConfiguration;
@@ -57,6 +58,8 @@
private String source;
private boolean inheritedByDefault = true;
+
+ private Artifact pluginArtifact;
private List artifacts;
@@ -365,4 +368,14 @@
{
return description;
}
+
+ public Artifact getPluginArtifact()
+ {
+ return pluginArtifact;
+ }
+
+ public void setPluginArtifact( Artifact pluginArtifact )
+ {
+ this.pluginArtifact = pluginArtifact;
+ }
}
Modified: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/pom.xml
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/pom.xml?rev=743587&r1=743586&r2=743587&view=diff
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/pom.xml (original)
+++ maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/pom.xml Thu Feb 12 01:32:50 2009
@@ -46,7 +46,7 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-ant-factory</artifactId>
- <version>1.0-alpha-2</version>
+ <version>1.0-alpha-2.1-SNAPSHOT</version>
<!-- We need to exclude this artifact's dependency on Ant, -->
<!-- because it has a different groupId that the version of Ant -->
<!-- we want to use now. -->
@@ -65,5 +65,27 @@
<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>
Modified: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java?rev=743587&r1=743586&r2=743587&view=diff
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java (original)
+++ maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntMojoWrapper.java Thu Feb 12 01:32:50 2009
@@ -19,26 +19,65 @@
* 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
+ 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;
@@ -47,6 +86,63 @@
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();
@@ -55,6 +151,8 @@
{
throw new MojoExecutionException( "Failed to execute: " + e.getMessage(), e );
}
+
+ unconstructedParts.clear();
}
public void setPluginContext( Map pluginContext )
@@ -77,6 +175,204 @@
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;
+ }
}
Added: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java?rev=743587&view=auto
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java (added)
+++ maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java Thu Feb 12 01:32:50 2009
@@ -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;
+ }
+}
Propchange: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/main/java/org/apache/maven/script/ant/AntPropertyHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java?rev=743587&view=auto
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java (added)
+++ maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java Thu Feb 12 01:32:50 2009
@@ -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 )
+ {
+ }
+ };
+
+}
Propchange: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/java/org/apache/maven/script/ant/AntMojoWrapperTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml?rev=743587&view=auto
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml (added)
+++ maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml Thu Feb 12 01:32:50 2009
@@ -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>
\ No newline at end of file
Propchange: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.0.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml?rev=743587&view=auto
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml (added)
+++ maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml Thu Feb 12 01:32:50 2009
@@ -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>
\ No newline at end of file
Propchange: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/META-INF/maven/plugin-2.1.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/test.build.xml
URL: http://svn.apache.org/viewvc/maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/test.build.xml?rev=743587&view=auto
==============================================================================
--- maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/test.build.xml (added)
+++ maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/test.build.xml Thu Feb 12 01:32:50 2009
@@ -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>
\ No newline at end of file
Propchange: maven/components/branches/maven-2.1.x/maven-script/maven-script-ant/src/test/resources/test.build.xml
------------------------------------------------------------------------------
svn:eol-style = native