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