mirror of https://github.com/apache/maven.git
MNG-1323 making extensions work inside a reactor
Submitted by: Piotr Tabor git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@575987 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7ddb9a1400
commit
2017c78e0a
|
@ -105,6 +105,8 @@ public class DefaultPluginManager
|
||||||
|
|
||||||
RESERVED_GROUP_IDS = rgids;
|
RESERVED_GROUP_IDS = rgids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected PluginRealmManager pluginRealmManager;
|
||||||
|
|
||||||
protected PlexusContainer container;
|
protected PlexusContainer container;
|
||||||
|
|
||||||
|
@ -377,67 +379,70 @@ public class DefaultPluginManager
|
||||||
{
|
{
|
||||||
// TODO When/if we go to project-level plugin instances (like for plugin-level deps in the
|
// TODO When/if we go to project-level plugin instances (like for plugin-level deps in the
|
||||||
// POM), we need to undo this somehow.
|
// POM), we need to undo this somehow.
|
||||||
ClassRealm pluginRealm = container.getComponentRealm( projectPlugin.getKey() );
|
|
||||||
|
|
||||||
if ( ( pluginRealm != null ) && ( pluginRealm != container.getContainerRealm() ) )
|
ClassRealm componentRealm = pluginRealmManager.getOrCreateRealm(projectPlugin, pluginArtifact, artifacts);
|
||||||
{
|
|
||||||
getLogger().debug(
|
// ClassRealm pluginRealm = container.getComponentRealm( projectPlugin.getKey()+":"+projectPlugin.getVersion() );
|
||||||
"Realm already exists for: " + projectPlugin.getKey()
|
|
||||||
+ ". Skipping addition..." );
|
|
||||||
// we've already discovered this plugin, and configured it, so skip it this time.
|
|
||||||
|
|
||||||
return;
|
// if ( ( pluginRealm != null ) && ( pluginRealm != container.getContainerRealm() ) )
|
||||||
}
|
// {
|
||||||
|
// getLogger().debug(
|
||||||
// ----------------------------------------------------------------------------
|
// "Realm already exists for: " + projectPlugin.getKey()
|
||||||
// Realm creation for a plugin
|
// + ". Skipping addition..." );
|
||||||
// ----------------------------------------------------------------------------
|
// // we've already discovered this plugin, and configured it, so skip it this time.
|
||||||
|
//
|
||||||
ClassRealm componentRealm = null;
|
// return;
|
||||||
|
// }
|
||||||
try
|
//
|
||||||
{
|
// // ----------------------------------------------------------------------------
|
||||||
List jars = new ArrayList();
|
// // Realm creation for a plugin
|
||||||
|
// // ----------------------------------------------------------------------------
|
||||||
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
|
//
|
||||||
{
|
// ClassRealm componentRealm = null;
|
||||||
Artifact artifact = (Artifact) i.next();
|
//
|
||||||
|
// try
|
||||||
jars.add( artifact.getFile() );
|
// {
|
||||||
}
|
// List jars = new ArrayList();
|
||||||
|
//
|
||||||
jars.add( pluginArtifact.getFile() );
|
// for ( Iterator i = artifacts.iterator(); i.hasNext(); )
|
||||||
|
// {
|
||||||
// Now here we need the artifact coreArtifactFilter stuff
|
// Artifact artifact = (Artifact) i.next();
|
||||||
|
//
|
||||||
componentRealm = container.createComponentRealm( projectPlugin.getKey(), jars );
|
// jars.add( artifact.getFile() );
|
||||||
|
// }
|
||||||
// adding for MNG-3012 to try to work around problems with Xpp3Dom (from plexus-utils)
|
//
|
||||||
// spawning a ClassCastException when a mojo calls plugin.getConfiguration() from maven-model...
|
// jars.add( pluginArtifact.getFile() );
|
||||||
componentRealm.importFrom( componentRealm.getParentRealm().getId(),
|
//
|
||||||
Xpp3Dom.class.getName() );
|
// // Now here we need the artifact coreArtifactFilter stuff
|
||||||
componentRealm.importFrom( componentRealm.getParentRealm().getId(),
|
//
|
||||||
"org.codehaus.plexus.util.xml.pull" );
|
// componentRealm = container.createComponentRealm( projectPlugin.getKey()+projectPlugin.getVersion(), jars );
|
||||||
|
//
|
||||||
// Adding for MNG-2878, since maven-reporting-impl was removed from the
|
// // adding for MNG-3012 to try to work around problems with Xpp3Dom (from plexus-utils)
|
||||||
// internal list of artifacts managed by maven, the classloader is different
|
// // spawning a ClassCastException when a mojo calls plugin.getConfiguration() from maven-model...
|
||||||
// between maven-reporting-impl and maven-reporting-api...so this resource
|
// componentRealm.importFrom( componentRealm.getParentRealm().getId(),
|
||||||
// is not available from the AbstractMavenReport since it uses:
|
// Xpp3Dom.class.getName() );
|
||||||
// getClass().getResourceAsStream( "/default-report.xml" )
|
// componentRealm.importFrom( componentRealm.getParentRealm().getId(),
|
||||||
// (maven-reporting-impl version 2.0; line 134; affects: checkstyle plugin, and probably others)
|
// "org.codehaus.plexus.util.xml.pull" );
|
||||||
componentRealm.importFrom( componentRealm.getParentRealm().getId(), "/default-report.xml" );
|
//
|
||||||
}
|
// // Adding for MNG-2878, since maven-reporting-impl was removed from the
|
||||||
catch ( PlexusContainerException e )
|
// // internal list of artifacts managed by maven, the classloader is different
|
||||||
{
|
// // between maven-reporting-impl and maven-reporting-api...so this resource
|
||||||
throw new PluginManagerException( "Failed to create realm for plugin '" + projectPlugin
|
// // is not available from the AbstractMavenReport since it uses:
|
||||||
+ ".", e );
|
// // getClass().getResourceAsStream( "/default-report.xml" )
|
||||||
}
|
// // (maven-reporting-impl version 2.0; line 134; affects: checkstyle plugin, and probably others)
|
||||||
catch ( NoSuchRealmException e )
|
// componentRealm.importFrom( componentRealm.getParentRealm().getId(), "/default-report.xml" );
|
||||||
{
|
// }
|
||||||
throw new PluginManagerException(
|
// catch ( PlexusContainerException e )
|
||||||
"Failed to import Xpp3Dom from parent realm for plugin: '"
|
// {
|
||||||
+ projectPlugin + ".", e );
|
// throw new PluginManagerException( "Failed to create realm for plugin '" + projectPlugin
|
||||||
}
|
// + ".", e );
|
||||||
|
// }
|
||||||
|
// catch ( NoSuchRealmException e )
|
||||||
|
// {
|
||||||
|
// throw new PluginManagerException(
|
||||||
|
// "Failed to import Xpp3Dom from parent realm for plugin: '"
|
||||||
|
// + projectPlugin + ".", e );
|
||||||
|
// }
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// The PluginCollector will now know about the plugin we are trying to load
|
// The PluginCollector will now know about the plugin we are trying to load
|
||||||
|
|
|
@ -0,0 +1,153 @@
|
||||||
|
package org.apache.maven.plugin;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.maven.artifact.Artifact;
|
||||||
|
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||||
|
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
|
||||||
|
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
|
||||||
|
import org.apache.maven.model.Plugin;
|
||||||
|
import org.apache.maven.project.MavenProject;
|
||||||
|
import org.codehaus.plexus.PlexusContainer;
|
||||||
|
import org.codehaus.plexus.PlexusContainerException;
|
||||||
|
import org.codehaus.plexus.classworlds.realm.ClassRealm;
|
||||||
|
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
|
||||||
|
import org.codehaus.plexus.logging.AbstractLogEnabled;
|
||||||
|
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:piotr@tabor.waw.pl">Piotr Tabor</a>
|
||||||
|
*/
|
||||||
|
public class DefaultPluginRealmManager
|
||||||
|
extends AbstractLogEnabled
|
||||||
|
implements PluginRealmManager
|
||||||
|
{
|
||||||
|
private PlexusContainer container;
|
||||||
|
|
||||||
|
public ClassRealm getOrCreateRealm( Plugin projectPlugin, Artifact pluginArtifact, Set artifacts )
|
||||||
|
throws PluginManagerException
|
||||||
|
{
|
||||||
|
Set allArtifacts = new HashSet( artifacts );
|
||||||
|
allArtifacts.add( pluginArtifact );
|
||||||
|
|
||||||
|
List/* <URL> */pluginJars = generateJarsListForArtifacts( allArtifacts );
|
||||||
|
|
||||||
|
String realmKey = generateChildContainerName( projectPlugin, allArtifacts );
|
||||||
|
ClassRealm pluginRealm = container.getComponentRealm( realmKey );
|
||||||
|
|
||||||
|
if ( ( pluginRealm != null ) && ( pluginRealm != container.getContainerRealm() ) )
|
||||||
|
{
|
||||||
|
getLogger().debug( "Realm already exists for: " + realmKey + ". Skipping addition..." );
|
||||||
|
/*
|
||||||
|
* we've already discovered this plugin, and configured it, so skip it this time.
|
||||||
|
*/
|
||||||
|
return pluginRealm;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Realm creation for a plugin
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ClassRealm componentRealm = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Now here we need the artifact coreArtifactFilter
|
||||||
|
// stuff
|
||||||
|
|
||||||
|
componentRealm = container.createComponentRealm( realmKey, pluginJars );
|
||||||
|
|
||||||
|
/*
|
||||||
|
* adding for MNG-3012 to try to work around problems with Xpp3Dom (from plexus-utils spawning a
|
||||||
|
* ClassCastException when a mojo calls plugin.getConfiguration() from maven-model...
|
||||||
|
*/
|
||||||
|
componentRealm.importFrom( componentRealm.getParentRealm().getId(), Xpp3Dom.class.getName() );
|
||||||
|
componentRealm.importFrom( componentRealm.getParentRealm().getId(), "org.codehaus.plexus.util.xml.pull" );
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Adding for MNG-2878, since maven-reporting-impl was removed from the internal list of artifacts managed
|
||||||
|
* by maven, the classloader is different between maven-reporting-impl and maven-reporting-api...so this
|
||||||
|
* resource is not available from the AbstractMavenReport since it uses: getClass().getResourceAsStream(
|
||||||
|
* "/default-report.xml" ) (maven-reporting-impl version 2.0; line 134; affects: checkstyle plugin, and
|
||||||
|
* probably others)
|
||||||
|
*/
|
||||||
|
componentRealm.importFrom( componentRealm.getParentRealm().getId(), "/default-report.xml" );
|
||||||
|
|
||||||
|
}
|
||||||
|
catch ( PlexusContainerException e )
|
||||||
|
{
|
||||||
|
throw new PluginManagerException( "Failed to create realm for plugin '" + projectPlugin + ".", e );
|
||||||
|
}
|
||||||
|
catch ( NoSuchRealmException e )
|
||||||
|
{
|
||||||
|
throw new PluginManagerException( "Failed to import Xpp3Dom from parent realm for plugin: '" +
|
||||||
|
projectPlugin + ".", e );
|
||||||
|
}
|
||||||
|
|
||||||
|
getLogger().debug( "Realm for plugin: " + realmKey + ":\n" + componentRealm );
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// The PluginCollector will now know about the plugin we
|
||||||
|
// are trying to load
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return componentRealm;
|
||||||
|
}
|
||||||
|
|
||||||
|
List/* <URL> */generateJarsListForArtifacts( Set/* <Artifact> */artifacts )
|
||||||
|
{
|
||||||
|
List/* <URL> */jars = new ArrayList();
|
||||||
|
|
||||||
|
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
|
||||||
|
{
|
||||||
|
Artifact artifact = (Artifact) i.next();
|
||||||
|
|
||||||
|
jars.add( artifact.getFile() );
|
||||||
|
}
|
||||||
|
|
||||||
|
return jars;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String generateChildContainerName( Plugin plugin, Set artifacts )
|
||||||
|
{
|
||||||
|
return plugin.getKey() + ":" + plugin.getVersion() + ":" + getHashOfArtifacts( artifacts );
|
||||||
|
}
|
||||||
|
|
||||||
|
static long getHashOfArtifacts( Set a )
|
||||||
|
{
|
||||||
|
long i = 1;
|
||||||
|
Iterator/* <Artifact> */iterator = a.iterator();
|
||||||
|
while ( iterator.hasNext() )
|
||||||
|
{
|
||||||
|
Artifact artifact = (Artifact) iterator.next();
|
||||||
|
i = ( i * artifact.hashCode() ) % 2147483647 /* big prime */;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,23 +26,33 @@ import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener;
|
||||||
import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
|
import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
|
||||||
import org.codehaus.plexus.logging.AbstractLogEnabled;
|
import org.codehaus.plexus.logging.AbstractLogEnabled;
|
||||||
|
|
||||||
|
import com.sun.jmx.remote.util.OrderClassLoaders;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.SortedMap;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
public class MavenPluginCollector
|
public class MavenPluginCollector
|
||||||
extends AbstractLogEnabled
|
extends AbstractLogEnabled
|
||||||
implements ComponentDiscoveryListener
|
implements ComponentDiscoveryListener
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Map from pluginDescriptor.getKey (groupId:artifactId) into (ordered) map from version into pluginDescriptor
|
||||||
|
* Internal map is ordered to make sure that builds are determinic (used pluginVersion is determined)
|
||||||
|
*/
|
||||||
|
private Map/* <String,OrderedMap<String,PluginDescriptor>> */pluginDescriptors = new HashMap();
|
||||||
|
|
||||||
private Set pluginsInProcess = new HashSet();
|
private Map/* <String,OrderedMap<String,PluginDescriptor>> */pluginIdsByPrefix = new HashMap();
|
||||||
|
|
||||||
private Map pluginDescriptors = new HashMap();
|
|
||||||
|
|
||||||
private Map pluginIdsByPrefix = new HashMap();
|
|
||||||
|
|
||||||
|
public String getId()
|
||||||
|
{
|
||||||
|
return "maven-plugin-collector";
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
// Mojo discovery
|
// Mojo discovery
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
@ -53,66 +63,125 @@ public class MavenPluginCollector
|
||||||
if ( componentSetDescriptor instanceof PluginDescriptor )
|
if ( componentSetDescriptor instanceof PluginDescriptor )
|
||||||
{
|
{
|
||||||
PluginDescriptor pluginDescriptor = (PluginDescriptor) componentSetDescriptor;
|
PluginDescriptor pluginDescriptor = (PluginDescriptor) componentSetDescriptor;
|
||||||
|
|
||||||
// TODO: see comment in getPluginDescriptor
|
|
||||||
String key = Plugin.constructKey( pluginDescriptor.getGroupId(), pluginDescriptor.getArtifactId() );
|
|
||||||
|
|
||||||
if ( !pluginsInProcess.contains( key ) )
|
putIntoPluginDescriptors( pluginDescriptor );
|
||||||
{
|
putIntoPluginIdsByPrefix( pluginDescriptor );
|
||||||
pluginsInProcess.add( key );
|
|
||||||
|
|
||||||
getLogger().debug( this + ": Discovered plugin: " + key );
|
|
||||||
|
|
||||||
pluginDescriptors.put( key, pluginDescriptor );
|
|
||||||
|
|
||||||
// TODO: throw an (not runtime) exception if there is a prefix overlap - means doing so elsewhere
|
|
||||||
// we also need to deal with multiple versions somehow - currently, first wins
|
|
||||||
if ( !pluginIdsByPrefix.containsKey( pluginDescriptor.getGoalPrefix() ) )
|
|
||||||
{
|
|
||||||
pluginIdsByPrefix.put( pluginDescriptor.getGoalPrefix(), pluginDescriptor );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId()
|
|
||||||
{
|
|
||||||
return "maven-plugin-collector";
|
|
||||||
}
|
|
||||||
|
|
||||||
public PluginDescriptor getPluginDescriptor( Plugin plugin )
|
public PluginDescriptor getPluginDescriptor( Plugin plugin )
|
||||||
{
|
{
|
||||||
// TODO: include version, but can't do this in the plugin manager as it is not resolved to the right version
|
SortedMap/* <String,PluginDescriptor> */pluginVersions = (SortedMap) pluginDescriptors.get( plugin.getKey() );
|
||||||
// at that point. Instead, move the duplication check to the artifact container, or store it locally based on
|
if ( pluginVersions != null )
|
||||||
// the unresolved version?
|
{
|
||||||
return (PluginDescriptor) pluginDescriptors.get( plugin.getKey() );
|
PluginDescriptor res;
|
||||||
|
if ( plugin.getVersion() != null )
|
||||||
|
{
|
||||||
|
res = (PluginDescriptor) pluginVersions.get( plugin.getVersion() );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = getDefaultPluginDescriptorVersion( pluginVersions );
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PluginDescriptor getDefaultPluginDescriptorVersion( SortedMap pluginVersions )
|
||||||
|
{
|
||||||
|
if ( pluginVersions.size() > 0 )
|
||||||
|
{
|
||||||
|
return (PluginDescriptor) pluginVersions.get( pluginVersions.lastKey() );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPluginInstalled( Plugin plugin )
|
public boolean isPluginInstalled( Plugin plugin )
|
||||||
{
|
{
|
||||||
// TODO: see comment in getPluginDescriptor
|
// TODO: see comment in getPluginDescriptor
|
||||||
return pluginDescriptors.containsKey( plugin.getKey() );
|
return getPluginDescriptor( plugin ) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PluginDescriptor getPluginDescriptorForPrefix( String prefix )
|
public PluginDescriptor getPluginDescriptorForPrefix( String prefix )
|
||||||
{
|
{
|
||||||
return (PluginDescriptor) pluginIdsByPrefix.get( prefix );
|
return getPluginDescriptorForPrefix( prefix, null );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flushPluginDescriptor( Plugin plugin )
|
public PluginDescriptor getPluginDescriptorForPrefix( String prefix, String version )
|
||||||
{
|
{
|
||||||
pluginsInProcess.remove( plugin.getKey() );
|
SortedMap/* <String,PluginDescriptor> */pluginVersions = (SortedMap) pluginIdsByPrefix.get( prefix );
|
||||||
pluginDescriptors.remove( plugin.getKey() );
|
if ( pluginVersions != null )
|
||||||
|
|
||||||
for ( Iterator it = pluginIdsByPrefix.entrySet().iterator(); it.hasNext(); )
|
|
||||||
{
|
{
|
||||||
Map.Entry entry = (Map.Entry) it.next();
|
PluginDescriptor res;
|
||||||
|
if ( version != null )
|
||||||
if ( plugin.getKey().equals( entry.getValue() ) )
|
|
||||||
{
|
{
|
||||||
it.remove();
|
res = (PluginDescriptor) pluginVersions.get( version );
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = getDefaultPluginDescriptorVersion( pluginVersions );
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public void flushPluginDescriptor( Plugin plugin )
|
||||||
|
// {
|
||||||
|
// getPluginDescriptor( plugin ).cleanPluginDescriptor();
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Puts given pluginDescriptor into pluginDescriptors map (if the map does not contains plugin for specified maven
|
||||||
|
* version)
|
||||||
|
*
|
||||||
|
* @param pluginDescriptor
|
||||||
|
*/
|
||||||
|
protected void putIntoPluginDescriptors( PluginDescriptor pluginDescriptor )
|
||||||
|
{
|
||||||
|
String key = Plugin.constructKey( pluginDescriptor.getGroupId(), pluginDescriptor.getArtifactId() );
|
||||||
|
|
||||||
|
SortedMap/* <String,PluginDescriptor> */descriptorsVersions = (SortedMap) pluginDescriptors.get( key );
|
||||||
|
if ( descriptorsVersions == null )
|
||||||
|
{
|
||||||
|
descriptorsVersions = new TreeMap();
|
||||||
|
pluginDescriptors.put( key, descriptorsVersions );
|
||||||
|
}
|
||||||
|
|
||||||
|
putIntoVersionsMap( descriptorsVersions, pluginDescriptor );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void putIntoVersionsMap( SortedMap/* <String(version),PluginDescriptor> */pluginVersions,
|
||||||
|
PluginDescriptor pluginDescriptor )
|
||||||
|
{
|
||||||
|
if ( !pluginVersions.containsKey( pluginDescriptor.getVersion() ) )
|
||||||
|
{
|
||||||
|
pluginVersions.put( pluginDescriptor.getVersion(), pluginDescriptor );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void putIntoPluginIdsByPrefix( PluginDescriptor pluginDescriptor )
|
||||||
|
{
|
||||||
|
String goalPrefix = pluginDescriptor.getGoalPrefix();
|
||||||
|
|
||||||
|
SortedMap/* <String,PluginDescriptor> */descriptorsVersions = (SortedMap) pluginIdsByPrefix.get( goalPrefix );
|
||||||
|
if ( descriptorsVersions == null )
|
||||||
|
{
|
||||||
|
descriptorsVersions = new TreeMap();
|
||||||
|
pluginIdsByPrefix.put( goalPrefix, descriptorsVersions );
|
||||||
|
}
|
||||||
|
|
||||||
|
putIntoVersionsMap( descriptorsVersions, pluginDescriptor );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package org.apache.maven.plugin;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.maven.artifact.Artifact;
|
||||||
|
import org.apache.maven.model.Plugin;
|
||||||
|
import org.codehaus.plexus.classworlds.realm.ClassRealm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:piotr@tabor.waw.pl">Piotr Tabor</a>
|
||||||
|
*/
|
||||||
|
public interface PluginRealmManager
|
||||||
|
{
|
||||||
|
public static final String ROLE=PluginRealmManager.class.getName();
|
||||||
|
|
||||||
|
public ClassRealm getOrCreateRealm( Plugin projectPlugin, Artifact pluginArtifact, Set artifacts ) throws PluginManagerException;
|
||||||
|
}
|
|
@ -64,7 +64,10 @@ under the License.
|
||||||
<requirement>
|
<requirement>
|
||||||
<role>org.apache.maven.plugin.MavenPluginCollector</role>
|
<role>org.apache.maven.plugin.MavenPluginCollector</role>
|
||||||
</requirement>
|
</requirement>
|
||||||
<requirement>
|
<requirement>
|
||||||
|
<role>org.apache.maven.plugin.PluginRealmManager</role>
|
||||||
|
</requirement>
|
||||||
|
<requirement>
|
||||||
<role>org.apache.maven.plugin.version.PluginVersionManager</role>
|
<role>org.apache.maven.plugin.version.PluginVersionManager</role>
|
||||||
<role-hint>default</role-hint>
|
<role-hint>default</role-hint>
|
||||||
</requirement>
|
</requirement>
|
||||||
|
@ -728,5 +731,14 @@ under the License.
|
||||||
</requirement>
|
</requirement>
|
||||||
</requirements>
|
</requirements>
|
||||||
</component>
|
</component>
|
||||||
|
<component>
|
||||||
|
<role>org.apache.maven.plugin.PluginRealmManager</role>
|
||||||
|
<implementation>org.apache.maven.plugin.DefaultPluginRealmManager</implementation>
|
||||||
|
<requirements>
|
||||||
|
<requirement>
|
||||||
|
<role>org.codehaus.plexus.PlexusContainer</role>
|
||||||
|
</requirement>
|
||||||
|
</requirements>
|
||||||
|
</component>
|
||||||
</components>
|
</components>
|
||||||
</component-set>
|
</component-set>
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
package org.apache.maven.plugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
import org.apache.maven.artifact.Artifact;
|
||||||
|
import org.apache.maven.artifact.factory.ArtifactFactory;
|
||||||
|
import org.codehaus.plexus.PlexusTestCase;
|
||||||
|
|
||||||
|
public class DefaultPluginRealmManagerTest
|
||||||
|
extends PlexusTestCase
|
||||||
|
{
|
||||||
|
public void testCreateComponent()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
assertNotNull( "Cannot lookup component: " + PluginRealmManager.ROLE, lookup( PluginRealmManager.ROLE ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGenerateJarsListForArtifactsEmpty()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
DefaultPluginRealmManager defaultPluginRealmManager = new DefaultPluginRealmManager();
|
||||||
|
|
||||||
|
assertEquals( "List of jars generated by empty set should be empty", 0,
|
||||||
|
defaultPluginRealmManager.generateJarsListForArtifacts( Collections.EMPTY_SET ).size() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGenerateJarsListForArtifacts()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
DefaultPluginRealmManager defaultPluginRealmManager = new DefaultPluginRealmManager();
|
||||||
|
|
||||||
|
/*Different artifactId*/
|
||||||
|
List/* <Artifact> */result =
|
||||||
|
defaultPluginRealmManager.generateJarsListForArtifacts( createArtifactSet( HashSet.class, 5 ) );
|
||||||
|
checkListOfNumeredFiles( 5, result );
|
||||||
|
|
||||||
|
/*Different groups*/
|
||||||
|
result =
|
||||||
|
defaultPluginRealmManager.generateJarsListForArtifacts( createArtifactSetGroups( HashSet.class, 6 ) );
|
||||||
|
checkListOfNumeredFiles( 6, result );
|
||||||
|
|
||||||
|
/*Different versions*/
|
||||||
|
result =
|
||||||
|
defaultPluginRealmManager.generateJarsListForArtifacts( createArtifactSetVersions( HashSet.class, 4 ) );
|
||||||
|
checkListOfNumeredFiles( 4, result );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetHashOfArtifacts() throws Exception
|
||||||
|
{
|
||||||
|
assertEquals("Hash code of empty set should be always the same", DefaultPluginRealmManager.getHashOfArtifacts( Collections.EMPTY_SET), DefaultPluginRealmManager.getHashOfArtifacts( Collections.EMPTY_SET));
|
||||||
|
assertEquals("Hash code should be the same for the same content of artifacts list",DefaultPluginRealmManager.getHashOfArtifacts(createArtifactSet(HashSet.class,5)),DefaultPluginRealmManager.getHashOfArtifacts(createArtifactSet(HashSet.class,5)));
|
||||||
|
assertEquals("Hash code should not depend on set implementation",DefaultPluginRealmManager.getHashOfArtifacts(createArtifactSet(HashSet.class,5)),DefaultPluginRealmManager.getHashOfArtifacts(createArtifactSet(TreeSet.class,5)));
|
||||||
|
|
||||||
|
assertTrue( "Hash should be different for different content", DefaultPluginRealmManager.getHashOfArtifacts(createArtifactSet(HashSet.class,5))!=DefaultPluginRealmManager.getHashOfArtifacts(createArtifactSet(TreeSet.class,6)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// =========================== Helpers ====================================================
|
||||||
|
|
||||||
|
private void checkListOfNumeredFiles( int count, List/* <File> */result )
|
||||||
|
throws MalformedURLException
|
||||||
|
{
|
||||||
|
assertEquals( "Unexpected size of created urls list: ", count, result.size() );
|
||||||
|
for ( int i = 0; i < count; i++ )
|
||||||
|
{
|
||||||
|
File expectedFile = createNumberedFile( i );
|
||||||
|
assertTrue( "Expected url not found: " + expectedFile + " in: " + result, result.contains( expectedFile ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Artifact createArtifact( String groupId, String artifactId, String version )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
|
||||||
|
|
||||||
|
// TODO: used to be SCOPE_COMPILE, check
|
||||||
|
return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Set/* <Artifact> */createArtifactSet( Class/* <? extends Set> */setClass, int count )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
Set/* <Artifact> */artifactSet = (Set) setClass.newInstance();
|
||||||
|
for ( int i = 0; i < count; i++ )
|
||||||
|
{
|
||||||
|
Artifact artifact = createArtifact( "testGroup", "testArtifact_" + i, "1.0" );
|
||||||
|
artifact.setFile( createNumberedFile( i ) );
|
||||||
|
artifactSet.add( artifact );
|
||||||
|
}
|
||||||
|
return artifactSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Set/* <Artifact> */createArtifactSetVersions( Class/* <? extends Set> */setClass, int count )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
Set/* <Artifact> */artifactSet = (Set) setClass.newInstance();
|
||||||
|
for ( int i = 0; i < count; i++ )
|
||||||
|
{
|
||||||
|
Artifact artifact = createArtifact( "testGroup", "testArtifact", i + ".0" );
|
||||||
|
artifact.setFile( createNumberedFile( i ) );
|
||||||
|
artifactSet.add( artifact );
|
||||||
|
}
|
||||||
|
return artifactSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Set/* <Artifact> */createArtifactSetGroups( Class/* <? extends Set> */setClass, int count )
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
Set/* <Artifact> */artifactSet = (Set) setClass.newInstance();
|
||||||
|
for ( int i = 0; i < count; i++ )
|
||||||
|
{
|
||||||
|
Artifact artifact = createArtifact( "testGroup" + i, "testArtifact_", "1.0" );
|
||||||
|
artifact.setFile( createNumberedFile( i ) );
|
||||||
|
artifactSet.add( artifact );
|
||||||
|
}
|
||||||
|
return artifactSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected File createNumberedFile( int i )
|
||||||
|
{
|
||||||
|
return new File( "/not_existing_file_" + i + ".jar" );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue