maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bentm...@apache.org
Subject svn commit: r795611 - in /maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model: building/DefaultModelBuilder.java management/DefaultDependencyManagementImporter.java management/DependencyManagementImporter.java
Date Sun, 19 Jul 2009 21:14:20 GMT
Author: bentmann
Date: Sun Jul 19 21:14:17 2009
New Revision: 795611

URL: http://svn.apache.org/viewvc?rev=795611&view=rev
Log:
[MNG-3220] Allow managed dependencies to be imported into other projects

o Restored import scope for 3.x

Added:
    maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DefaultDependencyManagementImporter.java
  (with props)
    maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DependencyManagementImporter.java
  (with props)
Modified:
    maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java

Modified: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java?rev=795611&r1=795610&r2=795611&view=diff
==============================================================================
--- maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
(original)
+++ maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
Sun Jul 19 21:14:17 2009
@@ -23,9 +23,12 @@
 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.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;
@@ -98,6 +102,9 @@
     private DependencyManagementInjector dependencyManagementInjector;
 
     @Requirement
+    private DependencyManagementImporter dependencyManagementImporter;
+
+    @Requirement
     private LifecycleBindingsInjector lifecycleBindingsInjector;
 
     @Requirement
@@ -177,6 +184,8 @@
 
         pluginManagementInjector.injectManagement( resultModel, request );
 
+        importDependencyManagement( resultModel, request, problems );
+
         dependencyManagementInjector.injectManagement( resultModel, request );
 
         modelNormalizer.injectDefaultValues( resultModel, request );
@@ -511,6 +520,86 @@
         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 @@
         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();
+    }
+
 }

Added: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DefaultDependencyManagementImporter.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DefaultDependencyManagementImporter.java?rev=795611&view=auto
==============================================================================
--- maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DefaultDependencyManagementImporter.java
(added)
+++ maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DefaultDependencyManagementImporter.java
Sun Jul 19 21:14:17 2009
@@ -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()
) );
+        }
+    }
+
+}

Propchange: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DefaultDependencyManagementImporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DefaultDependencyManagementImporter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DependencyManagementImporter.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DependencyManagementImporter.java?rev=795611&view=auto
==============================================================================
--- maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DependencyManagementImporter.java
(added)
+++ maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DependencyManagementImporter.java
Sun Jul 19 21:14:17 2009
@@ -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 );
+
+}

Propchange: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DependencyManagementImporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/management/DependencyManagementImporter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message