From 345e8c3732b1802207ceef3c3263f1876de27cb2 Mon Sep 17 00:00:00 2001 From: Milos Kleint Date: Tue, 23 Jan 2007 10:20:10 +0000 Subject: [PATCH] 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 --- .../apache/maven/embedder/MavenEmbedder.java | 108 +++++++++++++++++- .../maven/embedder/MavenEmbedderTest.java | 80 ++++++++++++- maven-embedder/src/test/resources/pom2.xml | 29 +++++ 3 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 maven-embedder/src/test/resources/pom2.xml diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java index 91d6e7261f..2384b9b968 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java @@ -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.ArtifactResolutionException; 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.writer.WriterUtils; 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.io.jdom.MavenJDOMWriter; 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.version.PluginVersionNotFoundException; +import org.apache.maven.plugin.version.PluginVersionResolutionException; import org.apache.maven.profiles.DefaultProfileManager; import org.apache.maven.profiles.ProfileManager; 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.logging.LoggerManager; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -69,8 +72,14 @@ import java.io.Writer; 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 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 @@ -238,6 +247,80 @@ public class MavenEmbedder { 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 @@ -251,10 +334,26 @@ public class MavenEmbedder try { 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() ), request.getLocalRepository(), profileManager, 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 ) { @@ -272,6 +371,11 @@ public class MavenEmbedder { 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 ); } diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java index 01a3636359..85c36b5659 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java @@ -7,15 +7,20 @@ import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.model.Model; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.FileUtils; - import java.io.File; import java.io.FileWriter; import java.io.Writer; import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; - 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 extends TestCase @@ -202,6 +207,28 @@ public class MavenEmbedderTest 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 @@ -235,4 +262,53 @@ public class MavenEmbedderTest { 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; + } + } } diff --git a/maven-embedder/src/test/resources/pom2.xml b/maven-embedder/src/test/resources/pom2.xml new file mode 100644 index 0000000000..d729acee1d --- /dev/null +++ b/maven-embedder/src/test/resources/pom2.xml @@ -0,0 +1,29 @@ + + 4.0.0 + org.apache.maven + embedder-test-project2 + jar + 1.0-SNAPSHOT + Maven Quick Start Archetype + http://maven.apache.org + + + junit + junit + 3.8.1 + test + mkleint + + + + + + + maven-surefire-plugin + RELEASE + true + + + +