Skip to content

Commit 004b948

Browse files
authored
[DOXIASITETOOLS-348] Allow to require a parent site descriptor (#173)
This leads to build failures in case it is required but cannot be resolved.
1 parent 1de20e3 commit 004b948

File tree

6 files changed

+169
-38
lines changed

6 files changed

+169
-38
lines changed

doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ private Map.Entry<SiteModel, MavenProject> getSiteModel(
10181018
MavenProject parentProject = project.getParent();
10191019

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

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

1041+
if (siteModel != null) {
1042+
if (siteModel.isRequireParent() && parentSiteModel == null) {
1043+
throw new SiteToolException(
1044+
"The site descriptor for '" + project.getId() + "' requires a parent site descriptor for '"
1045+
+ parentProject.getId() + "' or any of its parents but none could be found!");
1046+
}
1047+
}
10411048
// MSHARED-116 requires site model (instead of a null one)
10421049
// MSHARED-145 requires us to do this only if there is a parent to merge it with
10431050
if (siteModel == null && parentSiteModel != null) {
@@ -1065,6 +1072,9 @@ private Map.Entry<SiteModel, MavenProject> getSiteModel(
10651072
parentSiteModel,
10661073
projectDistMgmnt,
10671074
parentDistMgmnt == null ? projectDistMgmnt : parentDistMgmnt);
1075+
} else if (parentProject == null && siteModel != null && siteModel.isRequireParent()) {
1076+
throw new SiteToolException("The site descriptor for '" + project.getId()
1077+
+ "' requires a parent site descriptor but no parent is defined in the POM.");
10681078
}
10691079

10701080
return new AbstractMap.SimpleEntry<>(siteModel, parentProject);

doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package org.apache.maven.doxia.tools;
2020

2121
import javax.inject.Inject;
22-
import javax.inject.Named;
2322

2423
import java.io.File;
2524
import java.io.StringReader;
@@ -32,19 +31,15 @@
3231
import java.util.List;
3332
import java.util.Locale;
3433

35-
import org.apache.maven.artifact.repository.ArtifactRepository;
36-
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
37-
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
38-
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
3934
import org.apache.maven.doxia.site.LinkItem;
4035
import org.apache.maven.doxia.site.SiteModel;
4136
import org.apache.maven.doxia.site.Skin;
4237
import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Reader;
4338
import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Writer;
39+
import org.apache.maven.doxia.tools.stubs.MavenProjectStub;
4440
import org.apache.maven.doxia.tools.stubs.SiteToolMavenProjectStub;
4541
import org.apache.maven.project.MavenProject;
4642
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
47-
import org.codehaus.plexus.PlexusContainer;
4843
import org.codehaus.plexus.testing.PlexusTest;
4944
import org.codehaus.plexus.util.FileUtils;
5045
import org.codehaus.plexus.util.IOUtil;
@@ -61,6 +56,7 @@
6156
import static org.junit.jupiter.api.Assertions.assertNotNull;
6257
import static org.junit.jupiter.api.Assertions.assertNotSame;
6358
import static org.junit.jupiter.api.Assertions.assertNull;
59+
import static org.junit.jupiter.api.Assertions.assertThrows;
6460
import static org.junit.jupiter.api.Assertions.assertTrue;
6561

6662
/**
@@ -70,46 +66,16 @@
7066
@PlexusTest
7167
public class SiteToolTest {
7268

73-
@Inject
74-
private PlexusContainer container;
75-
76-
@Inject
77-
private ArtifactRepositoryFactory artifactRepositoryFactory;
78-
79-
@Inject
80-
@Named("default")
81-
private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
82-
8369
@Inject
8470
private DefaultSiteTool tool;
8571

86-
/**
87-
* @return the repo.
88-
*
89-
* @throws Exception
90-
*/
91-
protected ArtifactRepository getLocalRepo() throws Exception {
92-
String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
93-
String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
94-
ArtifactRepositoryPolicy snapshotsPolicy =
95-
new ArtifactRepositoryPolicy(true, updatePolicyFlag, checksumPolicyFlag);
96-
ArtifactRepositoryPolicy releasesPolicy =
97-
new ArtifactRepositoryPolicy(true, updatePolicyFlag, checksumPolicyFlag);
98-
return artifactRepositoryFactory.createArtifactRepository(
99-
"local",
100-
getTestFile("target/local-repo").toURI().toURL().toString(),
101-
defaultArtifactRepositoryLayout,
102-
snapshotsPolicy,
103-
releasesPolicy);
104-
}
105-
10672
/**
10773
* @return the local repo directory.
10874
*
10975
* @throws Exception
11076
*/
11177
protected File getLocalRepoDir() throws Exception {
112-
return new File(getLocalRepo().getBasedir());
78+
return getTestFile("target/local-repo");
11379
}
11480

11581
protected RepositorySystemSession newRepoSession() throws Exception {
@@ -567,6 +533,59 @@ public void testConvertOldToNewSiteModel() throws Exception {
567533
assertEquals(newModel, model);
568534
}
569535

536+
@Test
537+
public void testRequireParent() throws SiteToolException, Exception {
538+
assertNotNull(tool);
539+
540+
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("require-parent-test");
541+
MavenProjectStub parentProject = new MavenProjectStub() {
542+
@Override
543+
public File getBasedir() {
544+
return null; // this should be a non reactor/local project
545+
}
546+
};
547+
parentProject.setGroupId("org.apache.maven.shared.its");
548+
parentProject.setArtifactId("mshared-217-parent");
549+
parentProject.setVersion("1.0-SNAPSHOT");
550+
project.setParent(parentProject);
551+
List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
552+
553+
RepositorySystemSession repoSession = newRepoSession();
554+
// coordinates for site descriptor: <groupId>:<artifactId>:xml:site:<version>
555+
new SiteToolMavenProjectStub("require-parent-test");
556+
org.eclipse.aether.artifact.Artifact parentArtifact = new org.eclipse.aether.artifact.DefaultArtifact(
557+
"org.apache.maven.shared.its:mshared-217-parent:xml:site:1.0-SNAPSHOT");
558+
File parentArtifactInRepoFile = new File(
559+
repoSession.getLocalRepository().getBasedir(),
560+
repoSession.getLocalRepositoryManager().getPathForLocalArtifact(parentArtifact));
561+
562+
// model from current local build
563+
assertThrows(
564+
SiteToolException.class,
565+
() -> tool.getSiteModel(
566+
new File(project.getBasedir(), "src/site"),
567+
SiteTool.DEFAULT_LOCALE,
568+
project,
569+
reactorProjects,
570+
repoSession,
571+
project.getRemoteProjectRepositories()));
572+
573+
// now copy parent site descriptor to repo
574+
FileUtils.copyFile(
575+
getTestFile("src/test/resources/unit/require-parent-test/parent-site.xml"), parentArtifactInRepoFile);
576+
try {
577+
tool.getSiteModel(
578+
new File(project.getBasedir(), "src/site"),
579+
SiteTool.DEFAULT_LOCALE,
580+
project,
581+
reactorProjects,
582+
repoSession,
583+
project.getRemoteProjectRepositories());
584+
} finally {
585+
parentArtifactInRepoFile.delete();
586+
}
587+
}
588+
570589
private void writeModel(SiteModel model, String to) throws Exception {
571590
Writer writer = WriterFactory.newXmlWriter(getTestFile("target/test-classes/" + to));
572591
try {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<site xmlns="http://maven.apache.org/SITE/2.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/SITE/2.1.0 file:../../../target/generated-site/xsd/site-2.1.0.xsd">
23+
24+
</site>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>org.apache.maven.shared</groupId>
27+
<artifactId>enforce-parent-parent-test</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
</parent>
30+
<artifactId>enforce-parent-test</artifactId>
31+
<packaging>jar</packaging>
32+
33+
<name>dummy</name>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<version>3.8.1</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<site xmlns="http://maven.apache.org/SITE/2.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/SITE/2.1.0 file:../../../target/generated-site/xsd/site-2.1.0.xsd"
23+
requireParent="true">
24+
25+
</site>

doxia-site-model/src/main/mdo/site.mdo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ under the License.
6666
<type>String</type>
6767
<defaultValue>merge</defaultValue>
6868
</field>
69+
<field xml.attribute="true">
70+
<description><![CDATA[
71+
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.
72+
It does not necessarily need to be the direct parent but just a site descriptor anywhere in the parent hierarchy.
73+
]]></description>
74+
<name>requireParent</name>
75+
<version>2.0.0+</version>
76+
<type>boolean</type>
77+
<defaultValue>false</defaultValue>
78+
</field>
6979
<field>
7080
<name>bannerLeft</name>
7181
<description>Banner logo on the masthead of the site to the left.</description>

0 commit comments

Comments
 (0)