diff --git a/maven-core/src/main/java/org/apache/maven/model/lifecycle/DefaultLifecycleBindingsInjector.java b/maven-core/src/main/java/org/apache/maven/model/lifecycle/DefaultLifecycleBindingsInjector.java new file mode 100644 index 0000000000..986b377cfa --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/model/lifecycle/DefaultLifecycleBindingsInjector.java @@ -0,0 +1,118 @@ +package org.apache.maven.model.lifecycle; + +/* + * 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.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.maven.lifecycle.LifecycleExecutor; +import org.apache.maven.model.Build; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginContainer; +import org.apache.maven.model.merge.MavenModelMerger; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; + +/** + * Handles injection of plugin executions induced by the lifecycle bindings for a packaging. + * + * @author Benjamin Bentmann + */ +@Component( role = LifecycleBindingsInjector.class ) +public class DefaultLifecycleBindingsInjector + implements LifecycleBindingsInjector +{ + + private LifecycleBindingsMerger merger = new LifecycleBindingsMerger(); + + @Requirement + private LifecycleExecutor lifecycle; + + public void injectLifecycleBindings( Model model ) + { + String packaging = model.getPackaging(); + + Collection defaultPlugins = lifecycle.getPluginsBoundByDefaultToAllLifecycles( packaging ); + + if ( !defaultPlugins.isEmpty() ) + { + Model lifecycleModel = new Model(); + lifecycleModel.setBuild( new Build() ); + lifecycleModel.getBuild().getPlugins().addAll( defaultPlugins ); + + merger.merge( model, lifecycleModel ); + } + } + + private static class LifecycleBindingsMerger + extends MavenModelMerger + { + + public void merge( Model target, Model source ) + { + if ( target.getBuild() == null ) + { + target.setBuild( new Build() ); + } + mergePluginContainer_Plugins( target.getBuild(), source.getBuild(), false, Collections.emptyMap() ); + } + + @Override + protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source, + boolean sourceDominant, Map context ) + { + List src = source.getPlugins(); + if ( !src.isEmpty() ) + { + List tgt = target.getPlugins(); + + Map merged = new LinkedHashMap( ( src.size() + tgt.size() ) * 2 ); + + for ( Iterator it = src.iterator(); it.hasNext(); ) + { + Plugin element = it.next(); + Object key = getPluginKey( element ); + merged.put( key, element ); + } + + for ( Iterator it = tgt.iterator(); it.hasNext(); ) + { + Plugin element = it.next(); + Object key = getPluginKey( element ); + Plugin existing = merged.get( key ); + if ( existing != null ) + { + mergePlugin( element, existing, sourceDominant, context ); + } + merged.put( key, element ); + } + + target.setPlugins( new ArrayList( merged.values() ) ); + } + } + } + +} diff --git a/maven-core/src/main/java/org/apache/maven/model/lifecycle/LifecycleBindingsInjector.java b/maven-core/src/main/java/org/apache/maven/model/lifecycle/LifecycleBindingsInjector.java new file mode 100644 index 0000000000..af3cdaf495 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/model/lifecycle/LifecycleBindingsInjector.java @@ -0,0 +1,40 @@ +package org.apache.maven.model.lifecycle; + +/* + * 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 injection of plugin executions induced by the lifecycle bindings for a packaging. + * + * @author Benjamin Bentmann + */ +public interface LifecycleBindingsInjector +{ + + /** + * Injects plugin executions induced by lifecycle bindings into the specified model. + * + * @param model The model into which to inject the default plugin executions for its packaging, must not be + * null. + */ + void injectLifecycleBindings( Model model ); + +} 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 f7ababd29c..8508867621 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 @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; @@ -46,6 +45,7 @@ import org.apache.maven.model.ProcessorContext; 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.processors.PluginProcessor; import org.apache.maven.profiles.DefaultProfileManager; import org.apache.maven.profiles.ProfileActivationException; @@ -87,6 +87,9 @@ public class DefaultMavenProjectBuilder @Requirement private Interpolator interpolator; + @Requirement + private LifecycleBindingsInjector lifecycleBindingsInjector; + @Requirement private ResolutionErrorHandler resolutionErrorHandler; @@ -152,9 +155,7 @@ public class DefaultMavenProjectBuilder { Model model = interpolateDomainModel( domainModel, configuration, pomFile ); - Set plugins = lifecycle.getPluginsBoundByDefaultToAllLifecycles( model.getPackaging() ); - - addPluginsToModel( model, plugins ); + lifecycleBindingsInjector.injectLifecycleBindings( model ); ProcessorContext.processManagementNodes( model ); @@ -337,43 +338,6 @@ public class DefaultMavenProjectBuilder return null; } - public static void addPluginsToModel( Model target, Set plugins ) - { - List mngPlugins = ( target.getBuild().getPluginManagement() != null ) ? target.getBuild().getPluginManagement().getPlugins() : new ArrayList(); - - List pomPlugins = new ArrayList( target.getBuild().getPlugins() ); - - List lifecyclePlugins = new ArrayList(); - - for ( Plugin p : plugins ) - { - //Go ahead and add version if exists in pluginManagement - don't use default version - Plugin mngPlugin = containsPlugin( p, mngPlugins ); - if ( mngPlugin != null && mngPlugin.getVersion() != null ) - { - //System.out.println("Set version:" + p.getVersion() + ": To = " + mngPlugin.getVersion()); - p.setVersion( mngPlugin.getVersion() ); - } - - Plugin pomPlugin = containsPlugin( p, pomPlugins ); - if ( pomPlugin == null ) - { - lifecyclePlugins.add( p ); - } - else - { - PluginProcessor.copy2( p, pomPlugin, true ); - if ( p.getConfiguration() != null ) - { - System.out.println( Xpp3Dom.mergeXpp3Dom( (Xpp3Dom) p.getConfiguration(), (Xpp3Dom) pomPlugin.getConfiguration() ) ); - } - } - } - pomPlugins.addAll( lifecyclePlugins ); - target.getBuild().setPlugins( pomPlugins ); - - } - private static Plugin containsPlugin( Plugin plugin, List plugins ) { for ( Plugin p : plugins ) @@ -473,7 +437,7 @@ public class DefaultMavenProjectBuilder if ( mavenParents.size() > 0 ) { - DomainModel dm = (DomainModel) mavenParents.get( 0 ); + DomainModel dm = mavenParents.get( 0 ); parentFile = dm.getFile(); domainModel.setParentFile( parentFile ); lineageCount = mavenParents.size(); @@ -487,7 +451,7 @@ public class DefaultMavenProjectBuilder //Process Profiles for ( DomainModel domain : domainModels ) { - DomainModel dm = (DomainModel) domain; + DomainModel dm = domain; if ( !dm.getModel().getProfiles().isEmpty() ) { 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 b6db7183bb..5ba526818f 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.lifecycle.LifecycleBindingsInjector + default + lifecycleBindingsInjector + org.apache.maven.artifact.resolver.ResolutionErrorHandler default