[MNG-2145] Correcting several merge issues between profiles and main build, and also of duplicate plugin declarations within a single build section.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@617325 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2008-02-01 01:53:21 +00:00
parent f838337b7a
commit 5e8a0a0906
5 changed files with 124 additions and 14 deletions

View File

@ -66,7 +66,8 @@ public class DefaultProfileInjector
public void inject( Profile profile, Model model ) public void inject( Profile profile, Model model )
{ {
injectDependencies( profile, model );
model.setDependencies( injectDependencies( profile.getDependencies(), model.getDependencies() ) );
injectModules( profile, model ); injectModules( profile, model );
@ -213,6 +214,8 @@ public class DefaultProfileInjector
modelPlugin.setVersion( profilePlugin.getVersion() ); modelPlugin.setVersion( profilePlugin.getVersion() );
} }
modelPlugin.setDependencies( injectDependencies( profilePlugin.getDependencies(), modelPlugin.getDependencies() ) );
// merge the lists of goals that are not attached to an <execution/> // merge the lists of goals that are not attached to an <execution/>
injectConfigurationContainer( profilePlugin, modelPlugin ); injectConfigurationContainer( profilePlugin, modelPlugin );
@ -590,33 +593,29 @@ public class DefaultProfileInjector
recessive.flushReportSetMap(); recessive.flushReportSetMap();
} }
private void injectDependencies( Profile profile, Model model ) private List injectDependencies( List profileDeps, List modelDeps )
{ {
Map depsMap = new LinkedHashMap(); Map depsMap = new LinkedHashMap();
List deps = model.getDependencies(); if ( modelDeps != null )
if ( deps != null )
{ {
for ( Iterator it = deps.iterator(); it.hasNext(); ) for ( Iterator it = modelDeps.iterator(); it.hasNext(); )
{ {
Dependency dependency = (Dependency) it.next(); Dependency dependency = (Dependency) it.next();
depsMap.put( dependency.getManagementKey(), dependency ); depsMap.put( dependency.getManagementKey(), dependency );
} }
} }
deps = profile.getDependencies(); if ( profileDeps != null )
if ( deps != null )
{ {
for ( Iterator it = deps.iterator(); it.hasNext(); ) for ( Iterator it = profileDeps.iterator(); it.hasNext(); )
{ {
Dependency dependency = (Dependency) it.next(); Dependency dependency = (Dependency) it.next();
depsMap.put( dependency.getManagementKey(), dependency ); depsMap.put( dependency.getManagementKey(), dependency );
} }
} }
model.setDependencies( new ArrayList( depsMap.values() ) ); return new ArrayList( depsMap.values() );
} }
} }

View File

@ -739,6 +739,9 @@ public class DefaultMavenProjectBuilder
} }
} }
// merge any duplicated plugin definitions together, using the first appearance as the dominant one.
ModelUtils.mergeDuplicatePluginDefinitions( project.getModel().getBuild() );
mergeManagedDependencies(project.getModel(), localRepository, parentSearchRepositories); mergeManagedDependencies(project.getModel(), localRepository, parentSearchRepositories);
try try

View File

@ -61,6 +61,54 @@ import java.util.TreeMap;
public final class ModelUtils public final class ModelUtils
{ {
/**
* Given this plugin list:
*
* A1 -> B -> C -> A2 -> D
*
* Rearrange it to this:
*
* A(A1 + A2) -> B -> C -> D
*
* In cases of overlapping definitions, A1 is overridden by A2
*
*/
public static void mergeDuplicatePluginDefinitions( PluginContainer pluginContainer )
{
if ( pluginContainer == null )
{
return;
}
List originalPlugins = pluginContainer.getPlugins();
if ( ( originalPlugins == null ) || originalPlugins.isEmpty() )
{
return;
}
List normalized = new ArrayList( originalPlugins.size() );
for ( Iterator it = originalPlugins.iterator(); it.hasNext(); )
{
Plugin currentPlugin = (Plugin) it.next();
if ( normalized.contains( currentPlugin ) )
{
int idx = normalized.indexOf( currentPlugin );
Plugin firstPlugin = (Plugin) normalized.get( idx );
mergePluginDefinitions( firstPlugin, currentPlugin, false );
}
else
{
normalized.add( currentPlugin );
}
}
pluginContainer.setPlugins( normalized );
}
/** /**
* This should be the resulting ordering of plugins after merging: * This should be the resulting ordering of plugins after merging:
* *

View File

@ -19,16 +19,15 @@ package org.apache.maven.profiles.injection;
* under the License. * under the License.
*/ */
import junit.framework.TestCase;
import org.apache.maven.model.Build; import org.apache.maven.model.Build;
import org.apache.maven.model.BuildBase; import org.apache.maven.model.BuildBase;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginContainer; import org.apache.maven.model.PluginContainer;
import org.apache.maven.model.PluginExecution; import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.Profile; import org.apache.maven.model.Profile;
import org.apache.maven.model.Repository; import org.apache.maven.model.Repository;
import org.apache.maven.profiles.injection.DefaultProfileInjector;
import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.util.Collections; import java.util.Collections;
@ -36,10 +35,43 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase;
public class DefaultProfileInjectorTest public class DefaultProfileInjectorTest
extends TestCase extends TestCase
{ {
public void testShouldUseMainPluginDependencyVersionOverManagedDepVersion()
{
PluginContainer profile = new PluginContainer();
Plugin profilePlugin = createPlugin( "group", "artifact", "1", Collections.EMPTY_MAP );
Dependency profileDep = createDependency( "g", "a", "2" );
profilePlugin.addDependency( profileDep );
profile.addPlugin( profilePlugin );
PluginContainer model = new PluginContainer();
Plugin plugin = createPlugin( "group", "artifact", "1", Collections.EMPTY_MAP );
Dependency dep = createDependency( "g", "a", "1" );
plugin.addDependency( dep );
model.addPlugin( plugin );
new DefaultProfileInjector().injectPlugins( profile, model );
assertEquals( profileDep.getVersion(), ((Dependency) plugin.getDependencies().get( 0 ) ).getVersion() );
}
private Dependency createDependency( String gid,
String aid,
String ver )
{
Dependency dep = new Dependency();
dep.setGroupId( gid );
dep.setArtifactId( aid );
dep.setVersion( ver );
return dep;
}
/** /**
* Test that this is the resulting ordering of plugins after merging: * Test that this is the resulting ordering of plugins after merging:
* *

View File

@ -19,7 +19,6 @@ package org.apache.maven.project;
* under the License. * under the License.
*/ */
import junit.framework.TestCase;
import org.apache.maven.model.Build; import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency; import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
@ -36,10 +35,39 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase;
public class ModelUtilsTest public class ModelUtilsTest
extends TestCase extends TestCase
{ {
public void testShouldUseMainPluginDependencyVersionOverManagedDepVersion()
{
Plugin mgtPlugin = createPlugin( "group", "artifact", "1", Collections.EMPTY_MAP );
Dependency mgtDep = createDependency( "g", "a", "2" );
mgtPlugin.addDependency( mgtDep );
Plugin plugin = createPlugin( "group", "artifact", "1", Collections.EMPTY_MAP );
Dependency dep = createDependency( "g", "a", "1" );
plugin.addDependency( dep );
ModelUtils.mergePluginDefinitions( plugin, mgtPlugin, false );
assertEquals( dep.getVersion(), ((Dependency) plugin.getDependencies().get( 0 ) ).getVersion() );
}
private Dependency createDependency( String gid,
String aid,
String ver )
{
Dependency dep = new Dependency();
dep.setGroupId( gid );
dep.setArtifactId( aid );
dep.setVersion( ver );
return dep;
}
public void testShouldNotInheritPluginWithInheritanceSetToFalse() public void testShouldNotInheritPluginWithInheritanceSetToFalse()
{ {
PluginContainer parent = new PluginContainer(); PluginContainer parent = new PluginContainer();