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.artifact.repository.ArtifactRepository;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.building.AbstractModelBuildingListener;
27  import org.apache.maven.model.building.ModelBuildingEvent;
28  import org.apache.maven.model.building.ModelProblem.Severity;
29  import org.apache.maven.model.building.ModelProblem.Version;
30  import org.apache.maven.model.building.ModelProblemCollectorRequest;
31  import org.apache.maven.plugin.PluginManagerException;
32  import org.apache.maven.plugin.PluginResolutionException;
33  import org.apache.maven.plugin.version.PluginVersionResolutionException;
34  
35  /**
36   * Processes events from the model builder while building the effective model for a {@link MavenProject} instance.
37   *
38   * @author Benjamin Bentmann
39   */
40  public class DefaultModelBuildingListener extends AbstractModelBuildingListener {
41  
42      private MavenProject project;
43  
44      private ProjectBuildingHelper projectBuildingHelper;
45  
46      private ProjectBuildingRequest projectBuildingRequest;
47  
48      private List<ArtifactRepository> remoteRepositories;
49  
50      private List<ArtifactRepository> pluginRepositories;
51  
52      public DefaultModelBuildingListener(
53              MavenProject project,
54              ProjectBuildingHelper projectBuildingHelper,
55              ProjectBuildingRequest projectBuildingRequest) {
56          this.project = Objects.requireNonNull(project, "project cannot be null");
57          this.projectBuildingHelper =
58                  Objects.requireNonNull(projectBuildingHelper, "projectBuildingHelper cannot be null");
59          this.projectBuildingRequest =
60                  Objects.requireNonNull(projectBuildingRequest, "projectBuildingRequest cannot be null");
61          this.remoteRepositories = projectBuildingRequest.getRemoteRepositories();
62          this.pluginRepositories = projectBuildingRequest.getPluginArtifactRepositories();
63      }
64  
65      /**
66       * Gets the project whose model is being built.
67       *
68       * @return The project, never {@code null}.
69       */
70      public MavenProject getProject() {
71          return project;
72      }
73  
74      @Override
75      public void buildExtensionsAssembled(ModelBuildingEvent event) {
76          Model model = event.getModel();
77  
78          try {
79              pluginRepositories = projectBuildingHelper.createArtifactRepositories(
80                      model.getPluginRepositories(), pluginRepositories, projectBuildingRequest);
81          } catch (Exception e) {
82              event.getProblems()
83                      .add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
84                              .setMessage("Invalid plugin repository: " + e.getMessage())
85                              .setException(e));
86          }
87          project.setPluginArtifactRepositories(pluginRepositories);
88  
89          if (event.getRequest().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.getProblems()
98                          .add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
99                                  .setMessage("Unresolvable build extension: " + e.getMessage())
100                                 .setException(e));
101             }
102 
103             projectBuildingHelper.selectProjectRealm(project);
104         }
105 
106         // build the regular repos after extensions are loaded to allow for custom layouts
107         try {
108             remoteRepositories = projectBuildingHelper.createArtifactRepositories(
109                     model.getRepositories(), remoteRepositories, projectBuildingRequest);
110         } catch (Exception e) {
111             event.getProblems()
112                     .add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
113                             .setMessage("Invalid artifact repository: " + e.getMessage())
114                             .setException(e));
115         }
116         project.setRemoteArtifactRepositories(remoteRepositories);
117     }
118 }