View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.bridge;
20  
21  import java.io.File;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashSet;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import org.apache.maven.RepositoryUtils;
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.DefaultArtifact;
36  import org.apache.maven.artifact.InvalidRepositoryException;
37  import org.apache.maven.artifact.handler.ArtifactHandler;
38  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
39  import org.apache.maven.artifact.metadata.ArtifactMetadata;
40  import org.apache.maven.artifact.repository.ArtifactRepository;
41  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
42  import org.apache.maven.artifact.repository.Authentication;
43  import org.apache.maven.artifact.repository.MavenArtifactRepository;
44  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
45  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout2;
46  import org.apache.maven.artifact.resolver.filter.ExclusionArtifactFilter;
47  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
48  import org.apache.maven.artifact.versioning.VersionRange;
49  import org.apache.maven.execution.MavenExecutionRequest;
50  import org.apache.maven.model.Dependency;
51  import org.apache.maven.model.Plugin;
52  import org.apache.maven.model.Repository;
53  import org.apache.maven.repository.Proxy;
54  import org.apache.maven.repository.RepositorySystem;
55  import org.apache.maven.settings.Mirror;
56  import org.codehaus.plexus.component.annotations.Component;
57  import org.codehaus.plexus.component.annotations.Requirement;
58  import org.codehaus.plexus.util.StringUtils;
59  import org.eclipse.aether.RepositorySystemSession;
60  import org.eclipse.aether.repository.AuthenticationContext;
61  import org.eclipse.aether.repository.AuthenticationSelector;
62  import org.eclipse.aether.repository.ProxySelector;
63  import org.eclipse.aether.repository.RemoteRepository;
64  import org.slf4j.Logger;
65  import org.slf4j.LoggerFactory;
66  
67  /**
68   * @author Jason van Zyl
69   * @since 3.2.3
70   */
71  @Component(role = MavenRepositorySystem.class, hint = "default")
72  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  
79      @Requirement
80      private ArtifactHandlerManager artifactHandlerManager;
81  
82      @Requirement(role = ArtifactRepositoryLayout.class)
83      private Map<String, ArtifactRepositoryLayout> layouts;
84  
85      MavenRepositorySystem() {
86          instance = this;
87      }
88  
89      // DefaultProjectBuilder
90      public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
91          return createArtifactX(groupId, artifactId, version, scope, type);
92      }
93  
94      // DefaultProjectBuilder
95      public Artifact createProjectArtifact(String groupId, String artifactId, String metaVersionId) {
96          return createProjectArtifactX(groupId, artifactId, metaVersionId);
97      }
98  
99      // DefaultProjectBuilder
100     public Artifact createDependencyArtifact(Dependency d) {
101         if (d.getVersion() == null) {
102             return null;
103         }
104 
105         VersionRange versionRange;
106         try {
107             versionRange = VersionRange.createFromVersionSpec(d.getVersion());
108         } catch (InvalidVersionSpecificationException e) {
109             return null;
110         }
111 
112         Artifact artifact = createDependencyArtifactX(
113                 d.getGroupId(),
114                 d.getArtifactId(),
115                 versionRange,
116                 d.getType(),
117                 d.getClassifier(),
118                 d.getScope(),
119                 d.isOptional());
120 
121         if (Artifact.SCOPE_SYSTEM.equals(d.getScope()) && d.getSystemPath() != null) {
122             artifact.setFile(new File(d.getSystemPath()));
123         }
124 
125         if (!d.getExclusions().isEmpty()) {
126             artifact.setDependencyFilter(new ExclusionArtifactFilter(d.getExclusions()));
127         }
128 
129         return artifact;
130     }
131 
132     // DefaultProjectBuilder
133     public Artifact createExtensionArtifact(String groupId, String artifactId, String version) {
134         VersionRange versionRange;
135         try {
136             versionRange = VersionRange.createFromVersionSpec(version);
137         } catch (InvalidVersionSpecificationException e) {
138             return null;
139         }
140 
141         return createExtensionArtifactX(groupId, artifactId, versionRange);
142     }
143 
144     // DefaultProjectBuilder
145     public Artifact createParentArtifact(String groupId, String artifactId, String version) {
146         return createParentArtifactX(groupId, artifactId, version);
147     }
148 
149     // DefaultProjectBuilder
150     public Artifact createPluginArtifact(Plugin plugin) {
151         VersionRange versionRange;
152         try {
153             String version = plugin.getVersion();
154             if (StringUtils.isEmpty(version)) {
155                 version = "RELEASE";
156             }
157             versionRange = VersionRange.createFromVersionSpec(version);
158         } catch (InvalidVersionSpecificationException e) {
159             return null;
160         }
161 
162         return createPluginArtifactX(plugin.getGroupId(), plugin.getArtifactId(), versionRange);
163     }
164 
165     public void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors) {
166         if (repositories != null && mirrors != null) {
167             for (ArtifactRepository repository : repositories) {
168                 Mirror mirror = getMirror(repository, mirrors);
169                 injectMirror(repository, mirror);
170             }
171         }
172     }
173 
174     private Mirror getMirror(RepositorySystemSession session, ArtifactRepository repository) {
175         if (session != null) {
176             org.eclipse.aether.repository.MirrorSelector selector = session.getMirrorSelector();
177             if (selector != null) {
178                 RemoteRepository repo = selector.getMirror(RepositoryUtils.toRepo(repository));
179                 if (repo != null) {
180                     Mirror mirror = new Mirror();
181                     mirror.setId(repo.getId());
182                     mirror.setUrl(repo.getUrl());
183                     mirror.setLayout(repo.getContentType());
184                     mirror.setBlocked(repo.isBlocked());
185                     return mirror;
186                 }
187             }
188         }
189         return null;
190     }
191 
192     public void injectMirror(RepositorySystemSession session, List<ArtifactRepository> repositories) {
193         if (repositories != null && session != null) {
194             for (ArtifactRepository repository : repositories) {
195                 Mirror mirror = getMirror(session, repository);
196                 injectMirror(repository, mirror);
197             }
198         }
199     }
200 
201     private void injectMirror(ArtifactRepository repository, Mirror mirror) {
202         if (mirror != null) {
203             ArtifactRepository original = createArtifactRepository(
204                     repository.getId(),
205                     repository.getUrl(),
206                     repository.getLayout(),
207                     repository.getSnapshots(),
208                     repository.getReleases());
209 
210             repository.setMirroredRepositories(Collections.singletonList(original));
211 
212             repository.setId(mirror.getId());
213             repository.setUrl(mirror.getUrl());
214 
215             if (StringUtils.isNotEmpty(mirror.getLayout())) {
216                 repository.setLayout(getLayout(mirror.getId(), mirror.getLayout()));
217             }
218 
219             repository.setBlocked(mirror.isBlocked());
220         }
221     }
222 
223     private Authentication getAuthentication(RepositorySystemSession session, ArtifactRepository repository) {
224         if (session != null) {
225             AuthenticationSelector selector = session.getAuthenticationSelector();
226             if (selector != null) {
227                 RemoteRepository repo = RepositoryUtils.toRepo(repository);
228                 org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo);
229                 if (auth != null) {
230                     repo = new RemoteRepository.Builder(repo)
231                             .setAuthentication(auth)
232                             .build();
233                     AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
234                     Authentication result = new Authentication(
235                             authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
236                     result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
237                     result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
238                     authCtx.close();
239                     return result;
240                 }
241             }
242         }
243         return null;
244     }
245 
246     public void injectAuthentication(RepositorySystemSession session, List<ArtifactRepository> repositories) {
247         if (repositories != null && session != null) {
248             for (ArtifactRepository repository : repositories) {
249                 repository.setAuthentication(getAuthentication(session, repository));
250             }
251         }
252     }
253 
254     private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repository) {
255         if (session != null) {
256             ProxySelector selector = session.getProxySelector();
257             if (selector != null) {
258                 RemoteRepository repo = RepositoryUtils.toRepo(repository);
259                 org.eclipse.aether.repository.Proxy proxy = selector.getProxy(repo);
260                 if (proxy != null) {
261                     Proxy p = new Proxy();
262                     p.setHost(proxy.getHost());
263                     p.setProtocol(proxy.getType());
264                     p.setPort(proxy.getPort());
265                     if (proxy.getAuthentication() != null) {
266                         repo = new RemoteRepository.Builder(repo)
267                                 .setProxy(proxy)
268                                 .build();
269                         AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
270                         p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
271                         p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
272                         p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
273                         p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
274                         authCtx.close();
275                     }
276                     return p;
277                 }
278             }
279         }
280         return null;
281     }
282 
283     public void injectProxy(RepositorySystemSession session, List<ArtifactRepository> repositories) {
284         if (repositories != null && session != null) {
285             for (ArtifactRepository repository : repositories) {
286                 repository.setProxy(getProxy(session, repository));
287             }
288         }
289     }
290 
291     private ArtifactRepositoryLayout getLayout(String repoId, String layoutId) {
292         ArtifactRepositoryLayout layout = layouts.get(layoutId);
293 
294         if (layout == null) {
295             LOGGER.debug("No layout '{}' found for repository id '{}'", layoutId, repoId);
296             layout = new UnknownRepositoryLayout(layoutId, layouts.get("default"));
297         }
298 
299         return layout;
300     }
301 
302     //
303     // Taken from LegacyRepositorySystem
304     //
305 
306     public static org.apache.maven.model.Repository fromSettingsRepository(
307             org.apache.maven.settings.Repository settingsRepository) {
308         org.apache.maven.model.Repository modelRepository = new org.apache.maven.model.Repository();
309         modelRepository.setId(settingsRepository.getId());
310         modelRepository.setLayout(settingsRepository.getLayout());
311         modelRepository.setName(settingsRepository.getName());
312         modelRepository.setUrl(settingsRepository.getUrl());
313         modelRepository.setReleases(fromSettingsRepositoryPolicy(settingsRepository.getReleases()));
314         modelRepository.setSnapshots(fromSettingsRepositoryPolicy(settingsRepository.getSnapshots()));
315         return modelRepository;
316     }
317 
318     public static org.apache.maven.model.RepositoryPolicy fromSettingsRepositoryPolicy(
319             org.apache.maven.settings.RepositoryPolicy settingsRepositoryPolicy) {
320         org.apache.maven.model.RepositoryPolicy modelRepositoryPolicy = new org.apache.maven.model.RepositoryPolicy();
321         if (settingsRepositoryPolicy != null) {
322             modelRepositoryPolicy.setEnabled(settingsRepositoryPolicy.isEnabled());
323             modelRepositoryPolicy.setUpdatePolicy(settingsRepositoryPolicy.getUpdatePolicy());
324             modelRepositoryPolicy.setChecksumPolicy(settingsRepositoryPolicy.getChecksumPolicy());
325         }
326         return modelRepositoryPolicy;
327     }
328 
329     /**
330      * @deprecated use a service method {@link #buildArtifactRepositoryFromRepo(org.apache.maven.settings.Repository)} instead
331      */
332     @Deprecated
333     public static ArtifactRepository buildArtifactRepository(org.apache.maven.settings.Repository repo)
334             throws InvalidRepositoryException {
335         return instance.buildArtifactRepositoryFromRepo(repo);
336     }
337 
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 method {@link #buildArtifactRepositoryFromRepo(Repository)} instead
348      */
349     @Deprecated
350     public static ArtifactRepository buildArtifactRepository(org.apache.maven.model.Repository repo)
351             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 {
360         if (repo != null) {
361             String id = repo.getId();
362 
363             if (StringUtils.isEmpty(id)) {
364                 throw new InvalidRepositoryException("Repository identifier missing", "");
365             }
366 
367             String url = repo.getUrl();
368 
369             if (StringUtils.isEmpty(url)) {
370                 throw new InvalidRepositoryException("URL missing for repository " + id, id);
371             }
372 
373             ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy(repo.getSnapshots());
374 
375             ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy(repo.getReleases());
376 
377             ArtifactRepositoryLayout layout = getLayout(repo.getId(), repo.getLayout());
378 
379             return createArtifactRepository(id, url, layout, snapshots, releases);
380         } else {
381             return null;
382         }
383     }
384 
385     public static ArtifactRepositoryPolicy buildArtifactRepositoryPolicy(
386             org.apache.maven.model.RepositoryPolicy policy) {
387         boolean enabled = true;
388 
389         String updatePolicy = null;
390 
391         String checksumPolicy = null;
392 
393         if (policy != null) {
394             enabled = policy.isEnabled();
395 
396             if (policy.getUpdatePolicy() != null) {
397                 updatePolicy = policy.getUpdatePolicy();
398             }
399             if (policy.getChecksumPolicy() != null) {
400                 checksumPolicy = policy.getChecksumPolicy();
401             }
402         }
403 
404         return new ArtifactRepositoryPolicy(enabled, updatePolicy, checksumPolicy);
405     }
406 
407     public ArtifactRepository createArtifactRepository(
408             String id,
409             String url,
410             String layoutId,
411             ArtifactRepositoryPolicy snapshots,
412             ArtifactRepositoryPolicy releases)
413             throws Exception {
414         ArtifactRepositoryLayout layout = layouts.get(layoutId);
415 
416         checkLayout(id, layoutId, layout);
417 
418         return createArtifactRepository(id, url, layout, snapshots, releases);
419     }
420 
421     private void checkLayout(String repositoryId, String layoutId, ArtifactRepositoryLayout layout) throws Exception {
422         if (layout == null) {
423             throw new Exception(
424                     String.format("Cannot find ArtifactRepositoryLayout instance for: %s %s", layoutId, repositoryId));
425         }
426     }
427 
428     public static ArtifactRepository createArtifactRepository(
429             String id,
430             String url,
431             ArtifactRepositoryLayout repositoryLayout,
432             ArtifactRepositoryPolicy snapshots,
433             ArtifactRepositoryPolicy releases) {
434         if (snapshots == null) {
435             snapshots = new ArtifactRepositoryPolicy();
436         }
437 
438         if (releases == null) {
439             releases = new ArtifactRepositoryPolicy();
440         }
441 
442         ArtifactRepository repository;
443         if (repositoryLayout instanceof ArtifactRepositoryLayout2) {
444             repository = ((ArtifactRepositoryLayout2) repositoryLayout)
445                     .newMavenArtifactRepository(id, url, snapshots, releases);
446         } else {
447             repository = new MavenArtifactRepository(id, url, repositoryLayout, snapshots, releases);
448         }
449 
450         return repository;
451     }
452 
453     // ArtifactFactory
454     private Artifact createArtifactX(String groupId, String artifactId, String version, String scope, String type) {
455         return createArtifactX(groupId, artifactId, version, scope, type, null, null);
456     }
457 
458     private Artifact createDependencyArtifactX(
459             String groupId,
460             String artifactId,
461             VersionRange versionRange,
462             String type,
463             String classifier,
464             String scope,
465             boolean optional) {
466         return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, null, optional);
467     }
468 
469     private Artifact createProjectArtifactX(String groupId, String artifactId, String version) {
470         return createProjectArtifactX(groupId, artifactId, version, null);
471     }
472 
473     private Artifact createParentArtifactX(String groupId, String artifactId, String version) {
474         return createProjectArtifactX(groupId, artifactId, version);
475     }
476 
477     private Artifact createPluginArtifactX(String groupId, String artifactId, VersionRange versionRange) {
478         return createArtifactX(groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null);
479     }
480 
481     private Artifact createProjectArtifactX(String groupId, String artifactId, String version, String scope) {
482         return createArtifactX(groupId, artifactId, version, scope, "pom");
483     }
484 
485     private Artifact createExtensionArtifactX(String groupId, String artifactId, VersionRange versionRange) {
486         return createArtifactX(groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null);
487     }
488 
489     private Artifact createArtifactX(
490             String groupId,
491             String artifactId,
492             String version,
493             String scope,
494             String type,
495             String classifier,
496             String inheritedScope) {
497         VersionRange versionRange = null;
498         if (version != null) {
499             versionRange = VersionRange.createFromVersion(version);
500         }
501         return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope);
502     }
503 
504     private Artifact createArtifactX(
505             String groupId,
506             String artifactId,
507             VersionRange versionRange,
508             String type,
509             String classifier,
510             String scope,
511             String inheritedScope) {
512         return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false);
513     }
514 
515     @SuppressWarnings("checkstyle:parameternumber")
516     private Artifact createArtifactX(
517             String groupId,
518             String artifactId,
519             VersionRange versionRange,
520             String type,
521             String classifier,
522             String scope,
523             String inheritedScope,
524             boolean optional) {
525         String desiredScope = Artifact.SCOPE_RUNTIME;
526 
527         if (inheritedScope == null) {
528             desiredScope = scope;
529         } else if (Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_PROVIDED.equals(scope)) {
530             return null;
531         } else if (Artifact.SCOPE_COMPILE.equals(scope) && Artifact.SCOPE_COMPILE.equals(inheritedScope)) {
532             // added to retain compile artifactScope. Remove if you want compile inherited as runtime
533             desiredScope = Artifact.SCOPE_COMPILE;
534         }
535 
536         if (Artifact.SCOPE_TEST.equals(inheritedScope)) {
537             desiredScope = Artifact.SCOPE_TEST;
538         }
539 
540         if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) {
541             desiredScope = Artifact.SCOPE_PROVIDED;
542         }
543 
544         if (Artifact.SCOPE_SYSTEM.equals(scope)) {
545             // system scopes come through unchanged...
546             desiredScope = Artifact.SCOPE_SYSTEM;
547         }
548 
549         ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);
550 
551         return new DefaultArtifact(
552                 groupId, artifactId, versionRange, desiredScope, type, classifier, handler, optional);
553     }
554 
555     //
556     // Code taken from LegacyRepositorySystem
557     //
558 
559     public ArtifactRepository createDefaultRemoteRepository(MavenExecutionRequest request) throws Exception {
560         return createRepository(
561                 RepositorySystem.DEFAULT_REMOTE_REPO_URL,
562                 RepositorySystem.DEFAULT_REMOTE_REPO_ID,
563                 true,
564                 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
565                 false,
566                 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
567                 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
568     }
569 
570     public ArtifactRepository createRepository(
571             String url,
572             String repositoryId,
573             boolean releases,
574             String releaseUpdates,
575             boolean snapshots,
576             String snapshotUpdates,
577             String checksumPolicy)
578             throws Exception {
579         ArtifactRepositoryPolicy snapshotsPolicy =
580                 new ArtifactRepositoryPolicy(snapshots, snapshotUpdates, checksumPolicy);
581 
582         ArtifactRepositoryPolicy releasesPolicy =
583                 new ArtifactRepositoryPolicy(releases, releaseUpdates, checksumPolicy);
584 
585         return createArtifactRepository(repositoryId, url, "default", snapshotsPolicy, releasesPolicy);
586     }
587 
588     public Set<String> getRepoIds(List<ArtifactRepository> repositories) {
589         Set<String> repoIds = new HashSet<>();
590 
591         if (repositories != null) {
592             for (ArtifactRepository repository : repositories) {
593                 repoIds.add(repository.getId());
594             }
595         }
596 
597         return repoIds;
598     }
599 
600     /**
601      * Source from org.apache.maven.repository.legacy.LegacyRepositorySystem#getEffectiveRepositories
602      *
603      * @param repositories
604      * @return
605      * @since 3.6.1
606      */
607     public List<ArtifactRepository> getEffectiveRepositories(List<ArtifactRepository> repositories) {
608         if (repositories == null) {
609             return null;
610         }
611 
612         Map<String, List<ArtifactRepository>> reposByKey = new LinkedHashMap<>();
613 
614         for (ArtifactRepository repository : repositories) {
615             String key = repository.getId();
616 
617             List<ArtifactRepository> aliasedRepos = reposByKey.get(key);
618 
619             if (aliasedRepos == null) {
620                 aliasedRepos = new ArrayList<>();
621                 reposByKey.put(key, aliasedRepos);
622             }
623 
624             aliasedRepos.add(repository);
625         }
626 
627         List<ArtifactRepository> effectiveRepositories = new ArrayList<>();
628 
629         for (List<ArtifactRepository> aliasedRepos : reposByKey.values()) {
630             List<ArtifactRepository> mirroredRepos = new ArrayList<>();
631 
632             List<ArtifactRepositoryPolicy> releasePolicies = new ArrayList<>(aliasedRepos.size());
633 
634             for (ArtifactRepository aliasedRepo : aliasedRepos) {
635                 releasePolicies.add(aliasedRepo.getReleases());
636                 mirroredRepos.addAll(aliasedRepo.getMirroredRepositories());
637             }
638 
639             ArtifactRepositoryPolicy releasePolicy = getEffectivePolicy(releasePolicies);
640 
641             List<ArtifactRepositoryPolicy> snapshotPolicies = new ArrayList<>(aliasedRepos.size());
642 
643             for (ArtifactRepository aliasedRepo : aliasedRepos) {
644                 snapshotPolicies.add(aliasedRepo.getSnapshots());
645             }
646 
647             ArtifactRepositoryPolicy snapshotPolicy = getEffectivePolicy(snapshotPolicies);
648 
649             ArtifactRepository aliasedRepo = aliasedRepos.get(0);
650 
651             ArtifactRepository effectiveRepository = createArtifactRepository(
652                     aliasedRepo.getId(), aliasedRepo.getUrl(), aliasedRepo.getLayout(), snapshotPolicy, releasePolicy);
653 
654             effectiveRepository.setAuthentication(aliasedRepo.getAuthentication());
655 
656             effectiveRepository.setProxy(aliasedRepo.getProxy());
657 
658             effectiveRepository.setMirroredRepositories(mirroredRepos);
659 
660             effectiveRepository.setBlocked(aliasedRepo.isBlocked());
661 
662             effectiveRepositories.add(effectiveRepository);
663         }
664 
665         return effectiveRepositories;
666     }
667 
668     private ArtifactRepositoryPolicy getEffectivePolicy(Collection<ArtifactRepositoryPolicy> policies) {
669         ArtifactRepositoryPolicy effectivePolicy = null;
670 
671         for (ArtifactRepositoryPolicy policy : policies) {
672             if (effectivePolicy == null) {
673                 effectivePolicy = new ArtifactRepositoryPolicy(policy);
674             } else {
675                 effectivePolicy.merge(policy);
676             }
677         }
678 
679         return effectivePolicy;
680     }
681 
682     public ArtifactRepository createLocalRepository(MavenExecutionRequest request, File localRepository)
683             throws Exception {
684         return createRepository(
685                 "file://" + localRepository.toURI().getRawPath(),
686                 RepositorySystem.DEFAULT_LOCAL_REPO_ID,
687                 true,
688                 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
689                 true,
690                 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
691                 ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE);
692     }
693 
694     private static final String WILDCARD = "*";
695 
696     private static final String EXTERNAL_WILDCARD = "external:*";
697 
698     private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
699 
700     public static Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
701         String repoId = repository.getId();
702 
703         if (repoId != null && mirrors != null) {
704             for (Mirror mirror : mirrors) {
705                 if (repoId.equals(mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
706                     return mirror;
707                 }
708             }
709 
710             for (Mirror mirror : mirrors) {
711                 if (matchPattern(repository, mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
712                     return mirror;
713                 }
714             }
715         }
716 
717         return null;
718     }
719 
720     /**
721      * This method checks if the pattern matches the originalRepository. Valid patterns:
722      * <ul>
723      * <li>{@code *} (since 2.0.5)= everything,</li>
724      * <li>{@code external:*}  (since 2.0.9)= everything not on the localhost and not file based,</li>
725      * <li>{@code external:http:*} (since 3.8.0)= any repository not on the localhost using HTTP,</li>
726      * <li>{@code repo,repo1}  (since 2.0.9)= {@code repo} or {@code repo1},</li>
727      * <li>{@code *,!repo1} (since 2.0.9)= everything except {@code repo1}.</li>
728      * </ul>
729      *
730      * @param originalRepository to compare for a match.
731      * @param pattern used for match.
732      * @return true if the repository is a match to this pattern.
733      */
734     static boolean matchPattern(ArtifactRepository originalRepository, String pattern) {
735         boolean result = false;
736         String originalId = originalRepository.getId();
737 
738         // simple checks first to short circuit processing below.
739         if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
740             result = true;
741         } else {
742             // process the list
743             String[] repos = pattern.split(",");
744             for (String repo : repos) {
745                 // see if this is a negative match
746                 if (repo.length() > 1 && repo.startsWith("!")) {
747                     if (repo.substring(1).equals(originalId)) {
748                         // explicitly exclude. Set result and stop processing.
749                         result = false;
750                         break;
751                     }
752                 }
753                 // check for exact match
754                 else if (repo.equals(originalId)) {
755                     result = true;
756                     break;
757                 }
758                 // check for external:*
759                 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(originalRepository)) {
760                     result = true;
761                     // don't stop processing in case a future segment explicitly excludes this repo
762                 }
763                 // check for external:http:*
764                 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(originalRepository)) {
765                     result = true;
766                     // don't stop processing in case a future segment explicitly excludes this repo
767                 } else if (WILDCARD.equals(repo)) {
768                     result = true;
769                     // don't stop processing in case a future segment explicitly excludes this repo
770                 }
771             }
772         }
773         return result;
774     }
775 
776     /**
777      * Checks the URL to see if this repository refers to an external repository
778      *
779      * @param originalRepository
780      * @return true if external.
781      */
782     static boolean isExternalRepo(ArtifactRepository originalRepository) {
783         try {
784             URL url = new URL(originalRepository.getUrl());
785             return !(isLocal(url.getHost()) || url.getProtocol().equals("file"));
786         } catch (MalformedURLException e) {
787             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
788             return false;
789         }
790     }
791 
792     private static boolean isLocal(String host) {
793         return "localhost".equals(host) || "127.0.0.1".equals(host);
794     }
795 
796     /**
797      * Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
798      *
799      * @param originalRepository
800      * @return true if external.
801      */
802     static boolean isExternalHttpRepo(ArtifactRepository originalRepository) {
803         try {
804             URL url = new URL(originalRepository.getUrl());
805             return ("http".equalsIgnoreCase(url.getProtocol())
806                             || "dav".equalsIgnoreCase(url.getProtocol())
807                             || "dav:http".equalsIgnoreCase(url.getProtocol())
808                             || "dav+http".equalsIgnoreCase(url.getProtocol()))
809                     && !isLocal(url.getHost());
810         } catch (MalformedURLException e) {
811             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
812             return false;
813         }
814     }
815 
816     static boolean matchesLayout(ArtifactRepository repository, Mirror mirror) {
817         return matchesLayout(RepositoryUtils.getLayout(repository), mirror.getMirrorOfLayouts());
818     }
819 
820     /**
821      * Checks whether the layouts configured for a mirror match with the layout of the repository.
822      *
823      * @param repoLayout The layout of the repository, may be {@code null}.
824      * @param mirrorLayout The layouts supported by the mirror, may be {@code null}.
825      * @return {@code true} if the layouts associated with the mirror match the layout of the original repository,
826      *         {@code false} otherwise.
827      */
828     static boolean matchesLayout(String repoLayout, String mirrorLayout) {
829         boolean result = false;
830 
831         // simple checks first to short circuit processing below.
832         if (StringUtils.isEmpty(mirrorLayout) || WILDCARD.equals(mirrorLayout)) {
833             result = true;
834         } else if (mirrorLayout.equals(repoLayout)) {
835             result = true;
836         } else {
837             // process the list
838             String[] layouts = mirrorLayout.split(",");
839             for (String layout : layouts) {
840                 // see if this is a negative match
841                 if (layout.length() > 1 && layout.startsWith("!")) {
842                     if (layout.substring(1).equals(repoLayout)) {
843                         // explicitly exclude. Set result and stop processing.
844                         result = false;
845                         break;
846                     }
847                 }
848                 // check for exact match
849                 else if (layout.equals(repoLayout)) {
850                     result = true;
851                     break;
852                 } else if (WILDCARD.equals(layout)) {
853                     result = true;
854                     // don't stop processing in case a future segment explicitly excludes this repo
855                 }
856             }
857         }
858 
859         return result;
860     }
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     }
900 }