UnpackViewResources.java

  1. package org.jbehave.mojo;

  2. import java.io.File;
  3. import java.util.Arrays;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import java.util.stream.Collectors;

  7. import org.apache.maven.artifact.Artifact;
  8. import org.apache.maven.plugin.MojoExecutionException;
  9. import org.apache.maven.plugins.annotations.Component;
  10. import org.apache.maven.plugins.annotations.LifecyclePhase;
  11. import org.apache.maven.plugins.annotations.Mojo;
  12. import org.apache.maven.plugins.annotations.Parameter;
  13. import org.apache.maven.plugins.annotations.ResolutionScope;
  14. import org.apache.maven.project.MavenProject;
  15. import org.codehaus.plexus.archiver.UnArchiver;
  16. import org.codehaus.plexus.archiver.manager.ArchiverManager;
  17. import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
  18. import org.codehaus.plexus.util.StringUtils;
  19. import org.jbehave.core.embedder.Embedder;
  20. import org.jbehave.core.reporters.StoryReporterBuilder;

  21. /**
  22.  * Mojo to unpack resources to view directory, whose location is derived from
  23.  * the configured {@link StoryReporterBuilder} accessible from the {@link Embedder}.
  24.  */
  25. @Mojo(name = "unpack-view-resources", defaultPhase = LifecyclePhase.PROCESS_RESOURCES,
  26.         requiresDependencyResolution = ResolutionScope.TEST)
  27. public class UnpackViewResources extends AbstractEmbedderMojo {

  28.     @Component
  29.     ArchiverManager archiverManager;

  30.     @Parameter(defaultValue = "${project}", readonly = true, required = true)
  31.     MavenProject project;

  32.     @Parameter
  33.     String[] resourceArtifactIds = new String[] { "jbehave-site-resources", "jbehave-core" };

  34.     @Parameter
  35.     String[] resourceTypes = new String[] { "zip" };

  36.     @Parameter
  37.     String resourceIncludes;

  38.     @Parameter
  39.     String resourcesExcludes;

  40.     @Parameter
  41.     File viewDirectory;

  42.     @Override
  43.     public void execute() throws MojoExecutionException {
  44.         File destination = viewDirectory();
  45.         for (Artifact artifact : resourceArtifacts()) {
  46.             unpack(artifact.getFile(), destination, resourceIncludes, resourcesExcludes);
  47.         }
  48.     }

  49.     private File viewDirectory() {
  50.         if (viewDirectory != null) {
  51.             return viewDirectory;
  52.         }
  53.         StoryReporterBuilder storyReporterBuilder = newEmbedder().configuration().storyReporterBuilder();
  54.         String build = project.getBuild().getDirectory();
  55.         String output = storyReporterBuilder.outputDirectory().getName();
  56.         String view = storyReporterBuilder.viewResources().getProperty("viewDirectory");
  57.         return new File(build + "/" + output + "/" + view);
  58.     }

  59.     private Set<Artifact> resourceArtifacts() {
  60.         return allArtifacts().stream()
  61.                 .filter(artifact -> allowedBy("artifactId", artifact.getArtifactId(), resourceArtifactIds)
  62.                         && allowedBy("type", artifact.getType(), resourceTypes))
  63.                 .collect(Collectors.toSet());
  64.     }

  65.     private boolean allowedBy(String name, String property, String[] values) {
  66.         boolean allowed = false;
  67.         if (values.length > 0) {
  68.             for (String value : values) {
  69.                 if (property.equals(value)) {
  70.                     allowed = true;
  71.                     break;
  72.                 }
  73.             }
  74.         } else {
  75.             allowed = true;
  76.         }
  77.         if (!allowed) {
  78.             getLog().debug("Artifact property " + name + " not allowed by values " + Arrays.asList(values));
  79.         }
  80.         return allowed;
  81.     }

  82.     @SuppressWarnings("unchecked")
  83.     private Set<Artifact> allArtifacts() {
  84.         return new HashSet<Artifact>(project.getArtifacts());
  85.     }

  86.     private void unpack(File file, File destination, String includes, String excludes) throws MojoExecutionException {
  87.         try {
  88.             destination.mkdirs();

  89.             UnArchiver unArchiver = archiverManager.getUnArchiver(file);
  90.             unArchiver.setSourceFile(file);
  91.             unArchiver.setDestDirectory(destination);

  92.             if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
  93.                 IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] {
  94.                     new IncludeExcludeFileSelector() };
  95.                 if (StringUtils.isNotEmpty(excludes)) {
  96.                     selectors[0].setExcludes(excludes.split(","));
  97.                 }
  98.                 if (StringUtils.isNotEmpty(includes)) {
  99.                     selectors[0].setIncludes(includes.split(","));
  100.                 }
  101.                 unArchiver.setFileSelectors(selectors);
  102.             }

  103.             unArchiver.extract();

  104.             getLog().info("Unpacked " + file + " to " + destination);
  105.         } catch (Exception e) {
  106.             throw new MojoExecutionException("Failed unpacking " + file + " to " + destination, e);
  107.         }
  108.     }

  109. }