|
20 | 20 |
|
21 | 21 | import java.io.File; |
22 | 22 | import java.net.MalformedURLException; |
| 23 | +import java.net.URI; |
23 | 24 | import java.net.URL; |
24 | 25 | import java.util.List; |
25 | 26 | import java.util.Locale; |
| 27 | +import java.util.Objects; |
26 | 28 |
|
27 | 29 | import org.apache.maven.doxia.site.inheritance.URIPathDescriptor; |
28 | 30 | import org.apache.maven.doxia.tools.SiteTool; |
|
35 | 37 | import org.apache.maven.plugins.annotations.Parameter; |
36 | 38 | import org.apache.maven.plugins.site.AbstractSiteMojo; |
37 | 39 | import org.apache.maven.project.MavenProject; |
| 40 | +import org.apache.maven.scm.provider.ScmUrlUtils; |
38 | 41 | import org.apache.maven.settings.Proxy; |
39 | 42 | import org.apache.maven.settings.Server; |
40 | 43 | import org.apache.maven.settings.Settings; |
@@ -523,18 +526,69 @@ protected MavenProject getTopLevelProject(MavenProject project) throws MojoExecu |
523 | 526 | return oldProject; |
524 | 527 | } |
525 | 528 |
|
526 | | - // MSITE-600 |
527 | | - URIPathDescriptor siteURI = new URIPathDescriptor(URIEncoder.encodeURI(site.getUrl()), ""); |
528 | | - URIPathDescriptor oldSiteURI = new URIPathDescriptor(URIEncoder.encodeURI(oldSite.getUrl()), ""); |
529 | | - |
530 | | - if (!siteURI.sameSite(oldSiteURI.getBaseURI())) { |
| 529 | + try { |
| 530 | + if (!isSameSite(site.getUrl(), oldSite.getUrl())) { |
| 531 | + return oldProject; |
| 532 | + } |
| 533 | + } catch (IllegalArgumentException e) { |
| 534 | + getLog().warn("Failed to parse distributionManagement.site.url of project \"" + getFullName(oldProject) |
| 535 | + + "\" or project \"" + getFullName(parent) + "\": " + e.getMessage()); |
531 | 536 | return oldProject; |
532 | 537 | } |
533 | 538 | } |
534 | | - |
535 | 539 | return parent; |
536 | 540 | } |
537 | 541 |
|
| 542 | + /** |
| 543 | + * Returns {@code true} if the URIs are probably pointing to the same site which means |
| 544 | + * <ul> |
| 545 | + * <li>both arguments are hierarchical URIs,</li> |
| 546 | + * <li>both arguments share the same host and</li> |
| 547 | + * <li>the path of the latter URI is a subpath of the first URI</li> |
| 548 | + * </ul>. |
| 549 | + * @param parentUri |
| 550 | + * @param uri |
| 551 | + * @return {@code true} if the URIs are probably pointing to the same site |
| 552 | + * @throws IllegalArgumentException if the given URIs cannot be parsed |
| 553 | + */ |
| 554 | + static boolean isSameSite(String parentUri, String uri) { |
| 555 | + // this just normalizes the paths in it |
| 556 | + URIPathDescriptor siteURI = |
| 557 | + new URIPathDescriptor(URIEncoder.encodeURI(extractProviderSpecificPartFromScmUri(parentUri)), ""); |
| 558 | + URIPathDescriptor oldSiteURI = |
| 559 | + new URIPathDescriptor(URIEncoder.encodeURI(extractProviderSpecificPartFromScmUri(uri)), ""); |
| 560 | + // compare host and path (port and scheme should not matter) |
| 561 | + return isSameSite(siteURI.getBaseURI(), oldSiteURI.getBaseURI()); |
| 562 | + } |
| 563 | + |
| 564 | + private static boolean isSameSite(URI parentUri, URI uri) { |
| 565 | + // host must be equal |
| 566 | + if (!Objects.equals(uri.getHost(), parentUri.getHost())) { |
| 567 | + return false; |
| 568 | + } |
| 569 | + // path must be a subpath |
| 570 | + if (uri.getPath() == null |
| 571 | + || parentUri.getPath() == null |
| 572 | + || !uri.getPath().startsWith(parentUri.getPath())) { |
| 573 | + return false; |
| 574 | + } |
| 575 | + return true; |
| 576 | + } |
| 577 | + |
| 578 | + /** |
| 579 | + * Unwraps <a href="https://maven.apache.org/scm/scm-url-format.html">SCM URLs</a> to get the provider specific part. |
| 580 | + * @param uri |
| 581 | + * @return the provider specific part if the given URI is a SCM URI, otherwise just the uri |
| 582 | + * |
| 583 | + */ |
| 584 | + static String extractProviderSpecificPartFromScmUri(String uri) { |
| 585 | + if (ScmUrlUtils.isValid(uri)) { |
| 586 | + return ScmUrlUtils.getProviderSpecificPart(uri); |
| 587 | + } else { |
| 588 | + return uri; |
| 589 | + } |
| 590 | + } |
| 591 | + |
538 | 592 | private static class URIEncoder { |
539 | 593 | private static final String MARK = "-_.!~*'()"; |
540 | 594 | private static final String RESERVED = ";/?:@&=+$,"; |
|
0 commit comments