Skip to content

[DOXIASITETOOLS-348] Allow to enforce a parent site descriptor #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ private Map.Entry<SiteModel, MavenProject> getSiteModel(
MavenProject parentProject = project.getParent();

// 4. merge with parent project SiteModel
if (parentProject != null && (siteModel == null || siteModel.isMergeParent())) {
if (parentProject != null && (siteModel == null || siteModel.isMergeParent() || siteModel.isRequireParent())) {
depth++;
LOGGER.debug("Looking for site descriptor of level " + depth + " parent project: " + parentProject.getId());

Expand All @@ -1038,6 +1038,13 @@ private Map.Entry<SiteModel, MavenProject> getSiteModel(
depth, parentSiteDirectory, locale, parentProject, repoSession, remoteProjectRepositories)
.getKey();

if (siteModel != null) {
if (siteModel.isRequireParent() && parentSiteModel == null) {
throw new SiteToolException(
"The site descriptor for '" + project.getId() + "' requires a parent site descriptor for '"
+ parentProject.getId() + "' or any of its parents but none could be found!");
}
}
// MSHARED-116 requires site model (instead of a null one)
// MSHARED-145 requires us to do this only if there is a parent to merge it with
if (siteModel == null && parentSiteModel != null) {
Expand Down Expand Up @@ -1065,6 +1072,9 @@ private Map.Entry<SiteModel, MavenProject> getSiteModel(
parentSiteModel,
projectDistMgmnt,
parentDistMgmnt == null ? projectDistMgmnt : parentDistMgmnt);
} else if (parentProject == null && siteModel != null && siteModel.isRequireParent()) {
throw new SiteToolException("The site descriptor for '" + project.getId()
+ "' requires a parent site descriptor but no parent is defined in the POM.");
}

return new AbstractMap.SimpleEntry<>(siteModel, parentProject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.maven.doxia.tools;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.File;
import java.io.StringReader;
Expand All @@ -32,19 +31,15 @@
import java.util.List;
import java.util.Locale;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.doxia.site.LinkItem;
import org.apache.maven.doxia.site.SiteModel;
import org.apache.maven.doxia.site.Skin;
import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Reader;
import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Writer;
import org.apache.maven.doxia.tools.stubs.MavenProjectStub;
import org.apache.maven.doxia.tools.stubs.SiteToolMavenProjectStub;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
Expand All @@ -61,6 +56,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand All @@ -70,46 +66,16 @@
@PlexusTest
public class SiteToolTest {

@Inject
private PlexusContainer container;

@Inject
private ArtifactRepositoryFactory artifactRepositoryFactory;

@Inject
@Named("default")
private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;

@Inject
private DefaultSiteTool tool;

/**
* @return the repo.
*
* @throws Exception
*/
protected ArtifactRepository getLocalRepo() throws Exception {
String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
ArtifactRepositoryPolicy snapshotsPolicy =
new ArtifactRepositoryPolicy(true, updatePolicyFlag, checksumPolicyFlag);
ArtifactRepositoryPolicy releasesPolicy =
new ArtifactRepositoryPolicy(true, updatePolicyFlag, checksumPolicyFlag);
return artifactRepositoryFactory.createArtifactRepository(
"local",
getTestFile("target/local-repo").toURI().toURL().toString(),
defaultArtifactRepositoryLayout,
snapshotsPolicy,
releasesPolicy);
}

/**
* @return the local repo directory.
*
* @throws Exception
*/
protected File getLocalRepoDir() throws Exception {
return new File(getLocalRepo().getBasedir());
return getTestFile("target/local-repo");
}

protected RepositorySystemSession newRepoSession() throws Exception {
Expand Down Expand Up @@ -567,6 +533,59 @@ public void testConvertOldToNewSiteModel() throws Exception {
assertEquals(newModel, model);
}

@Test
public void testRequireParent() throws SiteToolException, Exception {
assertNotNull(tool);

SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("require-parent-test");
MavenProjectStub parentProject = new MavenProjectStub() {
@Override
public File getBasedir() {
return null; // this should be a non reactor/local project
}
};
parentProject.setGroupId("org.apache.maven.shared.its");
parentProject.setArtifactId("mshared-217-parent");
parentProject.setVersion("1.0-SNAPSHOT");
project.setParent(parentProject);
List<MavenProject> reactorProjects = new ArrayList<MavenProject>();

RepositorySystemSession repoSession = newRepoSession();
// coordinates for site descriptor: <groupId>:<artifactId>:xml:site:<version>
new SiteToolMavenProjectStub("require-parent-test");
org.eclipse.aether.artifact.Artifact parentArtifact = new org.eclipse.aether.artifact.DefaultArtifact(
"org.apache.maven.shared.its:mshared-217-parent:xml:site:1.0-SNAPSHOT");
File parentArtifactInRepoFile = new File(
repoSession.getLocalRepository().getBasedir(),
repoSession.getLocalRepositoryManager().getPathForLocalArtifact(parentArtifact));

// model from current local build
assertThrows(
SiteToolException.class,
() -> tool.getSiteModel(
new File(project.getBasedir(), "src/site"),
SiteTool.DEFAULT_LOCALE,
project,
reactorProjects,
repoSession,
project.getRemoteProjectRepositories()));

// now copy parent site descriptor to repo
FileUtils.copyFile(
getTestFile("src/test/resources/unit/require-parent-test/parent-site.xml"), parentArtifactInRepoFile);
try {
tool.getSiteModel(
new File(project.getBasedir(), "src/site"),
SiteTool.DEFAULT_LOCALE,
project,
reactorProjects,
repoSession,
project.getRemoteProjectRepositories());
} finally {
parentArtifactInRepoFile.delete();
}
}

private void writeModel(SiteModel model, String to) throws Exception {
Writer writer = WriterFactory.newXmlWriter(getTestFile("target/test-classes/" + to));
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
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.
-->
<site xmlns="http://maven.apache.org/SITE/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SITE/2.1.0 file:../../../target/generated-site/xsd/site-2.1.0.xsd">

</site>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.shared</groupId>
<artifactId>enforce-parent-parent-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>enforce-parent-test</artifactId>
<packaging>jar</packaging>

<name>dummy</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
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.
-->
<site xmlns="http://maven.apache.org/SITE/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SITE/2.1.0 file:../../../target/generated-site/xsd/site-2.1.0.xsd"
requireParent="true">

</site>
10 changes: 10 additions & 0 deletions doxia-site-model/src/main/mdo/site.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ under the License.
<type>String</type>
<defaultValue>merge</defaultValue>
</field>
<field xml.attribute="true">
<description><![CDATA[
Whether this "site.xml" should inherit from a parent "site.xml". If set to "true" it fails the build in case a parent site descriptor cannot be retrieved.
It does not necessarily need to be the direct parent but just a site descriptor anywhere in the parent hierarchy.
]]></description>
<name>requireParent</name>
<version>2.0.0+</version>
<type>boolean</type>
<defaultValue>false</defaultValue>
</field>
<field>
<name>bannerLeft</name>
<description>Banner logo on the masthead of the site to the left.</description>
Expand Down