1 package org.apache.maven.plugin.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
25 import org.apache.maven.surefire.api.testset.TestFilter;
26 import org.apache.maven.surefire.api.testset.TestListResolver;
27 import org.apache.maven.surefire.api.util.DefaultScanResult;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Enumeration;
34 import java.util.LinkedHashSet;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.jar.JarEntry;
38 import java.util.jar.JarFile;
39
40 import static org.apache.maven.plugin.surefire.util.ScannerUtil.convertJarFileResourceToJavaClassName;
41 import static org.apache.maven.plugin.surefire.util.ScannerUtil.isJavaClassFile;
42
43
44
45
46
47
48 public class DependencyScanner
49 {
50 private final List<File> dependenciesToScan;
51
52 private final TestListResolver filter;
53
54 public DependencyScanner( List<File> dependenciesToScan, TestListResolver filter )
55 {
56 this.dependenciesToScan = dependenciesToScan;
57 this.filter = filter;
58 }
59
60 public DefaultScanResult scan()
61 throws MojoExecutionException
62 {
63 Set<String> classes = new LinkedHashSet<>();
64 for ( File artifact : dependenciesToScan )
65 {
66 if ( artifact != null && artifact.isFile() && artifact.getName().endsWith( ".jar" ) )
67 {
68 try
69 {
70 scanArtifact( artifact, filter, classes );
71 }
72 catch ( IOException e )
73 {
74 throw new MojoExecutionException( "Could not scan dependency " + artifact.toString(), e );
75 }
76 }
77 }
78 return new DefaultScanResult( new ArrayList<>( classes ) );
79 }
80
81 private static void scanArtifact( File artifact, TestFilter<String, String> filter, Set<String> classes )
82 throws IOException
83 {
84 try ( JarFile jar = new JarFile( artifact ) )
85 {
86 for ( Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); )
87 {
88 JarEntry entry = entries.nextElement();
89 String path = entry.getName();
90 if ( !entry.isDirectory() && isJavaClassFile( path ) && filter.shouldRun( path, null ) )
91 {
92 classes.add( convertJarFileResourceToJavaClassName( path ) );
93 }
94 }
95 }
96 }
97
98
99
100
101
102
103
104
105
106 public static List<Artifact> filter( List<Artifact> artifacts, List<String> artifactPatterns )
107 {
108 if ( artifactPatterns == null || artifacts == null || artifacts.isEmpty() )
109 {
110 return Collections.emptyList();
111 }
112
113 PatternIncludesArtifactFilter artifactFilter = new PatternIncludesArtifactFilter( artifactPatterns );
114
115 List<Artifact> matches = new ArrayList<>();
116 for ( Artifact artifact : artifacts )
117 {
118 if ( artifactFilter.include( artifact ) )
119 {
120 matches.add( artifact );
121 }
122 }
123 return matches;
124 }
125 }