package com.github.ferstl.jarscan;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Set;

import org.adoptopenjdk.jitwatch.jarscan.JarScan;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

@Mojo(
    name = "scan",
    aggregator = false,
    defaultPhase = LifecyclePhase.NONE,
    inheritByDefault = false,
    requiresDependencyCollection = ResolutionScope.TEST,
    requiresDependencyResolution = ResolutionScope.TEST,
    requiresDirectInvocation = false,
    threadSafe = true)
public class JarScanMojo extends AbstractMojo {

  @Component
  private MavenProject project;

  @Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    @SuppressWarnings("unchecked")
    Set<Artifact> dependencies = this.project.getDependencyArtifacts();
    for (Artifact dependency : dependencies) {
      try {
        System.out.println("Scanning " + dependency);
        JarScan.iterateJar(dependency.getFile(), 325, new PrintWriter(new OutputStreamWriter(System.out)));
        System.out.println("done: " + dependency);
        System.out.println();
      } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
      }
    }
  }

}

@@ -112,32 +112,83 @@
 io.PrintWriter;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import java.util
@@ -287,16 +287,149 @@
 tifact;%0A
+import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;%0Aimport org.apache.maven.artifact.resolver.filter.ArtifactFilter;%0A
 import o
@@ -734,24 +734,79 @@
 tions.Mojo;%0A
+import org.apache.maven.plugins.annotations.Parameter;%0A
 import org.a
@@ -899,16 +899,252 @@
 Project;
+%0Aimport org.apache.maven.shared.artifact.filter.ScopeArtifactFilter;%0Aimport org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;%0Aimport org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
 %0A%0A@Mojo(
@@ -1527,16 +1527,640 @@
 oject;%0A%0A
+  /**%0A   * The scope of the artifacts that should be included.%0A   *%0A   * @since 1.0.0%0A   */%0A  @Parameter(property = %22scope%22)%0A  private String scope;%0A%0A  /**%0A   * Comma-separated list of artifacts to be included in the form of %7B@code groupId:artifactId:type:classifier%7D.%0A   *%0A   * @since 1.0.0%0A   */%0A  @Parameter(property = %22includes%22, defaultValue = %22%22)%0A  private List%3CString%3E includes;%0A%0A  /**%0A   * Comma-separated list of artifacts to be excluded in the form of %7B@code groupId:artifactId:type:classifier%7D.%0A   *%0A   * @since 1.0.0%0A   */%0A  @Parameter(property = %22excludes%22, defaultValue = %22%22)%0A  private List%3CString%3E excludes;%0A%0A
   @Overr
@@ -2352,67 +2352,165 @@
 ();%0A
-    for (Artifact dependency : dependencies) %7B%0A      try %7B%0A
+%0A    ArtifactFilter filter = createArtifactFilter();%0A%0A    for (Artifact dependency : dependencies) %7B%0A      if (filter.include(dependency)) %7B%0A        try %7B%0A  
     
@@ -2551,32 +2551,34 @@
  + dependency);%0A
+  
         JarScan.
@@ -2669,32 +2669,34 @@
 out)));%0A        
+  
 System.out.print
@@ -2714,32 +2714,34 @@
  + dependency);%0A
+  
         System.o
@@ -2760,16 +2760,18 @@
 ;%0A      
+  
 %7D catch 
@@ -2784,24 +2784,26 @@
 eption e) %7B%0A
+  
         thro
@@ -2858,15 +2858,508 @@
     
-%7D%0A    %7D
+  %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  private ArtifactFilter createArtifactFilter() %7B%0A    List%3CArtifactFilter%3E filters = new ArrayList%3C%3E(3);%0A%0A    if (this.scope != null) %7B%0A      filters.add(new ScopeArtifactFilter(this.scope));%0A    %7D%0A%0A    if (!this.includes.isEmpty()) %7B%0A      filters.add(new StrictPatternIncludesArtifactFilter(this.includes));%0A    %7D%0A%0A    if (!this.excludes.isEmpty()) %7B%0A      filters.add(new StrictPatternExcludesArtifactFilter(this.excludes));%0A    %7D%0A%0A    return new AndArtifactFilter(filters);
 %0A  %7D

Add include/exclude/scope filtering