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.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
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
65
66
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
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 }