diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index dd1a300092..4ccab65ccb 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -42,6 +42,7 @@ import org.apache.maven.model.Profile; import org.apache.maven.model.interpolator.Interpolator; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.lifecycle.LifecycleBindingsInjector; +import org.apache.maven.model.normalization.Normalizer; import org.apache.maven.model.plugin.PluginConfigurationExpander; import org.apache.maven.profiles.DefaultProfileManager; import org.apache.maven.profiles.ProfileActivationException; @@ -77,7 +78,10 @@ public class DefaultMavenProjectBuilder private RepositorySystem repositorySystem; @Requirement - List listeners; + private List listeners; + + @Requirement + private Normalizer normalizer; @Requirement private Interpolator interpolator; @@ -390,6 +394,11 @@ public class DefaultMavenProjectBuilder domainModels.addAll( mavenParents ); } + for ( DomainModel domain : domainModels ) + { + normalizer.mergeDuplicates( domain.getModel() ); + } + domainModels.add( new DomainModel( getSuperModel(), false ) ); List profileModels = new ArrayList(); //Process Profiles diff --git a/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml b/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml index 4c55190018..a7867b708f 100644 --- a/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml +++ b/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml @@ -36,6 +36,11 @@ default interpolator + + org.apache.maven.model.normalization.Normalizer + default + normalizer + org.apache.maven.model.lifecycle.LifecycleBindingsInjector default diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/normalization/DefaultNormalizer.java b/maven-model-builder/src/main/java/org/apache/maven/model/normalization/DefaultNormalizer.java new file mode 100644 index 0000000000..43220273f6 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/normalization/DefaultNormalizer.java @@ -0,0 +1,80 @@ +package org.apache.maven.model.normalization; + +/* + * 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.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.maven.model.Build; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.merge.MavenModelMerger; +import org.codehaus.plexus.component.annotations.Component; + +/** + * Handles normalization of a model. + * + * @author Benjamin Bentmann + */ +@Component( role = Normalizer.class ) +public class DefaultNormalizer + implements Normalizer +{ + + private DuplicateMerger merger = new DuplicateMerger(); + + public void mergeDuplicates( Model model ) + { + Build build = model.getBuild(); + if ( build != null ) + { + List original = build.getPlugins(); + Map normalized = new LinkedHashMap(); + + for ( Plugin plugin : original ) + { + Object key = plugin.getKey(); + Plugin first = normalized.get( key ); + if ( first != null ) + { + merger.mergePlugin( plugin, first ); + } + normalized.put( key, plugin ); + } + + build.setPlugins( new ArrayList( normalized.values() ) ); + } + } + + private static class DuplicateMerger + extends MavenModelMerger + { + + public void mergePlugin( Plugin target, Plugin source ) + { + super.mergePlugin( target, source, false, Collections.emptyMap() ); + } + + } + +} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/normalization/Normalizer.java b/maven-model-builder/src/main/java/org/apache/maven/model/normalization/Normalizer.java new file mode 100644 index 0000000000..a8b751f417 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/normalization/Normalizer.java @@ -0,0 +1,39 @@ +package org.apache.maven.model.normalization; + +/* + * 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 org.apache.maven.model.Model; + +/** + * Handles normalization of a model. + * + * @author Benjamin Bentmann + */ +public interface Normalizer +{ + + /** + * Merges duplicate elements like multiple declarations of the same build plugin in the specified model. + * + * @param model The model whose duplicate elements should be merged, must not be null. + */ + void mergeDuplicates( Model model ); + +}