o separating out the MavenEmbedderProjectWithExtensionReadingTest which shows that the artifacts handlers must be loaded up-front

in the readProjectWithDependencies method because it is not being done in the core unless projects are executed. So Milos' assertion
  is correct. I'm now looking at a layered approach for project resolution and then execution so that the readProjectWithDependencies
  (which is essential for IDE integration) will yield something that can be pushed into the lifecycle executor. Right now there
  is much duplication which makes the IDE integration crappy.

  Another result of this is trying to create a simple IDE import model that gives back client code the fully resolved, topo sorted
  set of projects which point to binary dependencies outside the reactor, and to source folders inside the reactor. The result will
  be a useful model for all IDE integration, right now everyone is doing their own thing. This model will need hooks for customization
  to take into account turning "workspace resolution" on/off and allow easy overriding of this process.

o Fixed IT0035



git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@572366 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2007-09-03 15:42:29 +00:00
parent 01f970117a
commit cc1ca10d74
11 changed files with 189 additions and 143 deletions

View File

@ -78,7 +78,7 @@ public class DefaultMavenExecutionResult
return exceptions;
}
public void addException( Throwable t )
public MavenExecutionResult addException( Throwable t )
{
if ( exceptions == null )
{
@ -86,6 +86,8 @@ public class DefaultMavenExecutionResult
}
exceptions.add( t );
return this;
}
public boolean hasExceptions()

View File

@ -48,7 +48,7 @@ public interface MavenExecutionResult
// - xmlpull parser exception
List getExceptions();
void addException( Throwable t );
MavenExecutionResult addException( Throwable t );
boolean hasExceptions();
}

View File

@ -228,5 +228,21 @@ under the License.
</plugins>
</build>
</profile>
<profile>
<!-- Single Test -->
<id>st</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/MavenEmbedderTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -96,6 +96,7 @@ import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -422,32 +423,35 @@ public class MavenEmbedder
{
MavenExecutionResult result = new DefaultMavenExecutionResult();
MavenProject project;
try
{
request = populator.populateDefaults( request, this );
//mkleint: copied from DefaultLifecycleExecutor
project = readProject( new File( request.getPomFile() ) );
// This is necessary to make the MavenEmbedderProjectWithExtensionReadingTest work which uses
// a custom type for a dependency like this:
//
// <dependency>
// <groupId>junit</groupId>
// <artifactId>junit</artifactId>
// <version>3.8.1</version>
// <scope>test</scope>
// <type>mkleint</type>
// </dependency>
//
// If the artifact handlers are not loaded up-front then this dependency element is not
// registered as an artifact and is not added to the classpath elements.
MavenProject project = readProject( new File( request.getPomFile() ) );
Map handlers = findArtifactTypeHandlers( project );
//is this necessary in this context, I doubt it..mkleint
artifactHandlerManager.addHandlers( handlers );
}
catch ( Exception e )
{
// At this point real project building, and artifact resolution have not occured.
result.addException( e );
return result;
return result.addException( e );
}
MavenProjectBuildingResult r = null;
ReactorManager reactorManager = maven.createReactorManager( request, result );
if ( result.hasExceptions() )
@ -455,14 +459,11 @@ public class MavenEmbedder
return result;
}
result.setTopologicallySortedProjects( reactorManager.getSortedProjects() );
// Now I should be able to pass this projects to the next request so that I don't have to process
// any local projects again. And this logic is still too complicated.
MavenProjectBuildingResult projectBuildingResult = null;
try
{
r = mavenProjectBuilder.buildWithDependencies(
projectBuildingResult = mavenProjectBuilder.buildWithDependencies(
new File( request.getPomFile() ),
request.getLocalRepository(),
profileManager,
@ -473,10 +474,28 @@ public class MavenEmbedder
result.addException( e );
}
result.setProject( r.getProject() );
if ( reactorManager.hasMultipleProjects() )
{
result.setProject( projectBuildingResult.getProject() );
result.setArtifactResolutionResult( r.getArtifactResolutionResult() );
result.setTopologicallySortedProjects( reactorManager.getSortedProjects() );
}
else
{
result.setProject( projectBuildingResult.getProject() );
result.setTopologicallySortedProjects( Arrays.asList( new MavenProject[]{ projectBuildingResult.getProject()} ) );
}
result.setArtifactResolutionResult( projectBuildingResult.getArtifactResolutionResult() );
// From this I could produce something that would help IDE integrators create importers:
// - topo sorted list of projects
// - binary dependencies
// - source dependencies (projects in the reactor)
//
// We could create a layer approach here. As to do anything you must resolve a projects artifacts,
// and with that set you could then subsequently execute goals for each of those project.
return result;
}

View File

@ -37,18 +37,7 @@ import org.apache.maven.wagon.resource.Resource;
public abstract class AbstractConsoleDownloadMonitorTest
extends TestCase
{
private AbstractConsoleDownloadMonitor monitor;
public AbstractConsoleDownloadMonitorTest()
{
super();
}
public void setMonitor( AbstractConsoleDownloadMonitor monitor )
{
this.monitor = monitor;
}
protected AbstractConsoleDownloadMonitor monitor;
public AbstractConsoleDownloadMonitor getMonitor()
{

View File

@ -31,8 +31,6 @@ public class BatchModeDownloadMonitorTest
protected void setUp()
throws Exception
{
super.setMonitor( new BatchModeDownloadMonitor() );
super.setUp();
monitor = new BatchModeDownloadMonitor();
}
}

View File

@ -21,19 +21,16 @@ package org.apache.maven.cli;
/**
* Test for {@link ConsoleDownloadMonitor}
*
*
* @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
* @version $Id$
*/
public class ConsoleDownloadMonitorTest
extends AbstractConsoleDownloadMonitorTest
{
protected void setUp()
throws Exception
{
super.setMonitor( new ConsoleDownloadMonitor() );
super.setUp();
monitor = new ConsoleDownloadMonitor();
}
}

View File

@ -0,0 +1,103 @@
package org.apache.maven.embedder;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginManagerException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.artifact.handler.ArtifactHandler;
import java.io.File;
import java.util.Map;
import java.util.HashMap;
/** @author Jason van Zyl */
public class MavenEmbedderProjectWithExtensionReadingTest
extends MavenEmbedderTest
{
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 );
assertNoExceptions( result );
// sources, test sources, and the junit jar..
assertEquals( 3, result.getProject().getTestClasspathElements().size() );
}
private class ExtendableMavenEmbedder
extends MavenEmbedder
{
public ExtendableMavenEmbedder( ClassLoader classLoader )
throws MavenEmbedderException
{
super( new DefaultConfiguration()
.setClassLoader( classLoader )
.setMavenEmbedderLogger( 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

@ -29,7 +29,6 @@ import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginManagerException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
@ -46,18 +45,16 @@ import java.io.FileWriter;
import java.io.IOException;
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;
public class MavenEmbedderTest
extends TestCase
{
private String basedir;
protected String basedir;
private MavenEmbedder maven;
protected MavenEmbedder maven;
protected void setUp()
throws Exception
@ -86,7 +83,7 @@ public class MavenEmbedderTest
maven.stop();
}
private void assertNoExceptions( MavenExecutionResult result )
protected void assertNoExceptions( MavenExecutionResult result )
{
List exceptions = result.getExceptions();
if ( ( exceptions == null ) || exceptions.isEmpty() )
@ -330,22 +327,6 @@ public class MavenEmbedderTest
artifacts.iterator().next();
}
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 );
assertNoExceptions( result );
// sources, test sources, and the junit jar..
assertEquals( 3, result.getProject().getTestClasspathElements().size() );
}
/*
public void testProjectReadingWithDistributionStatus()
throws Exception
@ -533,72 +514,4 @@ public class MavenEmbedderTest
{
return new File( basedir, "src/test/resources/pom.xml" );
}
private class ExtendableMavenEmbedder
extends MavenEmbedder
{
public ExtendableMavenEmbedder( ClassLoader classLoader )
throws MavenEmbedderException
{
super( new DefaultConfiguration()
.setClassLoader( classLoader )
.setMavenEmbedderLogger( 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

@ -31,8 +31,8 @@ import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* We want to make sure when projects are newly created and have dependencies between them that
@ -60,22 +60,31 @@ public class MavenEmbedderProjectNotInRepositoryTest
MavenEmbedder embedder = new MavenEmbedder( configuration );
File projectDirectory = new File( getBasedir(), "src/test/projects/no-artifact-in-repository-test" );
File pom = new File( getBasedir(), "src/test/projects/no-artifact-in-repository-test" );
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
.setBaseDirectory( projectDirectory );
.setBaseDirectory( pom );
MavenExecutionResult result = embedder.readProjectWithDependencies( request );
List projects = result.getTopologicallySortedProjects();
/*
for ( Iterator i = projects.iterator(); i.hasNext(); )
{
MavenProject project = (MavenProject) i.next();
MavenProject project;
System.out.println( "project = " + project );
}
*/
project = (MavenProject) projects.get( 0 );
assertEquals( "child-1", project.getArtifactId() );
project = (MavenProject) projects.get( 1 );
assertEquals( "child-2", project.getArtifactId() );
List deps = project.getDependencies();
assertEquals( 2, deps.size() );
project = (MavenProject) projects.get( 2 );
assertEquals( "parent", project.getArtifactId() );
}
}

View File

@ -2,7 +2,7 @@
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>test</groupId>
<artifactId>no-artifact-in-repository-test</artifactId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>no-artifact-in-repository-test</name>