You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2016/09/24 15:13:34 UTC

[13/40] maven git commit: [MNG-6073] Addition of a core extension point to the model builer supporting model finalization. [MNG-3825] Dependencies with classifier should not always require a version.

[MNG-6073] Addition of a core extension point to the model builer supporting model finalization.
[MNG-3825] Dependencies with classifier should not always require a version.


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/17f68388
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/17f68388
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/17f68388

Branch: refs/heads/MNG-6012-Missing-Profile-At-End
Commit: 17f6838881b24df0eeb88f6b83a20065c51c48b5
Parents: 5307750
Author: Christian Schulte <sc...@apache.org>
Authored: Sat Jul 30 20:10:16 2016 +0200
Committer: Christian Schulte <sc...@apache.org>
Committed: Sat Jul 30 20:10:16 2016 +0200

----------------------------------------------------------------------
 .../model/building/DefaultModelBuilder.java     |  18 +++
 .../DependencyManagementModelFinalizer.java     | 159 +++++++++++++++++++
 .../model/finalization/ModelFinalizer.java      |  44 +++++
 maven-model-builder/src/site/apt/index.apt      |   2 +
 4 files changed, 223 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/17f68388/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
index 0224367..415808b 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
@@ -51,6 +51,7 @@ import org.apache.maven.model.Repository;
 import org.apache.maven.model.building.ModelProblem.Severity;
 import org.apache.maven.model.building.ModelProblem.Version;
 import org.apache.maven.model.composition.DependencyManagementImporter;
+import org.apache.maven.model.finalization.ModelFinalizer;
 import org.apache.maven.model.inheritance.InheritanceAssembler;
 import org.apache.maven.model.interpolation.ModelInterpolator;
 import org.apache.maven.model.io.ModelParseException;
@@ -139,6 +140,9 @@ public class DefaultModelBuilder
     @Requirement
     private ReportingConverter reportingConverter;
 
+    @Requirement( optional = true )
+    private List<ModelFinalizer> modelFinalizers;
+
     public DefaultModelBuilder setModelProcessor( ModelProcessor modelProcessor )
     {
         this.modelProcessor = modelProcessor;
@@ -241,6 +245,12 @@ public class DefaultModelBuilder
         return this;
     }
 
+    public DefaultModelBuilder setModelFinalizers( List<ModelFinalizer> value )
+    {
+        this.modelFinalizers = value;
+        return this;
+    }
+
     @Override
     public ModelBuildingResult build( ModelBuildingRequest request )
         throws ModelBuildingException
@@ -502,6 +512,14 @@ public class DefaultModelBuilder
             pluginConfigurationExpander.expandPluginConfiguration( resultModel, request, problems );
         }
 
+        if ( this.modelFinalizers != null )
+        {
+            for ( final ModelFinalizer modelFinalizer : this.modelFinalizers )
+            {
+                modelFinalizer.finalizeModel( resultModel, request, problems );
+            }
+        }
+
         // effective model validation
         modelValidator.validateEffectiveModel( resultModel, request, problems );
 

http://git-wip-us.apache.org/repos/asf/maven/blob/17f68388/maven-model-builder/src/main/java/org/apache/maven/model/finalization/DependencyManagementModelFinalizer.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/finalization/DependencyManagementModelFinalizer.java b/maven-model-builder/src/main/java/org/apache/maven/model/finalization/DependencyManagementModelFinalizer.java
new file mode 100644
index 0000000..c69a174
--- /dev/null
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/finalization/DependencyManagementModelFinalizer.java
@@ -0,0 +1,159 @@
+package org.apache.maven.model.finalization;
+
+/*
+ * 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.HashSet;
+import java.util.Set;
+
+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.apache.maven.model.building.ModelProblemCollector;
+import org.apache.maven.model.management.DependencyManagementInjector;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+
+/**
+ * A {@code ModelFinalizer} adding classified dependency declarations to the dependency management for all unclassified
+ * dependency declarations with classifiers taken from set of well-known classifiers.
+ * <p>
+ * This class requires the {@code Set} implementation of well-known classifiers to be provided by the runtime
+ * environment. By default, no such implementation exists. As a starting point, that {@code Set} implementation should
+ * at least contain the following classifiers corresponding to plugin defaults.
+ * <table>
+ * <tr>
+ * <th>Plugin</th>
+ * <th>Classifiers</th>
+ * </tr>
+ * <tr>
+ * <td>maven-jar-plugin</td>
+ * <td>tests</td>
+ * </tr>
+ * <tr>
+ * <td>maven-javadoc-plugin</td>
+ * <td>javadoc, test-javadoc</td>
+ * </tr>
+ * <tr>
+ * <td>maven-shade-plugin</td>
+ * <td>shaded</td>
+ * </tr>
+ * <tr>
+ * <td>maven-site-plugin</td>
+ * <td>site</td>
+ * </tr>
+ * <tr>
+ * <td>maven-source-plugin</td>
+ * <td>sources, test-sources</td>
+ * </tr>
+ * </table>
+ * </p>
+ *
+ * @author Christian Schulte
+ * @since 3.4
+ */
+@Component( role = ModelFinalizer.class, hint = "dependency-management" )
+public class DependencyManagementModelFinalizer
+    implements ModelFinalizer
+{
+
+    @Requirement( role = Set.class, hint = "dependency-management-classifiers", optional = true )
+    private Set<String> dependencyManagementClassifiers;
+
+    @Requirement
+    private DependencyManagementInjector dependencyManagementInjector;
+
+    public DependencyManagementModelFinalizer setDependencyManagementInjector( DependencyManagementInjector value )
+    {
+        this.dependencyManagementInjector = value;
+        return this;
+    }
+
+    public DependencyManagementModelFinalizer setDependencyManagementClassifiers( Set<String> value )
+    {
+        this.dependencyManagementClassifiers = value;
+        return this;
+    }
+
+    /**
+     * Adds classified dependency declarations for all unclassified dependency declarations to the dependency management
+     * of the given {@code Model} with classifiers taken from set of well-known classifiers.
+     *
+     * @param model The {@code Model} to add classified dependency declarations to, must not be {@code null}.
+     * @param request The model building request that holds further settings, must not be {@code null}.
+     * @param problems The container used to collect problems that were encountered, must not be {@code null}.
+     */
+    @Override
+    public void finalizeModel( final Model model, final ModelBuildingRequest request,
+                               final ModelProblemCollector problems )
+    {
+        if ( this.dependencyManagementClassifiers != null && model.getDependencyManagement() != null )
+        {
+            final Set<Dependency> classifiedDependencies = new HashSet<>();
+
+            for ( final Dependency managedDependency : model.getDependencyManagement().getDependencies() )
+            {
+                for ( final String classifier : this.dependencyManagementClassifiers )
+                {
+                    Dependency classifiedDependency =
+                        getDependency( model.getDependencyManagement(), managedDependency.getGroupId(),
+                                       managedDependency.getArtifactId(), managedDependency.getType(),
+                                       managedDependency.getClassifier() );
+
+                    if ( classifiedDependency == null )
+                    {
+                        classifiedDependency = managedDependency.clone();
+                        classifiedDependencies.add( classifiedDependency );
+
+                        classifiedDependency.setClassifier( classifier );
+                    }
+                }
+            }
+
+            if ( !classifiedDependencies.isEmpty() )
+            {
+                model.getDependencyManagement().getDependencies().addAll( classifiedDependencies );
+                this.dependencyManagementInjector.injectManagement( model, request, problems );
+            }
+        }
+    }
+
+    private static Dependency getDependency( final DependencyManagement dependencyManagement,
+                                             final String groupId, final String artifactId, final String type,
+                                             final String classifier )
+    {
+        Dependency dependency = null;
+
+        for ( final Dependency candidate : dependencyManagement.getDependencies() )
+        {
+            if ( groupId.equals( candidate.getGroupId() )
+                     && artifactId.equals( candidate.getArtifactId() )
+                     && type.equals( candidate.getType() )
+                     && classifier.equals( candidate.getClassifier() ) )
+            {
+                dependency = candidate;
+                break;
+            }
+        }
+
+        return dependency;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/17f68388/maven-model-builder/src/main/java/org/apache/maven/model/finalization/ModelFinalizer.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/finalization/ModelFinalizer.java b/maven-model-builder/src/main/java/org/apache/maven/model/finalization/ModelFinalizer.java
new file mode 100644
index 0000000..5f3da0f
--- /dev/null
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/finalization/ModelFinalizer.java
@@ -0,0 +1,44 @@
+package org.apache.maven.model.finalization;
+
+/*
+ * 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;
+import org.apache.maven.model.building.ModelBuildingRequest;
+import org.apache.maven.model.building.ModelProblemCollector;
+
+/**
+ * Interface to model finalization.
+ *
+ * @author Christian Schulte
+ * @since 3.4
+ */
+public interface ModelFinalizer
+{
+
+    /**
+     * Finalizes a model.
+     *
+     * @param model The model to finalize, must not be {@code null}.
+     * @param request The model building request that holds further settings, must not be {@code null}.
+     * @param problems The container used to collect problems that were encountered, must not be {@code null}.
+     */
+    void finalizeModel( Model model, ModelBuildingRequest request, ModelProblemCollector problems );
+
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/17f68388/maven-model-builder/src/site/apt/index.apt
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/site/apt/index.apt b/maven-model-builder/src/site/apt/index.apt
index 0ff6f81..f2bed91 100644
--- a/maven-model-builder/src/site/apt/index.apt
+++ b/maven-model-builder/src/site/apt/index.apt
@@ -108,6 +108,8 @@ Maven Model Builder
    with its <<<DefaultPluginConfigurationExpander>>> implementation
    ({{{./xref/org/apache/maven/model/plugin/DefaultPluginConfigurationExpander.html}source}})
 
+   ** <(optional)> model finalization: <<<ModelFinalizer>>> ({{{./apidocs/org/apache/maven/model/finalization/ModelFinalizer.html}javadoc}})
+
    ** effective model validation: <<<ModelValidator>>> ({{{./apidocs/org/apache/maven/model/validation/ModelValidator.html}javadoc}}),
    with its <<<DefaultModelValidator>>> implementation
    ({{{./xref/org/apache/maven/model/validation/DefaultModelValidator.html}source}})