diff --git a/maven-project/src/main/java/org/apache/maven/profiles/injection/DefaultProfileInjector.java b/maven-project/src/main/java/org/apache/maven/profiles/injection/DefaultProfileInjector.java index 1e769815c1..c3822f5b2e 100644 --- a/maven-project/src/main/java/org/apache/maven/profiles/injection/DefaultProfileInjector.java +++ b/maven-project/src/main/java/org/apache/maven/profiles/injection/DefaultProfileInjector.java @@ -66,7 +66,8 @@ public class DefaultProfileInjector public void inject( Profile profile, Model model ) { - injectDependencies( profile, model ); + + model.setDependencies( injectDependencies( profile.getDependencies(), model.getDependencies() ) ); injectModules( profile, model ); @@ -213,6 +214,8 @@ public class DefaultProfileInjector modelPlugin.setVersion( profilePlugin.getVersion() ); } + modelPlugin.setDependencies( injectDependencies( profilePlugin.getDependencies(), modelPlugin.getDependencies() ) ); + // merge the lists of goals that are not attached to an injectConfigurationContainer( profilePlugin, modelPlugin ); @@ -590,33 +593,29 @@ public class DefaultProfileInjector recessive.flushReportSetMap(); } - private void injectDependencies( Profile profile, Model model ) + private List injectDependencies( List profileDeps, List modelDeps ) { Map depsMap = new LinkedHashMap(); - List deps = model.getDependencies(); - - if ( deps != null ) + if ( modelDeps != null ) { - for ( Iterator it = deps.iterator(); it.hasNext(); ) + for ( Iterator it = modelDeps.iterator(); it.hasNext(); ) { Dependency dependency = (Dependency) it.next(); depsMap.put( dependency.getManagementKey(), dependency ); } } - deps = profile.getDependencies(); - - if ( deps != null ) + if ( profileDeps != null ) { - for ( Iterator it = deps.iterator(); it.hasNext(); ) + for ( Iterator it = profileDeps.iterator(); it.hasNext(); ) { Dependency dependency = (Dependency) it.next(); depsMap.put( dependency.getManagementKey(), dependency ); } } - model.setDependencies( new ArrayList( depsMap.values() ) ); + return new ArrayList( depsMap.values() ); } } diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 770f9347db..499319556b 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -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); try diff --git a/maven-project/src/main/java/org/apache/maven/project/ModelUtils.java b/maven-project/src/main/java/org/apache/maven/project/ModelUtils.java index bb062bcfd7..2d2269b799 100644 --- a/maven-project/src/main/java/org/apache/maven/project/ModelUtils.java +++ b/maven-project/src/main/java/org/apache/maven/project/ModelUtils.java @@ -61,6 +61,54 @@ import java.util.TreeMap; 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: * diff --git a/maven-project/src/test/java/org/apache/maven/profiles/injection/DefaultProfileInjectorTest.java b/maven-project/src/test/java/org/apache/maven/profiles/injection/DefaultProfileInjectorTest.java index c7ed057e79..11de3a2532 100644 --- a/maven-project/src/test/java/org/apache/maven/profiles/injection/DefaultProfileInjectorTest.java +++ b/maven-project/src/test/java/org/apache/maven/profiles/injection/DefaultProfileInjectorTest.java @@ -19,16 +19,15 @@ package org.apache.maven.profiles.injection; * under the License. */ -import junit.framework.TestCase; import org.apache.maven.model.Build; import org.apache.maven.model.BuildBase; +import org.apache.maven.model.Dependency; import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginContainer; import org.apache.maven.model.PluginExecution; import org.apache.maven.model.Profile; import org.apache.maven.model.Repository; -import org.apache.maven.profiles.injection.DefaultProfileInjector; import org.codehaus.plexus.util.xml.Xpp3Dom; import java.util.Collections; @@ -36,10 +35,43 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import junit.framework.TestCase; + public class DefaultProfileInjectorTest 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: * diff --git a/maven-project/src/test/java/org/apache/maven/project/ModelUtilsTest.java b/maven-project/src/test/java/org/apache/maven/project/ModelUtilsTest.java index 55918c5390..8b1d4b4c34 100644 --- a/maven-project/src/test/java/org/apache/maven/project/ModelUtilsTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/ModelUtilsTest.java @@ -19,7 +19,6 @@ package org.apache.maven.project; * under the License. */ -import junit.framework.TestCase; import org.apache.maven.model.Build; import org.apache.maven.model.Dependency; import org.apache.maven.model.Plugin; @@ -36,10 +35,39 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import junit.framework.TestCase; + public class ModelUtilsTest 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() { PluginContainer parent = new PluginContainer();