1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.report.projectinfo.dependencies;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.ArtifactUtils;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
28 import org.apache.maven.plugin.logging.Log;
29 import org.apache.maven.project.DefaultProjectBuildingRequest;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.project.ProjectBuilder;
32 import org.apache.maven.project.ProjectBuildingException;
33 import org.apache.maven.project.ProjectBuildingRequest;
34 import org.apache.maven.repository.RepositorySystem;
35 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
36 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
37 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
38
39
40
41
42
43
44
45 public class RepositoryUtils {
46 private final Log log;
47
48 private final ProjectBuilder projectBuilder;
49
50 private final RepositorySystem repositorySystem;
51
52 private final List<ArtifactRepository> remoteRepositories;
53
54 private final List<ArtifactRepository> pluginRepositories;
55
56 private final ArtifactResolver resolver;
57
58 private final ProjectBuildingRequest buildingRequest;
59
60
61
62
63
64
65
66
67
68
69
70 public RepositoryUtils(
71 Log log,
72 ProjectBuilder projectBuilder,
73 RepositorySystem repositorySystem,
74 ArtifactResolver resolver,
75 List<ArtifactRepository> remoteRepositories,
76 List<ArtifactRepository> pluginRepositories,
77 ProjectBuildingRequest buildingRequest,
78 RepositoryMetadataManager repositoryMetadataManager) {
79 this.log = log;
80 this.projectBuilder = projectBuilder;
81 this.repositorySystem = repositorySystem;
82 this.resolver = resolver;
83 this.remoteRepositories = remoteRepositories;
84 this.pluginRepositories = pluginRepositories;
85 this.buildingRequest = buildingRequest;
86 }
87
88
89
90
91
92 public void resolve(Artifact artifact) throws ArtifactResolverException {
93 List<ArtifactRepository> repos = new ArrayList<>(pluginRepositories.size() + remoteRepositories.size());
94 repos.addAll(pluginRepositories);
95 repos.addAll(remoteRepositories);
96
97 ProjectBuildingRequest buildRequest = new DefaultProjectBuildingRequest(buildingRequest);
98 buildRequest.setRemoteRepositories(repos);
99
100 ArtifactResult result = resolver.resolveArtifact(buildRequest, artifact);
101 artifact.setFile(result.getArtifact().getFile());
102 }
103
104
105
106
107
108
109
110
111 public MavenProject getMavenProjectFromRepository(Artifact artifact) throws ProjectBuildingException {
112 Artifact projectArtifact = artifact;
113
114 boolean allowStubModel = false;
115 if (!"pom".equals(artifact.getType())) {
116 projectArtifact = repositorySystem.createProjectArtifact(
117 artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
118 allowStubModel = true;
119 }
120
121 return projectBuilder
122 .build(projectArtifact, allowStubModel, buildingRequest)
123 .getProject();
124 }
125
126
127
128
129
130
131
132 public String getDependencyUrlFromRepository(Artifact artifact, ArtifactRepository repo) {
133 if (repo.isBlacklisted()) {
134 return null;
135 }
136
137 Artifact copyArtifact = ArtifactUtils.copyArtifact(artifact);
138
139 if ((artifact.isSnapshot() && repo.getSnapshots().isEnabled())) {
140 if (artifact.getBaseVersion().equals(artifact.getVersion())) {
141
142 if (artifact.getMetadataList() == null
143 || artifact.getMetadataList().isEmpty()) {
144 try {
145 resolve(artifact);
146 } catch (ArtifactResolverException e) {
147 log.error("Artifact: " + artifact.getId() + " could not be resolved.");
148 }
149 }
150 }
151 }
152
153 return repo.getUrl() + "/" + repo.pathOf(copyArtifact);
154 }
155 }