mirror of https://github.com/apache/maven.git
o Moved injection of default plugin executions defined by lifecycle bindings into dedicated component and decoupled from processors
git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@773402 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f48503a610
commit
f1f44eb281
|
@ -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<Plugin> 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<Object, Object> context )
|
||||
{
|
||||
List<Plugin> src = source.getPlugins();
|
||||
if ( !src.isEmpty() )
|
||||
{
|
||||
List<Plugin> tgt = target.getPlugins();
|
||||
|
||||
Map<Object, Plugin> merged = new LinkedHashMap<Object, Plugin>( ( src.size() + tgt.size() ) * 2 );
|
||||
|
||||
for ( Iterator<Plugin> it = src.iterator(); it.hasNext(); )
|
||||
{
|
||||
Plugin element = it.next();
|
||||
Object key = getPluginKey( element );
|
||||
merged.put( key, element );
|
||||
}
|
||||
|
||||
for ( Iterator<Plugin> 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<Plugin>( merged.values() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
* <code>null</code>.
|
||||
*/
|
||||
void injectLifecycleBindings( Model model );
|
||||
|
||||
}
|
|
@ -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<Plugin> 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<Plugin> plugins )
|
||||
{
|
||||
List<Plugin> mngPlugins = ( target.getBuild().getPluginManagement() != null ) ? target.getBuild().getPluginManagement().getPlugins() : new ArrayList<Plugin>();
|
||||
|
||||
List<Plugin> pomPlugins = new ArrayList<Plugin>( target.getBuild().getPlugins() );
|
||||
|
||||
List<Plugin> lifecyclePlugins = new ArrayList<Plugin>();
|
||||
|
||||
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<Plugin> 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() )
|
||||
{
|
||||
|
|
|
@ -36,6 +36,11 @@
|
|||
<role-hint>default</role-hint>
|
||||
<field-name>interpolator</field-name>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.model.lifecycle.LifecycleBindingsInjector</role>
|
||||
<role-hint>default</role-hint>
|
||||
<field-name>lifecycleBindingsInjector</field-name>
|
||||
</requirement>
|
||||
<requirement>
|
||||
<role>org.apache.maven.artifact.resolver.ResolutionErrorHandler</role>
|
||||
<role-hint>default</role-hint>
|
||||
|
|
Loading…
Reference in New Issue