MEVENIDE-484 - embedder's project loading didn't take the artifact handlers in plugin extensions into account when loading project dependencies.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@498975 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Milos Kleint 2007-01-23 10:20:10 +00:00
parent 43112103d6
commit 345e8c3732
3 changed files with 213 additions and 4 deletions

View File

@ -30,6 +30,7 @@ import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.embedder.execution.MavenExecutionRequestDefaultsPopulator; import org.apache.maven.embedder.execution.MavenExecutionRequestDefaultsPopulator;
import org.apache.maven.embedder.writer.WriterUtils; import org.apache.maven.embedder.writer.WriterUtils;
import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.DefaultMavenExecutionResult;
@ -39,7 +40,10 @@ import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.model.io.jdom.MavenJDOMWriter; import org.apache.maven.model.io.jdom.MavenJDOMWriter;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.InvalidPluginException;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder; import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
import org.apache.maven.plugin.version.PluginVersionNotFoundException;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.profiles.DefaultProfileManager; import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.ProfileManager; import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
@ -61,7 +65,6 @@ import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.logging.LoggerManager; import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
@ -69,8 +72,14 @@ import java.io.Writer;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginManager;
import org.apache.maven.plugin.PluginManagerException;
import org.apache.maven.plugin.PluginNotFoundException;
/** /**
* Class intended to be used by clients who wish to embed Maven into their applications * Class intended to be used by clients who wish to embed Maven into their applications
@ -239,6 +248,80 @@ public class MavenEmbedder
return mavenProjectBuilder.build( mavenProject, localRepository, profileManager ); return mavenProjectBuilder.build( mavenProject, localRepository, profileManager );
} }
/**
* mkleint: protected so that IDE integrations can selectively allow downloading artifacts
* from remote repositories (if they prohibit by default on project loading)
*/
protected void verifyPlugin( Plugin plugin, MavenProject project )
throws ComponentLookupException,
ArtifactResolutionException,
PluginVersionResolutionException,
ArtifactNotFoundException,
InvalidVersionSpecificationException,
InvalidPluginException,
PluginManagerException,
PluginNotFoundException,
PluginVersionNotFoundException
{
PluginManager pluginManager = (PluginManager) container.lookup( PluginManager.ROLE );
pluginManager.verifyPlugin(plugin, project, settings, localRepository);
}
/**
* protected for tests only..
*/
protected Map getPluginExtensionComponents(Plugin plugin) throws PluginManagerException
{
try
{
PluginManager pluginManager = (PluginManager) container.lookup( PluginManager.ROLE );
return pluginManager.getPluginComponents( plugin, ArtifactHandler.ROLE );
}
catch ( ComponentLookupException e )
{
getLogger().debug( "Unable to find the lifecycle component in the extension", e );
return new HashMap();
}
}
/**
* mkleint: copied from DefaultLifecycleExecutor
*
* @todo Not particularly happy about this. Would like WagonManager and ArtifactTypeHandlerManager to be able to
* lookup directly, or have them passed in
*
* @todo Move this sort of thing to the tail end of the project-building process
*/
private Map findArtifactTypeHandlers( MavenProject project )
throws Exception
{
Map map = new HashMap();
for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
{
Plugin plugin = (Plugin) i.next();
if ( plugin.isExtensions() )
{
verifyPlugin( plugin, project);
map.putAll( getPluginExtensionComponents(plugin));
// shudder...
for ( Iterator j = map.values().iterator(); j.hasNext(); )
{
ArtifactHandler handler = (ArtifactHandler) j.next();
if ( project.getPackaging().equals( handler.getPackaging() ) )
{
project.getArtifact().setArtifactHandler( handler );
}
}
}
}
return map;
}
/** /**
* This method is used to grab the list of dependencies that belong to a project so that a UI * This method is used to grab the list of dependencies that belong to a project so that a UI
* can be populated. For example, a list of libraries that are used by an Eclipse, Netbeans, or * can be populated. For example, a list of libraries that are used by an Eclipse, Netbeans, or
@ -252,9 +335,25 @@ public class MavenEmbedder
{ {
request = defaultsPopulator.populateDefaults( request ); request = defaultsPopulator.populateDefaults( request );
project = readProject( new File (request.getPomFile()) );
//mkleint: copied from DefaultLifecycleExecutor
Map handlers = findArtifactTypeHandlers( project );
//is this necessary in this context, I doubt it..mkleint
artifactHandlerManager.addHandlers( handlers );
project = mavenProjectBuilder.buildWithDependencies( new File( request.getPomFile() ), project = mavenProjectBuilder.buildWithDependencies( new File( request.getPomFile() ),
request.getLocalRepository(), profileManager, request.getLocalRepository(), profileManager,
request.getTransferListener() ); request.getTransferListener() );
}
catch (PluginManagerException e)
{
return new DefaultMavenExecutionResult( project, Collections.singletonList( e ) );
}
catch ( PluginNotFoundException e )
{
return new DefaultMavenExecutionResult( project, Collections.singletonList( e ) );
} }
catch ( MavenEmbedderException e ) catch ( MavenEmbedderException e )
{ {
@ -272,6 +371,11 @@ public class MavenEmbedder
{ {
return new DefaultMavenExecutionResult( project, Collections.singletonList( e ) ); return new DefaultMavenExecutionResult( project, Collections.singletonList( e ) );
} }
//mkleint: why do we have so many various exception handlings with same result?
catch (Exception e)
{
return new DefaultMavenExecutionResult( project, Collections.singletonList( e ) );
}
return new DefaultMavenExecutionResult( project, Collections.EMPTY_LIST ); return new DefaultMavenExecutionResult( project, Collections.EMPTY_LIST );
} }

View File

@ -7,15 +7,20 @@ import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.Writer; import java.io.Writer;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginManagerException;
public class MavenEmbedderTest public class MavenEmbedderTest
extends TestCase extends TestCase
@ -203,6 +208,28 @@ public class MavenEmbedderTest
System.out.println( "artifact = " + artifact ); System.out.println( "artifact = " + artifact );
} }
public void testProjectWithExtensionsReading()
throws Exception
{
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
.setShowErrors( true )
.setPomFile(new File( basedir, "src/test/resources/pom2.xml" ).getAbsolutePath());
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MavenExecutionResult result = new ExtendableMavenEmbedder(classLoader).readProjectWithDependencies( request );
// Iterator it = result.getMavenProject().getTestClasspathElements().iterator();
// while(it.hasNext()) {
// Object object = (Object) it.next();
// System.out.println(" element=" + object);
// }
// sources, test sources, and the junit jar..
assertEquals( 3, result.getMavenProject().getTestClasspathElements().size());
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Model Writing // Model Writing
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -235,4 +262,53 @@ public class MavenEmbedderTest
{ {
return new File( basedir, "src/test/resources/pom.xml" ); return new File( basedir, "src/test/resources/pom.xml" );
} }
private class ExtendableMavenEmbedder extends MavenEmbedder {
public ExtendableMavenEmbedder(ClassLoader classLoader) throws MavenEmbedderException {
super( classLoader, new MavenEmbedderConsoleLogger());
}
protected Map getPluginExtensionComponents(Plugin plugin) throws PluginManagerException {
Map toReturn = new HashMap();
MyArtifactHandler handler = new MyArtifactHandler();
toReturn.put("mkleint", handler);
return toReturn;
}
protected void verifyPlugin( Plugin plugin, MavenProject project ) {
//ignore don't want to actually verify in test
}
}
private class MyArtifactHandler implements ArtifactHandler {
public String getExtension() {
return "jar";
}
public String getDirectory() {
throw new UnsupportedOperationException("Not supported yet.");
}
public String getClassifier() {
return null;
}
public String getPackaging() {
return "mkleint";
}
public boolean isIncludesDependencies() {
return false;
}
public String getLanguage() {
return "java";
}
public boolean isAddedToClasspath() {
return true;
}
}
} }

View File

@ -0,0 +1,29 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven</groupId>
<artifactId>embedder-test-project2</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
<type>mkleint</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- it's here just for the extensions tag -->
<artifactId>maven-surefire-plugin</artifactId>
<version>RELEASE</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>