1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
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
67
68
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
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 }