mirror of https://github.com/apache/maven.git
[MNG-3220] Allow managed dependencies to be imported into other projects
o Restored import scope for 3.x git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@795611 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
df346c93b0
commit
a48742bc1a
|
@ -23,9 +23,12 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.maven.model.Dependency;
|
||||
import org.apache.maven.model.DependencyManagement;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.Parent;
|
||||
import org.apache.maven.model.Profile;
|
||||
|
@ -35,6 +38,7 @@ import org.apache.maven.model.interpolation.ModelInterpolationException;
|
|||
import org.apache.maven.model.interpolation.ModelInterpolator;
|
||||
import org.apache.maven.model.io.ModelParseException;
|
||||
import org.apache.maven.model.io.ModelReader;
|
||||
import org.apache.maven.model.management.DependencyManagementImporter;
|
||||
import org.apache.maven.model.management.DependencyManagementInjector;
|
||||
import org.apache.maven.model.management.PluginManagementInjector;
|
||||
import org.apache.maven.model.normalization.ModelNormalizer;
|
||||
|
@ -97,6 +101,9 @@ public class DefaultModelBuilder
|
|||
@Requirement
|
||||
private DependencyManagementInjector dependencyManagementInjector;
|
||||
|
||||
@Requirement
|
||||
private DependencyManagementImporter dependencyManagementImporter;
|
||||
|
||||
@Requirement
|
||||
private LifecycleBindingsInjector lifecycleBindingsInjector;
|
||||
|
||||
|
@ -177,6 +184,8 @@ public class DefaultModelBuilder
|
|||
|
||||
pluginManagementInjector.injectManagement( resultModel, request );
|
||||
|
||||
importDependencyManagement( resultModel, request, problems );
|
||||
|
||||
dependencyManagementInjector.injectManagement( resultModel, request );
|
||||
|
||||
modelNormalizer.injectDefaultValues( resultModel, request );
|
||||
|
@ -511,6 +520,86 @@ public class DefaultModelBuilder
|
|||
return ModelUtils.cloneModel( superPomProvider.getSuperModel( "4.0.0" ) );
|
||||
}
|
||||
|
||||
private void importDependencyManagement( Model model, ModelBuildingRequest request, List<ModelProblem> problems )
|
||||
{
|
||||
DependencyManagement depMngt = model.getDependencyManagement();
|
||||
|
||||
if ( depMngt == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ModelResolver modelResolver = request.getModelResolver();
|
||||
|
||||
ModelBuildingRequest importRequest = null;
|
||||
|
||||
List<Model> importModels = null;
|
||||
|
||||
for ( Iterator<Dependency> it = depMngt.getDependencies().iterator(); it.hasNext(); )
|
||||
{
|
||||
Dependency dependency = it.next();
|
||||
|
||||
if ( !"pom".equals( dependency.getType() ) || !"import".equals( dependency.getScope() ) )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
it.remove();
|
||||
|
||||
if ( modelResolver == null )
|
||||
{
|
||||
throw new IllegalArgumentException( "no model resolver provided, cannot resolve import POM "
|
||||
+ toId( dependency ) + " for POM " + toSourceHint( model ) );
|
||||
}
|
||||
|
||||
ModelSource importSource;
|
||||
try
|
||||
{
|
||||
importSource =
|
||||
modelResolver.resolveModel( dependency.getGroupId(), dependency.getArtifactId(),
|
||||
dependency.getVersion() );
|
||||
}
|
||||
catch ( UnresolvableModelException e )
|
||||
{
|
||||
problems.add( new ModelProblem( "Non-resolvable import POM " + toId( dependency ) + " for POM "
|
||||
+ toSourceHint( model ) + ": " + e.getMessage(), ModelProblem.Severity.ERROR,
|
||||
toSourceHint( model ), e ) );
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( importRequest == null )
|
||||
{
|
||||
importRequest = new DefaultModelBuildingRequest();
|
||||
importRequest.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
|
||||
}
|
||||
|
||||
importRequest.setModelSource( importSource );
|
||||
importRequest.setModelResolver( modelResolver.newCopy() );
|
||||
|
||||
ModelBuildingResult importResult;
|
||||
try
|
||||
{
|
||||
importResult = build( importRequest );
|
||||
}
|
||||
catch ( ModelBuildingException e )
|
||||
{
|
||||
problems.addAll( e.getProblems() );
|
||||
continue;
|
||||
}
|
||||
|
||||
problems.addAll( importResult.getProblems() );
|
||||
|
||||
if ( importModels == null )
|
||||
{
|
||||
importModels = new ArrayList<Model>();
|
||||
}
|
||||
|
||||
importModels.add( importResult.getEffectiveModel() );
|
||||
}
|
||||
|
||||
dependencyManagementImporter.importManagement( model, importModels, request );
|
||||
}
|
||||
|
||||
private void fireBuildExtensionsAssembled( Model model, ModelBuildingRequest request, List<ModelProblem> problems )
|
||||
throws ModelBuildingException
|
||||
{
|
||||
|
@ -609,4 +698,17 @@ public class DefaultModelBuilder
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
private String toId( Dependency dependency )
|
||||
{
|
||||
StringBuilder buffer = new StringBuilder( 64 );
|
||||
|
||||
buffer.append( dependency.getGroupId() );
|
||||
buffer.append( ':' );
|
||||
buffer.append( dependency.getArtifactId() );
|
||||
buffer.append( ':' );
|
||||
buffer.append( dependency.getVersion() );
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
package org.apache.maven.model.management;
|
||||
|
||||
/*
|
||||
* 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.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.maven.model.Dependency;
|
||||
import org.apache.maven.model.DependencyManagement;
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.building.ModelBuildingRequest;
|
||||
import org.codehaus.plexus.component.annotations.Component;
|
||||
|
||||
/**
|
||||
* Handles the import of dependency management from other models into the target model.
|
||||
*
|
||||
* @author Benjamin Bentmann
|
||||
*/
|
||||
@Component( role = DependencyManagementImporter.class )
|
||||
public class DefaultDependencyManagementImporter
|
||||
implements DependencyManagementImporter
|
||||
{
|
||||
|
||||
public void importManagement( Model target, List<? extends Model> sources, ModelBuildingRequest request )
|
||||
{
|
||||
if ( sources != null && !sources.isEmpty() )
|
||||
{
|
||||
Map<String, Dependency> dependencies = new LinkedHashMap<String, Dependency>();
|
||||
|
||||
DependencyManagement depMngt = target.getDependencyManagement();
|
||||
|
||||
if ( depMngt != null )
|
||||
{
|
||||
for ( Dependency dependency : depMngt.getDependencies() )
|
||||
{
|
||||
dependencies.put( dependency.getManagementKey(), dependency );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
depMngt = new DependencyManagement();
|
||||
target.setDependencyManagement( depMngt );
|
||||
}
|
||||
|
||||
for ( Model source : sources )
|
||||
{
|
||||
DependencyManagement depMngtImport = source.getDependencyManagement();
|
||||
|
||||
if ( depMngtImport != null )
|
||||
{
|
||||
for ( Dependency dependency : depMngtImport.getDependencies() )
|
||||
{
|
||||
String key = dependency.getManagementKey();
|
||||
if ( !dependencies.containsKey( key ) )
|
||||
{
|
||||
dependencies.put( key, dependency );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
depMngt.setDependencies( new ArrayList<Dependency>( dependencies.values() ) );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package org.apache.maven.model.management;
|
||||
|
||||
/*
|
||||
* 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.List;
|
||||
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.building.ModelBuildingRequest;
|
||||
|
||||
/**
|
||||
* Handles the import of dependency management from other models into the target model.
|
||||
*
|
||||
* @author Benjamin Bentmann
|
||||
*/
|
||||
public interface DependencyManagementImporter
|
||||
{
|
||||
|
||||
/**
|
||||
* Imports the dependency management of the specified source models into the given target model.
|
||||
*
|
||||
* @param target The model into which to import the dependency management section, must not be <code>null</code>.
|
||||
* @param sources The models from which to import the dependency management sections, may be <code>null</code>.
|
||||
* @param request The model building request that holds further settings, must not be {@code null}.
|
||||
*/
|
||||
void importManagement( Model target, List<? extends Model> sources, ModelBuildingRequest request );
|
||||
|
||||
}
|
Loading…
Reference in New Issue