Implemented embedder extensions. You can register a set of URLs that

are scanned for components, after the container is started, that
will override any core components defined.

o Updated (Default)MavenEmbedRequest with a getter/setter for the extension list;

o Updated the MavenEmbedder to scan the extensions.

o Added testcase: dummy component implementation and components.xml to specify
  the override. 
  
o Also added pom.properties since running unit tests in an IDE
  will fail because the pom.properties is generated only inside the archive,
  and with project references inside the IDE that resource is not available.



git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@496904 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kenney Westerhof 2007-01-17 00:46:49 +00:00
parent 6b39882779
commit b61fba1d5f
7 changed files with 153 additions and 5 deletions

View File

@ -45,6 +45,11 @@ public class DefaultMavenEmbedRequest
private Properties systemProperties;
/**
* List<URL>.
*/
private List extensions = new ArrayList();
/**
* Creates a new instance of DefaultMavenEmbedRequest
*/
@ -138,4 +143,13 @@ public class DefaultMavenEmbedRequest
return systemProperties != null ? systemProperties : System.getProperties();
}
public void addExtension( URL url )
{
extensions.add( url );
}
public List getExtensions()
{
return extensions;
}
}

View File

@ -79,4 +79,8 @@ public interface MavenEmbedRequest
ContainerCustomizer getContainerCustomizer();
Properties getSystemProperties();
void addExtension( URL url );
List getExtensions();
}

View File

@ -20,9 +20,9 @@ import org.apache.maven.Maven;
import org.apache.maven.MavenTools;
import org.apache.maven.SettingsConfigurationException;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
@ -37,8 +37,8 @@ import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.jdom.MavenJDOMWriter;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.ProfileManager;
@ -47,13 +47,16 @@ import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.MutablePlexusContainer;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import org.codehaus.plexus.component.repository.ComponentDescriptor;
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@ -62,8 +65,10 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
@ -73,7 +78,7 @@ import java.util.List;
*/
public class MavenEmbedder
{
private PlexusContainer container;
private MutablePlexusContainer container;
// ----------------------------------------------------------------------
// Components
@ -420,7 +425,7 @@ public class MavenEmbedder
{
throw new MavenEmbedderException( "Error starting Maven embedder.", e );
}
if ( logger != null )
{
MavenEmbedderLoggerManager loggerManager =
@ -436,6 +441,8 @@ public class MavenEmbedder
req.getContainerCustomizer().customize( container );
}
handleExtensions( req.getExtensions() );
// ----------------------------------------------------------------------
// Lookup each of the components we need to provide the desired
// client interface.
@ -498,6 +505,45 @@ public class MavenEmbedder
// Lifecycle
// ----------------------------------------------------------------------
private void handleExtensions( List extensions )
throws MavenEmbedderException
{
ClassRealm childRealm;
try
{
childRealm = container.getContainerRealm().createChildRealm( "embedder-extensions" );
}
catch ( DuplicateRealmException e1 )
{
try
{
childRealm = classWorld.getRealm( "embedder-extensions" );
}
catch ( NoSuchRealmException e )
{
throw new MavenEmbedderException( "Cannot create realm 'extensions'", e );
}
}
for ( Iterator it = extensions.iterator(); it.hasNext(); )
{
childRealm.addURL( (URL) it.next() );
}
try
{
container.discoverComponents( childRealm, true );
}
catch ( PlexusConfigurationException e )
{
throw new MavenEmbedderException( "Configuration error while discovering extension components", e );
}
catch ( ComponentRepositoryException e )
{
throw new MavenEmbedderException( "Component repository error while discovering extension components", e );
}
}
public void stop()
throws MavenEmbedderException
{

View File

@ -0,0 +1,8 @@
<component-set>
<components>
<component>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
<implementation>org.apache.maven.embedder.CustomArtifactFactory</implementation>
</component>
</components>
</component-set>

View File

@ -0,0 +1,14 @@
package org.apache.maven.embedder;
import org.apache.maven.artifact.factory.DefaultArtifactFactory;
/**
*
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
*
*/
public class CustomArtifactFactory
extends DefaultArtifactFactory
{
}

View File

@ -0,0 +1,61 @@
package org.apache.maven.embedder;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import java.io.File;
import junit.framework.TestCase;
/**
*
* @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
*
*/
public class TestComponentOverride
extends TestCase
{
private String basedir;
private MavenEmbedder maven;
protected PlexusContainer container;
protected void setUp()
throws Exception
{
basedir = System.getProperty( "basedir" );
maven = new MavenEmbedder( Thread.currentThread().getContextClassLoader(), new MavenEmbedderConsoleLogger() );
MavenEmbedRequest request = new DefaultMavenEmbedRequest();
request.addExtension( new File( basedir, "src/test/extensions" ).toURI().toURL() );
// register callback to get a hold of the container
request.setConfigurationCustomizer( new ContainerCustomizer()
{
public void customize( PlexusContainer container )
{
TestComponentOverride.this.container = container;
}
} );
maven.start( request );
}
public void testComponentOverride()
throws ComponentLookupException
{
ArtifactFactory factory = (ArtifactFactory) container.lookup( ArtifactFactory.class );
assertNotNull( factory );
assertTrue( "Expecting " + CustomArtifactFactory.class.getName() + " but was " + factory.getClass().getName(),
CustomArtifactFactory.class.isAssignableFrom( factory.getClass() ) );
// test wheter the requirement is injected - if not, it nullpointers
factory.createArtifact( "testGroupId", "testArtifactId", "testVersion", "compile", "jar" );
}
}

View File

@ -0,0 +1 @@
version=2.1-SNAPSHOT