3636import org .apache .maven .artifact .InvalidRepositoryException ;
3737import org .apache .maven .artifact .handler .ArtifactHandler ;
3838import org .apache .maven .artifact .handler .manager .ArtifactHandlerManager ;
39+ import org .apache .maven .artifact .metadata .ArtifactMetadata ;
3940import org .apache .maven .artifact .repository .ArtifactRepository ;
4041import org .apache .maven .artifact .repository .ArtifactRepositoryPolicy ;
4142import org .apache .maven .artifact .repository .Authentication ;
4243import org .apache .maven .artifact .repository .MavenArtifactRepository ;
4344import org .apache .maven .artifact .repository .layout .ArtifactRepositoryLayout ;
4445import org .apache .maven .artifact .repository .layout .ArtifactRepositoryLayout2 ;
45- import org .apache .maven .artifact .repository .layout .DefaultRepositoryLayout ;
4646import org .apache .maven .artifact .resolver .filter .ExclusionArtifactFilter ;
4747import org .apache .maven .artifact .versioning .InvalidVersionSpecificationException ;
4848import org .apache .maven .artifact .versioning .VersionRange ;
4949import org .apache .maven .execution .MavenExecutionRequest ;
5050import org .apache .maven .model .Dependency ;
5151import org .apache .maven .model .Plugin ;
52+ import org .apache .maven .model .Repository ;
5253import org .apache .maven .repository .Proxy ;
5354import org .apache .maven .repository .RepositorySystem ;
5455import org .apache .maven .settings .Mirror ;
6061import org .eclipse .aether .repository .AuthenticationSelector ;
6162import org .eclipse .aether .repository .ProxySelector ;
6263import 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" )
6972public 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}
0 commit comments