Skip to content

Commit

Permalink
[DOXIASITETOOLS-348] Allow to enforce a parent site descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Sep 20, 2024
1 parent 4e12aa8 commit 4bdef85
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,8 @@ 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.isEnforceParentDescriptor())) {
depth++;
LOGGER.debug("Looking for site descriptor of level " + depth + " parent project: " + parentProject.getId());

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

if (siteModel != null) {
if (siteModel.isEnforceParentDescriptor() && parentSiteModel == null) {
throw new SiteToolException("The site descriptor for '" + project.getId()
+ "' requires a parent site descriptor for '" + parentProject.getId() + "'");
}
}
// 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
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 testEnforcedParentSiteDescriptor() throws SiteToolException, Exception {
assertNotNull(tool);

SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("enforce-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("enforce-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/enforce-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"
enforceParentDescriptor="true">

</site>
2 changes: 1 addition & 1 deletion doxia-site-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ under the License.
<model>src/main/mdo/site.mdo</model>
</models>
<!-- TODO Do not forget to update the version in the description. See DOXIASITETOOLS-98. -->
<version>2.0.0</version>
<version>2.1.0</version>
</configuration>
</execution>
<execution>
Expand Down
9 changes: 9 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,15 @@ under the License.
<type>String</type>
<defaultValue>merge</defaultValue>
</field>
<field xml.attribute="true">
<description><![CDATA[
Whether to enforce a parent site descriptor.
]]></description>
<name>enforceParentDescriptor</name>
<version>2.1.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

0 comments on commit 4bdef85

Please sign in to comment.