Skip to content

Commit

Permalink
Reimplemented resource copy using 'plexus.util.DirectorScanner' inste…
Browse files Browse the repository at this point in the history
…ad of maven-filtering (#597)

Reimplement resource copy using 'org.codehaus.plexus.util.DirectoryScanner'
instead of maven-filtering

In exchange for some extra file management code, this reduces the amount of dependencies
used and injected into AsciidoctorMojo.

* Created model classes for Resources to ensure Mojo descriptors don't leak internals
* Removed 'org.apache.maven.shared:maven-filtering' dependency
* Bump plexus-utils to v3.3.1
* Removed unnecessary IT 'resource-filtering' since "under-the-hood" filtering is no longer possible
* Created CopyResourcesProcessor to extract logic from AsciidoctorMojo
* Remove unused encoding from resource processing
* ResourcesProcessor no longer throws MojoExecutionException
  • Loading branch information
abelsromero committed Aug 27, 2022
1 parent 59cd841 commit 1f9bfe3
Show file tree
Hide file tree
Showing 20 changed files with 705 additions and 355 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Improvements::
* Split plugin and site integration in sub-modules: asciidoctor-maven-plugin and asciidoctor-doxia-module (#595)
* Add 'asciidoc' as valid file extension in AsciidoctorDoxiaParserModule (#595)
* Fix throwing an exception when registering a non Extension (#596)
* Reimplement resource copy using 'plexus.util.DirectorScanner' instead of 'maven-filtering' to reduce dependencies and build time (#597)

Build / Infrastructure::

* Bump Doxia to v1.11.1 and maven-site-plugin in IT to 3.12.0 (#579)
* Bump netty-codec-http to v4.1.77.Final (fix CVE-2021-21290) (#582)
* Upgrade Asciidoctorj to v2.5.4 and jRuby to v9.3.4.0 (#584)
Expand Down
13 changes: 6 additions & 7 deletions asciidoctor-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

<properties>
<maven.plugin.plugin.version>3.5</maven.plugin.plugin.version>
<maven.filtering.version>3.2.0</maven.filtering.version>
<plexus.utils.version>3.0.23</plexus.utils.version>
<plexus.utils.version>3.3.1</plexus.utils.version>
<netty.version>4.1.77.Final</netty.version>
<maven.coveralls.plugin.version>4.3.0</maven.coveralls.plugin.version>
</properties>
Expand Down Expand Up @@ -57,11 +56,6 @@
<artifactId>maven-core</artifactId>
<version>${maven.project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>${maven.filtering.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interpolation</artifactId>
Expand All @@ -72,6 +66,11 @@
<artifactId>plexus-utils</artifactId>
<version>${plexus.utils.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
Expand Down

This file was deleted.

49 changes: 0 additions & 49 deletions asciidoctor-maven-plugin/src/it/resource-filtering/pom.xml

This file was deleted.

This file was deleted.

This file was deleted.

25 changes: 0 additions & 25 deletions asciidoctor-maven-plugin/src/it/resource-filtering/validate.groovy

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package org.asciidoctor.maven;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.MavenFilteringException;
import org.apache.maven.shared.filtering.MavenResourcesExecution;
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
import org.asciidoctor.*;
import org.asciidoctor.jruby.AsciidoctorJRuby;
import org.asciidoctor.jruby.internal.JRubyRuntimeContext;
import org.asciidoctor.maven.commons.AsciidoctorHelper;
import org.asciidoctor.maven.extensions.AsciidoctorJExtensionRegistry;
import org.asciidoctor.maven.extensions.ExtensionConfiguration;
import org.asciidoctor.maven.extensions.ExtensionRegistry;
import org.asciidoctor.maven.io.AsciidoctorFileScanner;
import org.asciidoctor.maven.log.LogHandler;
import org.asciidoctor.maven.log.LogRecordFormatter;
import org.asciidoctor.maven.log.LogRecordsProcessors;
import org.asciidoctor.maven.log.MemoryLogHandler;
import org.asciidoctor.maven.model.Resource;
import org.asciidoctor.maven.process.CopyResourcesProcessor;
import org.asciidoctor.maven.process.ResourcesProcessor;
import org.asciidoctor.maven.process.SourceDirectoryFinder;
import org.asciidoctor.maven.process.SourceDocumentFinder;
Expand All @@ -34,8 +30,8 @@
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static org.asciidoctor.maven.commons.StringUtils.isBlank;
import static org.asciidoctor.maven.process.SourceDirectoryFinder.DEFAULT_SOURCE_DIR;


Expand All @@ -45,9 +41,6 @@
@Mojo(name = "process-asciidoc", threadSafe = true)
public class AsciidoctorMojo extends AbstractMojo {

@Parameter(defaultValue = "${project.build.sourceEncoding}")
protected String encoding;

@Parameter(property = AsciidoctorMaven.PREFIX + "sourceDirectory", defaultValue = "${basedir}/" + DEFAULT_SOURCE_DIR)
protected File sourceDirectory;

Expand Down Expand Up @@ -139,17 +132,8 @@ public class AsciidoctorMojo extends AbstractMojo {
@Inject
protected MavenProject project;

@Inject
protected MavenSession session;

@Inject
protected MavenResourcesFiltering outputResourcesFiltering;

protected final ResourcesProcessor defaultResourcesProcessor =
(sourcesRootDirectory, outputRootDirectory, encoding, configuration) -> {
final List<Resource> finalResources = prepareResources(sourcesRootDirectory, configuration);
copyResources(finalResources, encoding, outputRootDirectory, outputResourcesFiltering, project, session);
};
protected final ResourcesProcessor defaultResourcesProcessor = new CopyResourcesProcessor();

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand Down Expand Up @@ -231,7 +215,7 @@ public void processSources(List<File> sourceFiles, ResourcesProcessor resourcesP

// Copy output resources
final File sourceDir = sourceDirectoryCandidate.get();
resourcesProcessor.process(sourceDir, outputDirectory, encoding, this);
resourcesProcessor.process(sourceDir, outputDirectory, this);

// register LogHandler to capture asciidoctor messages
final Boolean outputToConsole = logHandler.getOutputToConsole() == null ? Boolean.TRUE : logHandler.getOutputToConsole();
Expand Down Expand Up @@ -275,76 +259,20 @@ private boolean isRelativePath(String candidateName) {
return candidateName.startsWith("./") || candidateName.startsWith(".\\");
}

/**
* Initializes resources attribute excluding AsciiDoc documents, internal directories/files (those prefixed with
* underscore), and docinfo files.
* By default everything in the sources directories is copied.
*
* @return Collection of resources with properly configured includes and excludes conditions.
*/
private List<Resource> prepareResources(File sourceDirectory, AsciidoctorMojo configuration) {
final List<Resource> resources = configuration.getResources() != null
? configuration.getResources()
: new ArrayList<>();
if (resources.isEmpty()) {
// we don't want to copy files considered sources
Resource resource = new Resource();
resource.setDirectory(sourceDirectory.getAbsolutePath());
// exclude sourceDocumentName if defined
if (!isBlank(configuration.getSourceDocumentName())) {
resource.getExcludes()
.add(configuration.getSourceDocumentName());
}
// exclude filename extensions if defined
resources.add(resource);
}
public static List<org.apache.maven.model.Resource> mapResources(List<Resource> resources) {
if (resources == null || resources.isEmpty())
return Collections.emptyList();

// All resources must exclude AsciiDoc documents and folders beginning with underscore
for (Resource resource : resources) {
List<String> excludes = new ArrayList<>();
for (String value : AsciidoctorFileScanner.INTERNAL_FOLDERS_AND_FILES_PATTERNS) {
excludes.add(value);
}
for (String value : AsciidoctorFileScanner.IGNORED_FILE_NAMES) {
excludes.add("**/" + value);
}
for (String value : AsciidoctorFileScanner.DEFAULT_ASCIIDOC_EXTENSIONS) {
excludes.add(value);
}
for (String docExtension : configuration.getSourceDocumentExtensions()) {
resource.getExcludes().add("**/*." + docExtension);
}
// in case someone wants to include some of the default excluded files (e.g. AsciiDoc sources)
excludes.removeAll(resource.getIncludes());
resource.getExcludes().addAll(excludes);
}
return resources;
}

/**
* Copies the resources defined in the 'resources' attribute.
*
* @param resources Collection of {@link Resource} defining what resources to {@code outputDirectory}.
* @param encoding Files expected encoding.
* @param outputDirectory Directory where to copy resources.
* @param mavenResourcesFiltering Current {@link MavenResourcesFiltering} instance.
* @param mavenProject Current {@link MavenProject} instance.
* @param mavenSession Current {@link MavenSession} instance.
*/
private void copyResources(List<Resource> resources, String encoding, File outputDirectory,
MavenResourcesFiltering mavenResourcesFiltering, MavenProject mavenProject, MavenSession mavenSession) throws MojoExecutionException {
try {
// Right now "maven filtering" (replacements) is not officially supported, but could be used
MavenResourcesExecution resourcesExecution =
new MavenResourcesExecution(resources, outputDirectory, mavenProject, encoding,
Collections.emptyList(), Collections.emptyList(), mavenSession);
resourcesExecution.setIncludeEmptyDirs(true);
resourcesExecution.setAddDefaultExcludes(true);
mavenResourcesFiltering.filterResources(resourcesExecution);

} catch (MavenFilteringException e) {
throw new MojoExecutionException("Could not copy resources", e);
}
return resources.stream()
.map(mojoResource -> {
org.apache.maven.model.Resource resource = new org.apache.maven.model.Resource();
resource.setDirectory(mojoResource.getDirectory());
resource.setTargetPath(mojoResource.getTargetPath());
resource.setIncludes(mojoResource.getIncludes());
resource.setExcludes(mojoResource.getExcludes());
return resource;
})
.collect(Collectors.toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.asciidoctor.maven.io;

import org.apache.maven.model.Resource;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.Scanner;
import org.sonatype.plexus.build.incremental.BuildContext;

import java.io.File;
import java.util.*;
Expand Down Expand Up @@ -44,21 +44,15 @@ public class AsciidoctorFileScanner {
"*-docinfo-footer.xml"
};


private final BuildContext buildContext;

public AsciidoctorFileScanner(final BuildContext buildContext) {
this.buildContext = buildContext;
}

/**
* Scans a resource directory (and sub-subdirectories) returning all AsciiDoc documents found.
*
* @param resource {@link Resource} to scan (the directory property is mandatory)
* @return List of found documents matching the resource properties
*/
public List<File> scan(Resource resource) {
Scanner scanner = buildContext.newScanner(new File(resource.getDirectory()), true);
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(new File(resource.getDirectory()));
setupScanner(scanner, resource);
scanner.scan();
List<File> files = new ArrayList<>();
Expand Down

0 comments on commit 1f9bfe3

Please sign in to comment.