Skip to content
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

Use clean URLs for the generated Maven site #26

Merged
merged 4 commits into from
Feb 13, 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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ jobs:
name: build
path: .

- name: 'Set up Java 11'
- name: 'Set up Java 17'
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17
cache: 'maven'

- name: 'Cache SonarCloud packages'
Expand Down
143 changes: 142 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</plugin>
<plugin>
<groupId>com.github.genthaler</groupId>
<artifactId>beanshell-maven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency><!-- used by late-site-add-canonical-urls -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
<plugins>
Expand Down Expand Up @@ -350,7 +362,7 @@
<plugin>
<groupId>org.simplify4u.plugins</groupId>
<artifactId>sitemapxml-maven-plugin</artifactId>
<version>2.0.0</version>
<version>2.2.0</version>
<configuration>
<maxDepth>8</maxDepth>
</configuration>
Expand Down Expand Up @@ -402,6 +414,135 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.genthaler</groupId>
<artifactId>beanshell-maven-plugin</artifactId>
<executions>
<execution>
<id>add-redirect-pages</id>
<phase>pre-site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<quiet>true</quiet>
<script><![CDATA[
// Generate redirect pages for historic site URLs (MPIR-323)
String contents(String target) {
String url = project.url + target;
String template = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " +
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" +
"<head>\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
" <link rel=\"canonical\" href=\"" + url + "\" />\n" +
" <meta http-equiv=\"refresh\" content=\"0;url=" + target + "\" />\n" +
"</head>\n<body>\n<h1>\n" +
" This page has been moved to <a href=\"" + target + "\">" + url + "</a>\n" +
" <script type='text/javascript'> window.location.replace(\"" + url + "\"); </script>\n" +
"</h1>\n</body>\n</html>\n";
return template;
}
File site = new File(project.reporting.outputDirectory);
site.mkdirs();
String[][] filenameArray = new String[][] {
{"integration.html", "ci-management.html"},
{"issue-tracking.html", "issue-management.html"},
{"license.html", "licenses.html"},
{"project-summary.html", "summary.html"},
{"source-repository.html", "scm.html"},
{"team-list.html", "team.html"}};
for (int i=0; i<filenameArray.length; i++) {
File file = new File(site, filenameArray[i][0]);
org.codehaus.plexus.util.FileUtils.fileWrite(file, "UTF-8", contents(filenameArray[i][1]));
}
]]>
</script>
</configuration>
</execution>
<execution>
<id>late-site-add-canonical-urls</id>
<phase>site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<quiet>true</quiet>
<script><![CDATA[
// Add canonical URLs to any site HTML pages that are missing them
String buildCanonicalUrl(String absoluteFilePath, String baseDirectory, String baseUrl) {
String urlPath = (
org.codehaus.plexus.util.FileUtils.basename(absoluteFilePath).equalsIgnoreCase("index.")
? org.codehaus.plexus.util.FileUtils.dirname(absoluteFilePath) + File.separator
: absoluteFilePath
).substring(baseDirectory.length());
return baseUrl + (File.separator.equals("/") ? urlPath : urlPath.replace(File.separator, "/"));
}
void insert(String filename, long offset, String content) throws IOException {
File tempFile = File.createTempFile(org.codehaus.plexus.util.FileUtils.filename(filename), null);
try {
RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");
try {
RandomAccessFile rtemp = new RandomAccessFile(tempFile, "rw");
try {
final long fileSize = r.length();
java.nio.channels.FileChannel sourceChannel = r.getChannel();
try {
java.nio.channels.FileChannel targetChannel = rtemp.getChannel();
try {
//move origin file contents from offset to end-of-file to temp file
sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
//clear origin file after offset
sourceChannel.truncate(offset);
r.seek(offset); //move to new end-of-file
r.writeBytes(content); //write new content
long newOffset = r.getFilePointer(); //obtain offset for new end-of-file
targetChannel.position(0L); //set cursor in temp file to beginning for read
//move saved content from temp file back to end of origin file
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
} finally {
targetChannel.close();
}
} finally {
sourceChannel.close();
}
} finally {
rtemp.close();
}
} finally {
r.close();
}
} finally {
org.codehaus.plexus.util.FileUtils.forceDelete(tempFile);
}
}
int countCanonicalLinks(File htmlFile, String projectUrl) throws IOException {
//jsoup object scope
org.jsoup.nodes.Document document = org.jsoup.Jsoup.parse(htmlFile, "UTF-8", projectUrl);
return document.head().selectXpath("//link[@rel='canonical']").size();
}
void ensureCanonicalLink(String absoluteFilePath, String outputDirectory, String projectUrl) throws IOException {
if (countCanonicalLinks(new File(absoluteFilePath), projectUrl) == 0) {
//build canonical link tag
String canonicalLinkTag = "<link rel=\"canonical\" href=\"" +
buildCanonicalUrl(absoluteFilePath, outputDirectory, projectUrl.substring(0, projectUrl.length() - 1)) + "\" />\n";
//find </head>
int offset = org.codehaus.plexus.util.FileUtils.fileRead(absoluteFilePath, "UTF-8").indexOf("</head>");
//insert link tag and linebreak
insert(absoluteFilePath, offset, canonicalLinkTag);
}
}
files = org.codehaus.plexus.util.FileUtils.getFilesFromExtension(
project.reporting.outputDirectory, new String[] { "htm", "html" });
for (int i=0; i<files.length; i++) {
ensureCanonicalLink(files[i], project.reporting.outputDirectory, project.url);
}
]]>
</script>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
Expand Down
Loading