View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.util.List;
22  import java.util.Objects;
23  
24  import org.apache.maven.api.services.BuilderProblem;
25  import org.apache.maven.api.services.ModelProblem;
26  import org.apache.maven.api.services.model.ModelBuildingEvent;
27  import org.apache.maven.api.services.model.ModelBuildingListener;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.model.Model;
30  import org.apache.maven.plugin.PluginManagerException;
31  import org.apache.maven.plugin.PluginResolutionException;
32  import org.apache.maven.plugin.version.PluginVersionResolutionException;
33  
34  /**
35   * Processes events from the model builder while building the effective model for a {@link MavenProject} instance.
36   *
37   */
38  public class DefaultModelBuildingListener implements ModelBuildingListener {
39  
40      private final MavenProject project;
41  
42      private final ProjectBuildingHelper projectBuildingHelper;
43  
44      private final ProjectBuildingRequest projectBuildingRequest;
45  
46      private List<ArtifactRepository> remoteRepositories;
47  
48      private List<ArtifactRepository> pluginRepositories;
49  
50      public DefaultModelBuildingListener(
51              MavenProject project,
52              ProjectBuildingHelper projectBuildingHelper,
53              ProjectBuildingRequest projectBuildingRequest) {
54          this.project = Objects.requireNonNull(project, "project cannot be null");
55          this.projectBuildingHelper =
56                  Objects.requireNonNull(projectBuildingHelper, "projectBuildingHelper cannot be null");
57          this.projectBuildingRequest =
58                  Objects.requireNonNull(projectBuildingRequest, "projectBuildingRequest cannot be null");
59          this.remoteRepositories = projectBuildingRequest.getRemoteRepositories();
60          this.pluginRepositories = projectBuildingRequest.getPluginArtifactRepositories();
61      }
62  
63      /**
64       * Gets the project whose model is being built.
65       *
66       * @return The project, never {@code null}.
67       */
68      public MavenProject getProject() {
69          return project;
70      }
71  
72      @Override
73      public void buildExtensionsAssembled(ModelBuildingEvent event) {
74          Model model = new Model(event.model());
75  
76          try {
77              pluginRepositories = projectBuildingHelper.createArtifactRepositories(
78                      model.getPluginRepositories(), pluginRepositories, projectBuildingRequest);
79          } catch (Exception e) {
80              event.problems()
81                      .add(
82                              BuilderProblem.Severity.ERROR,
83                              ModelProblem.Version.BASE,
84                              "Invalid plugin repository: " + e.getMessage(),
85                              e);
86          }
87          project.setPluginArtifactRepositories(pluginRepositories);
88  
89          if (event.request().isProcessPlugins()) {
90              try {
91                  ProjectRealmCache.CacheRecord record =
92                          projectBuildingHelper.createProjectRealm(project, model, projectBuildingRequest);
93  
94                  project.setClassRealm(record.getRealm());
95                  project.setExtensionDependencyFilter(record.getExtensionArtifactFilter());
96              } catch (PluginResolutionException | PluginManagerException | PluginVersionResolutionException e) {
97                  event.problems()
98                          .add(
99                                  BuilderProblem.Severity.ERROR,
100                                 ModelProblem.Version.BASE,
101                                 "Unresolvable build extension: " + e.getMessage(),
102                                 e);
103             }
104 
105             projectBuildingHelper.selectProjectRealm(project);
106         }
107 
108         // build the regular repos after extensions are loaded to allow for custom layouts
109         try {
110             remoteRepositories = projectBuildingHelper.createArtifactRepositories(
111                     model.getRepositories(), remoteRepositories, projectBuildingRequest);
112         } catch (Exception e) {
113             event.problems()
114                     .add(
115                             BuilderProblem.Severity.ERROR,
116                             ModelProblem.Version.BASE,
117                             "Invalid artifact repository: " + e.getMessage(),
118                             e);
119         }
120         project.setRemoteArtifactRepositories(remoteRepositories);
121 
122         if (model.getDelegate() != event.model()) {
123             event.update().accept(model.getDelegate());
124         }
125     }
126 }