Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ under the License.
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-api</artifactId>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions src/it/projects/site-inheritance/sub_module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ under the License.
<relativePath>../module</relativePath>
</parent>

<distributionManagement>
<site>
<id>website</id>
<!-- automatic inheritance calculation would incorrectly yield file:///@project.build.directory@/it/site-inheritance/webhost.company.com/deploy/www/website/submodule -->
<url>file:///@project.build.directory@/it/site-inheritance/webhost.company.com/deploy/www/website/module/submodule</url>
</site>
</distributionManagement>

<artifactId>sub_module</artifactId>
<name>Sub-Module</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import org.apache.maven.doxia.site.inheritance.URIPathDescriptor;
import org.apache.maven.doxia.tools.SiteTool;
Expand All @@ -35,6 +37,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.site.AbstractSiteMojo;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.provider.ScmUrlUtils;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
Expand Down Expand Up @@ -523,18 +526,69 @@ protected MavenProject getTopLevelProject(MavenProject project) throws MojoExecu
return oldProject;
}

// MSITE-600
URIPathDescriptor siteURI = new URIPathDescriptor(URIEncoder.encodeURI(site.getUrl()), "");
URIPathDescriptor oldSiteURI = new URIPathDescriptor(URIEncoder.encodeURI(oldSite.getUrl()), "");

if (!siteURI.sameSite(oldSiteURI.getBaseURI())) {
try {
if (!isSameSite(site.getUrl(), oldSite.getUrl())) {
return oldProject;
}
} catch (IllegalArgumentException e) {
getLog().warn("Failed to parse distributionManagement.site.url of project \"" + getFullName(oldProject)
+ "\" or project \"" + getFullName(parent) + "\": " + e.getMessage());
return oldProject;
}
}

return parent;
}

/**
* Returns {@code true} if the URIs are probably pointing to the same site which means
* <ul>
* <li>both arguments are hierarchical URIs,</li>
* <li>both arguments share the same host and</li>
* <li>the path of the latter URI is a subpath of the first URI</li>
* </ul>.
* @param parentUri
* @param uri
* @return {@code true} if the URIs are probably pointing to the same site
* @throws IllegalArgumentException if the given URIs cannot be parsed
*/
static boolean isSameSite(String parentUri, String uri) {
// this just normalizes the paths in it
URIPathDescriptor siteURI =
new URIPathDescriptor(URIEncoder.encodeURI(extractProviderSpecificPartFromScmUri(parentUri)), "");
URIPathDescriptor oldSiteURI =
new URIPathDescriptor(URIEncoder.encodeURI(extractProviderSpecificPartFromScmUri(uri)), "");
// compare host and path (port and scheme should not matter)
return isSameSite(siteURI.getBaseURI(), oldSiteURI.getBaseURI());
}

private static boolean isSameSite(URI parentUri, URI uri) {
// host must be equal
if (!Objects.equals(uri.getHost(), parentUri.getHost())) {
return false;
}
// path must be a subpath
if (uri.getPath() == null
|| parentUri.getPath() == null
|| !uri.getPath().startsWith(parentUri.getPath())) {
return false;
}
return true;
}

/**
* Unwraps <a href="https://maven.apache.org/scm/scm-url-format.html">SCM URLs</a> to get the provider specific part.
* @param uri
* @return the provider specific part if the given URI is a SCM URI, otherwise just the uri
*
*/
static String extractProviderSpecificPartFromScmUri(String uri) {
if (ScmUrlUtils.isValid(uri)) {
return ScmUrlUtils.getProviderSpecificPart(uri);
} else {
return uri;
}
}

private static class URIEncoder {
private static final String MARK = "-_.!~*'()";
private static final String RESERVED = ";/?:@&=+$,";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.site.deploy;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class AbstractDeployMojoTest {

@Test
public void testIsSameSite() {
Copy link
Member

@slawekjaranowski slawekjaranowski Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the same for?

scm:git:ssh://abc.abc
scm:git:https://abc.abc

Copy link
Member Author

@kwin kwin Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertTrue(AbstractDeployMojo.isSameSite(
"scm:svn:https://svn.apache.org/repos/asf/maven/website/components/${maven.site.path}",
"scm:svn:https://svn.apache.org/repos/asf/maven/website/components/${maven.site.path}"));
assertTrue(AbstractDeployMojo.isSameSite(
"https://svn.apache.org/repos/asf/maven/website/components/${maven.site.path}",
"https://svn.apache.org/repos/asf/maven/website/components/${maven.site.path}"));
// different scheme (encapsulated in SCM URL) and subpath
assertTrue(AbstractDeployMojo.isSameSite(
"scm:svn:http://svn.apache.org/repos/asf/maven/website",
"scm:svn:https://svn.apache.org/repos/asf/maven/website/components"));
assertTrue(AbstractDeployMojo.isSameSite(
"scm:git:ssh://github.com/codehaus-plexus/plexus-pom.git/",
"scm:git:ssh://github.com/codehaus-plexus/plexus-pom.git/"));
assertTrue(
AbstractDeployMojo.isSameSite(
"file:////Users/konradwindszus/git/maven/maven-site-plugin/target/it/site-inheritance/webhost.company.com/deploy/www/website/module",
"file:////Users/konradwindszus/git/maven/maven-site-plugin/target/it/site-inheritance/webhost.company.com/deploy/www/website/module/submodule"));
// sibling paths
assertFalse(AbstractDeployMojo.isSameSite(
"scm:svn:https://svn.apache.org/repos/asf/maven/website/a/${maven.site.path}",
"scm:svn:https://svn.apache.org/repos/asf/maven/website/b/${maven.site.path}"));
// sibling paths
assertFalse(AbstractDeployMojo.isSameSite(
"scm:git:ssh://github.com/codehaus-plexus/plexus-pom.git/",
"scm:git:ssh://[email protected]/codehaus-plexus/plexus-sec-dispatcher.git/"));
// SCM URLs which are opaque (i.e. non hierarchical URIs)
assertThrows(
IllegalArgumentException.class,
() -> AbstractDeployMojo.isSameSite(
"scm:local:/usr/modules:my_module", "scm:local:/usr/modules:my_module"));
}
}
Loading