Skip to content

Commit 1d01e2d

Browse files
Repository layout should be used in MavenRepositorySystem
1 parent a51e117 commit 1d01e2d

File tree

8 files changed

+114
-20
lines changed

8 files changed

+114
-20
lines changed

maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,20 @@
3636
import org.apache.maven.artifact.InvalidRepositoryException;
3737
import org.apache.maven.artifact.handler.ArtifactHandler;
3838
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
39+
import org.apache.maven.artifact.metadata.ArtifactMetadata;
3940
import org.apache.maven.artifact.repository.ArtifactRepository;
4041
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
4142
import org.apache.maven.artifact.repository.Authentication;
4243
import org.apache.maven.artifact.repository.MavenArtifactRepository;
4344
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
4445
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout2;
45-
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
4646
import org.apache.maven.artifact.resolver.filter.ExclusionArtifactFilter;
4747
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
4848
import org.apache.maven.artifact.versioning.VersionRange;
4949
import org.apache.maven.execution.MavenExecutionRequest;
5050
import org.apache.maven.model.Dependency;
5151
import org.apache.maven.model.Plugin;
52+
import org.apache.maven.model.Repository;
5253
import org.apache.maven.repository.Proxy;
5354
import org.apache.maven.repository.RepositorySystem;
5455
import org.apache.maven.settings.Mirror;
@@ -60,19 +61,31 @@
6061
import org.eclipse.aether.repository.AuthenticationSelector;
6162
import org.eclipse.aether.repository.ProxySelector;
6263
import org.eclipse.aether.repository.RemoteRepository;
64+
import org.slf4j.Logger;
65+
import org.slf4j.LoggerFactory;
6366

6467
/**
6568
* @author Jason van Zyl
6669
* @since 3.2.3
6770
*/
6871
@Component(role = MavenRepositorySystem.class, hint = "default")
6972
public class MavenRepositorySystem {
73+
74+
// Singleton instance for static deprecated methods
75+
private static MavenRepositorySystem instance;
76+
77+
private static final Logger LOGGER = LoggerFactory.getLogger(MavenRepositorySystem.class);
78+
7079
@Requirement
7180
private ArtifactHandlerManager artifactHandlerManager;
7281

7382
@Requirement(role = ArtifactRepositoryLayout.class)
7483
private Map<String, ArtifactRepositoryLayout> layouts;
7584

85+
MavenRepositorySystem() {
86+
instance = this;
87+
}
88+
7689
// DefaultProjectBuilder
7790
public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
7891
return createArtifactX(groupId, artifactId, version, scope, type);
@@ -200,7 +213,7 @@ private void injectMirror(ArtifactRepository repository, Mirror mirror) {
200213
repository.setUrl(mirror.getUrl());
201214

202215
if (StringUtils.isNotEmpty(mirror.getLayout())) {
203-
repository.setLayout(getLayout(mirror.getLayout()));
216+
repository.setLayout(getLayout(mirror.getId(), mirror.getLayout()));
204217
}
205218

206219
repository.setBlocked(mirror.isBlocked());
@@ -275,9 +288,14 @@ public void injectProxy(RepositorySystemSession session, List<ArtifactRepository
275288
}
276289
}
277290

278-
private ArtifactRepositoryLayout getLayout(String id) {
291+
private ArtifactRepositoryLayout getLayout(String repoId, String id) {
279292
ArtifactRepositoryLayout layout = layouts.get(id);
280293

294+
if (layout == null) {
295+
LOGGER.debug("No layout '{}' found for repository id '{}'", id, repoId);
296+
layout = new UnknownRepositoryLayout(id, layouts.get("default"));
297+
}
298+
281299
return layout;
282300
}
283301

@@ -308,13 +326,37 @@ public static org.apache.maven.model.RepositoryPolicy fromSettingsRepositoryPoli
308326
return modelRepositoryPolicy;
309327
}
310328

329+
/**
330+
* @deprecated use a service metod {@link #buildArtifactRepositoryFromRepo(org.apache.maven.settings.Repository) instead
331+
*/
332+
@Deprecated
311333
public static ArtifactRepository buildArtifactRepository(org.apache.maven.settings.Repository repo)
312334
throws InvalidRepositoryException {
313-
return buildArtifactRepository(fromSettingsRepository(repo));
335+
return instance.buildArtifactRepositoryFromRepo(repo);
314336
}
315337

338+
/**
339+
* @since 3.9.12
340+
*/
341+
public ArtifactRepository buildArtifactRepositoryFromRepo(org.apache.maven.settings.Repository repo)
342+
throws InvalidRepositoryException {
343+
return buildArtifactRepositoryFromRepo(fromSettingsRepository(repo));
344+
}
345+
346+
/**
347+
* @deprecated use a service metod {@link #buildArtifactRepositoryFromRepo(Repository)} instead
348+
*/
349+
@Deprecated
316350
public static ArtifactRepository buildArtifactRepository(org.apache.maven.model.Repository repo)
317351
throws InvalidRepositoryException {
352+
return instance.buildArtifactRepositoryFromRepo(repo);
353+
}
354+
355+
/**
356+
* @since 3.9.12
357+
*/
358+
public ArtifactRepository buildArtifactRepositoryFromRepo(org.apache.maven.model.Repository repo)
359+
throws InvalidRepositoryException {
318360
if (repo != null) {
319361
String id = repo.getId();
320362

@@ -332,7 +374,7 @@ public static ArtifactRepository buildArtifactRepository(org.apache.maven.model.
332374

333375
ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy(repo.getReleases());
334376

335-
ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
377+
ArtifactRepositoryLayout layout = getLayout(repo.getId(), repo.getLayout());
336378

337379
return createArtifactRepository(id, url, layout, snapshots, releases);
338380
} else {
@@ -816,4 +858,43 @@ else if (layout.equals(repoLayout)) {
816858

817859
return result;
818860
}
861+
862+
/**
863+
* In the future, the legacy system might encounter repository types for which no layout components exists because
864+
* the actual communication with the repository happens via a repository connector. As a minimum, the legacy system
865+
* needs to retain the id of this layout so that the content type of the remote repository can still be accurately
866+
* described.
867+
*/
868+
static class UnknownRepositoryLayout implements ArtifactRepositoryLayout {
869+
870+
private final String id;
871+
872+
private final ArtifactRepositoryLayout fallback;
873+
874+
UnknownRepositoryLayout(String id, ArtifactRepositoryLayout fallback) {
875+
this.id = id;
876+
this.fallback = fallback;
877+
}
878+
879+
public String getId() {
880+
return id;
881+
}
882+
883+
public String pathOf(Artifact artifact) {
884+
return fallback.pathOf(artifact);
885+
}
886+
887+
public String pathOfLocalRepositoryMetadata(ArtifactMetadata metadata, ArtifactRepository repository) {
888+
return fallback.pathOfLocalRepositoryMetadata(metadata, repository);
889+
}
890+
891+
public String pathOfRemoteRepositoryMetadata(ArtifactMetadata metadata) {
892+
return fallback.pathOfRemoteRepositoryMetadata(metadata);
893+
}
894+
895+
@Override
896+
public String toString() {
897+
return getId();
898+
}
899+
}
819900
}

maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequestPopulator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public MavenExecutionRequest populateFromSettings(MavenExecutionRequest request,
241241
List<Repository> remoteRepositories = rawProfile.getRepositories();
242242
for (Repository remoteRepository : remoteRepositories) {
243243
try {
244-
request.addRemoteRepository(repositorySystem.buildArtifactRepository(remoteRepository));
244+
request.addRemoteRepository(repositorySystem.buildArtifactRepositoryFromRepo(remoteRepository));
245245
} catch (InvalidRepositoryException e) {
246246
// do nothing for now
247247
}
@@ -250,7 +250,8 @@ public MavenExecutionRequest populateFromSettings(MavenExecutionRequest request,
250250
List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
251251
for (Repository pluginRepo : pluginRepositories) {
252252
try {
253-
request.addPluginArtifactRepository(repositorySystem.buildArtifactRepository(pluginRepo));
253+
request.addPluginArtifactRepository(
254+
repositorySystem.buildArtifactRepositoryFromRepo(pluginRepo));
254255
} catch (InvalidRepositoryException e) {
255256
// do nothing for now
256257
}

maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ HashMap<String, Artifact> compute() {
831831
try {
832832
DeploymentRepository r = project.getDistributionManagement().getRepository();
833833
if (!StringUtils.isEmpty(r.getId()) && !StringUtils.isEmpty(r.getUrl())) {
834-
ArtifactRepository repo = repositorySystem.buildArtifactRepository(r);
834+
ArtifactRepository repo = repositorySystem.buildArtifactRepositoryFromRepo(r);
835835
repositorySystem.injectProxy(projectBuildingRequest.getRepositorySession(), Arrays.asList(repo));
836836
repositorySystem.injectAuthentication(
837837
projectBuildingRequest.getRepositorySession(), Arrays.asList(repo));
@@ -849,7 +849,7 @@ HashMap<String, Artifact> compute() {
849849
try {
850850
DeploymentRepository r = project.getDistributionManagement().getSnapshotRepository();
851851
if (!StringUtils.isEmpty(r.getId()) && !StringUtils.isEmpty(r.getUrl())) {
852-
ArtifactRepository repo = repositorySystem.buildArtifactRepository(r);
852+
ArtifactRepository repo = repositorySystem.buildArtifactRepositoryFromRepo(r);
853853
repositorySystem.injectProxy(projectBuildingRequest.getRepositorySession(), Arrays.asList(repo));
854854
repositorySystem.injectAuthentication(
855855
projectBuildingRequest.getRepositorySession(), Arrays.asList(repo));

maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public List<ArtifactRepository> createArtifactRepositories(
8888
List<ArtifactRepository> internalRepositories = new ArrayList<>();
8989

9090
for (Repository repository : pomRepositories) {
91-
internalRepositories.add(repositorySystem.buildArtifactRepository(repository));
91+
internalRepositories.add(repositorySystem.buildArtifactRepositoryFromRepo(repository));
9292
}
9393

9494
repositorySystem.injectMirror(request.getRepositorySession(), internalRepositories);

maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private List<ArtifactRepository> getRepositoriesFromModel(RepositorySystemSessio
288288
List<ArtifactRepository> pomRepositories = new ArrayList<>();
289289
for (Repository modelRepository : model.getRepositories()) {
290290
try {
291-
pomRepositories.add(MavenRepositorySystem.buildArtifactRepository(modelRepository));
291+
pomRepositories.add(repositorySystem.buildArtifactRepositoryFromRepo(modelRepository));
292292
} catch (InvalidRepositoryException e) {
293293
// can not use this then
294294
}

maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ protected List<ArtifactRepository> getRemoteRepositories() throws InvalidReposit
192192
repository.setReleases(policy);
193193
repository.setSnapshots(policy);
194194

195-
return Arrays.asList(repositorySystem.buildArtifactRepository(repository));
195+
return Arrays.asList(repositorySystem.buildArtifactRepositoryFromRepo(repository));
196196
}
197197

198198
protected List<ArtifactRepository> getPluginArtifactRepositories() throws InvalidRepositoryException {

maven-core/src/test/java/org/apache/maven/execution/DefaultMavenExecutionRequestPopulatorTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@
1818
*/
1919
package org.apache.maven.execution;
2020

21-
import javax.inject.Inject;
22-
2321
import java.util.List;
2422

2523
import org.apache.maven.artifact.repository.ArtifactRepository;
2624
import org.apache.maven.settings.Profile;
2725
import org.apache.maven.settings.Repository;
2826
import org.apache.maven.settings.Settings;
29-
import org.eclipse.sisu.launch.InjectedTestCase;
27+
import org.codehaus.plexus.ContainerConfiguration;
28+
import org.codehaus.plexus.PlexusConstants;
29+
import org.codehaus.plexus.PlexusTestCase;
30+
31+
public class DefaultMavenExecutionRequestPopulatorTest extends PlexusTestCase {
3032

31-
public class DefaultMavenExecutionRequestPopulatorTest extends InjectedTestCase {
32-
@Inject
33-
MavenExecutionRequestPopulator testee;
33+
@Override
34+
protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
35+
super.customizeContainerConfiguration(configuration);
36+
configuration.setAutoWiring(true);
37+
configuration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
38+
}
3439

3540
public void testPluginRepositoryInjection() throws Exception {
41+
42+
MavenExecutionRequestPopulator testee = lookup(MavenExecutionRequestPopulator.class);
43+
3644
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
3745

3846
Repository r = new Repository();

maven-embedder/src/main/java/org/apache/maven/cli/configuration/SettingsXmlConfigurationProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public class SettingsXmlConfigurationProcessor implements ConfigurationProcessor
7575
@Inject
7676
private SettingsDecrypter settingsDecrypter;
7777

78+
@Inject
79+
private MavenRepositorySystem mavenRepositorySystem;
80+
7881
@Override
7982
public void process(CliRequest cliRequest) throws Exception {
8083
CommandLine commandLine = cliRequest.getCommandLine();
@@ -212,7 +215,8 @@ private MavenExecutionRequest populateFromSettings(MavenExecutionRequest request
212215
List<Repository> remoteRepositories = rawProfile.getRepositories();
213216
for (Repository remoteRepository : remoteRepositories) {
214217
try {
215-
request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
218+
request.addRemoteRepository(
219+
mavenRepositorySystem.buildArtifactRepositoryFromRepo(remoteRepository));
216220
} catch (InvalidRepositoryException e) {
217221
// do nothing for now
218222
}
@@ -222,7 +226,7 @@ private MavenExecutionRequest populateFromSettings(MavenExecutionRequest request
222226
for (Repository pluginRepository : pluginRepositories) {
223227
try {
224228
request.addPluginArtifactRepository(
225-
MavenRepositorySystem.buildArtifactRepository(pluginRepository));
229+
mavenRepositorySystem.buildArtifactRepositoryFromRepo(pluginRepository));
226230
} catch (InvalidRepositoryException e) {
227231
// do nothing for now
228232
}

0 commit comments

Comments
 (0)