You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kw...@apache.org on 2024/09/19 17:15:57 UTC

(maven-doxia-sitetools) 01/01: [DOXIASITETOOLS-348] Allow to enforce a parent site descriptor

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/enforce-parent-site-descriptor
in repository https://gitbox.apache.org/repos/asf/maven-doxia-sitetools.git

commit bd5647d25121db9faa43ca0851b9d58eade4a07d
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Sep 19 12:59:30 2024 +0200

    [DOXIASITETOOLS-348] Allow to enforce a parent site descriptor
---
 .../apache/maven/doxia/tools/DefaultSiteTool.java  |  9 ++-
 .../org/apache/maven/doxia/tools/SiteToolTest.java | 86 ++++++++++++++--------
 .../unit/enforce-parent-test/parent-site.xml       | 24 ++++++
 .../resources/unit/enforce-parent-test/pom.xml     | 43 +++++++++++
 .../unit/enforce-parent-test/src/site/site.xml     | 25 +++++++
 doxia-site-model/pom.xml                           |  2 +-
 doxia-site-model/src/main/mdo/site.mdo             |  9 +++
 7 files changed, 165 insertions(+), 33 deletions(-)

diff --git a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
index 76e464a..6036175 100644
--- a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
+++ b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
@@ -1018,7 +1018,7 @@ public class DefaultSiteTool implements SiteTool {
         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());
 
@@ -1038,6 +1038,13 @@ public class DefaultSiteTool implements SiteTool {
                             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) {
diff --git a/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java b/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
index c4dd00b..d86bc4a 100644
--- a/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
+++ b/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
@@ -24,6 +24,8 @@ import javax.inject.Named;
 import java.io.File;
 import java.io.StringReader;
 import java.io.Writer;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -32,6 +34,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
@@ -41,6 +45,7 @@ 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;
@@ -61,6 +66,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 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;
 
 /**
@@ -70,46 +76,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 @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 {
@@ -567,6 +543,54 @@ public class SiteToolTest {
         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 {
diff --git a/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/parent-site.xml b/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/parent-site.xml
new file mode 100644
index 0000000..f059a27
--- /dev/null
+++ b/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/parent-site.xml
@@ -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>
diff --git a/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/pom.xml b/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/pom.xml
new file mode 100644
index 0000000..89c1a8a
--- /dev/null
+++ b/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/pom.xml
@@ -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>
diff --git a/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/src/site/site.xml b/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/src/site/site.xml
new file mode 100644
index 0000000..753f384
--- /dev/null
+++ b/doxia-integration-tools/src/test/resources/unit/enforce-parent-test/src/site/site.xml
@@ -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>
diff --git a/doxia-site-model/pom.xml b/doxia-site-model/pom.xml
index 036596c..1c7f133 100644
--- a/doxia-site-model/pom.xml
+++ b/doxia-site-model/pom.xml
@@ -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>
diff --git a/doxia-site-model/src/main/mdo/site.mdo b/doxia-site-model/src/main/mdo/site.mdo
index fc68742..46faf18 100644
--- a/doxia-site-model/src/main/mdo/site.mdo
+++ b/doxia-site-model/src/main/mdo/site.mdo
@@ -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>