|
28 | 28 | import org.jboss.shrinkwrap.resolver.api.maven.ScopeType; |
29 | 29 | import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependencies; |
30 | 30 | import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency; |
31 | | -import org.jboss.shrinkwrap.resolver.api.maven.filter.MavenResolutionFilter; |
| 31 | +import org.jboss.shrinkwrap.resolver.api.maven.strategy.MavenResolutionStrategy; |
| 32 | +import org.jboss.shrinkwrap.resolver.api.maven.strategy.NonTransitiveStrategy; |
32 | 33 |
|
33 | 34 | /** |
34 | | - * An utility to apply pre and post filter on either a list of resolved dependencies (post filtering) or on a list of resolution |
35 | | - * candidates (pre filtering) |
| 35 | + * A utility to apply post filter on a list of resolved dependencies (post filtering). |
36 | 36 | * |
37 | 37 | * @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a> |
38 | 38 | * |
39 | 39 | */ |
40 | | -class PostResolutionFilterApplicator { |
41 | | - private static final Logger log = Logger.getLogger(PostResolutionFilterApplicator.class.getName()); |
| 40 | +class PostResolutionFilter { |
| 41 | + private static final Logger log = Logger.getLogger(PostResolutionFilter.class.getName()); |
42 | 42 |
|
43 | 43 | /** |
44 | | - * Run post-resolution filtering to weed out POMs. |
| 44 | + * Run post-resolution filtering to weed out unwanted POMs. |
45 | 45 | * |
46 | 46 | * @param artifactResults The original list of resolved artifacts |
| 47 | + * @param depsForResolution Resolutions for the request. Used for specifying unwanted POMs |
| 48 | + * @param strategy Resolution strategy |
47 | 49 | * @return List of modified artifactResults |
48 | 50 | */ |
49 | | - static Collection<MavenResolvedArtifact> postFilter(final Collection<MavenResolvedArtifact> artifactResults) { |
| 51 | + static Collection<MavenResolvedArtifact> filter(final Collection<MavenResolvedArtifact> artifactResults, List<MavenDependency> depsForResolution, final MavenResolutionStrategy strategy) { |
50 | 52 |
|
51 | | - final MavenResolutionFilter postResolutionFilter = RestrictPomArtifactFilter.INSTANCE; |
52 | | - final Collection<MavenResolvedArtifact> filteredArtifacts = new ArrayList<MavenResolvedArtifact>(); |
53 | | - final List<MavenDependency> emptyList = Collections.emptyList(); |
| 53 | + final Collection<MavenResolvedArtifact> filteredArtifacts = new ArrayList<>(); |
54 | 54 |
|
55 | 55 | for (final MavenResolvedArtifact artifact : artifactResults) { |
56 | 56 | final MavenDependency dependency = MavenDependencies.createDependency(artifact.getCoordinate(), |
57 | 57 | ScopeType.COMPILE, false); |
58 | 58 | // Empty lists OK here because we know the RestrictPOM Filter doesn't consult them |
59 | | - if (postResolutionFilter.accepts(dependency, emptyList, emptyList)) { |
| 59 | + if (PackagingType.POM.equals(dependency.getPackaging())) { |
| 60 | + if (log.isLoggable(Level.FINER)) { |
| 61 | + log.finer("Filtering out POM dependency resolution: " + dependency |
| 62 | + + "; its transitive dependencies will be included"); |
| 63 | + } |
| 64 | + // Keeping POM if specified in the resolution (G:A:pom:V) if only the POM should be resolved |
| 65 | + if (strategy.getClass().equals(NonTransitiveStrategy.class) && checkForPomInDependencies(dependency, depsForResolution)) { |
| 66 | + filteredArtifacts.add(artifact); |
| 67 | + } |
| 68 | + } |
| 69 | + else { |
60 | 70 | filteredArtifacts.add(artifact); |
61 | 71 | } |
62 | 72 | } |
63 | 73 | return Collections.unmodifiableCollection(filteredArtifacts); |
64 | 74 | } |
65 | 75 |
|
66 | | - /** |
67 | | - * {@link MavenResolutionFilter} implementation which does not allow POMs to pass through |
68 | | - * |
69 | | - * @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a> |
70 | | - */ |
71 | | - private enum RestrictPomArtifactFilter implements MavenResolutionFilter { |
72 | | - |
73 | | - INSTANCE; |
74 | | - |
75 | | - /** |
76 | | - * {@inheritDoc} |
77 | | - * |
78 | | - * @see org.jboss.shrinkwrap.resolver.api.maven.filter.MavenResolutionFilter#accepts(org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency, |
79 | | - * java.util.List, java.util.List) |
80 | | - */ |
81 | | - @Override |
82 | | - public boolean accepts(final MavenDependency coordinate, final List<MavenDependency> dependenciesForResolution, final List<MavenDependency> dependencyAncestors) |
83 | | - throws IllegalArgumentException { |
84 | | - if (PackagingType.POM.equals(coordinate.getPackaging())) { |
85 | | - if (log.isLoggable(Level.FINER)) { |
86 | | - log.finer("Filtering out POM dependency resolution: " + coordinate |
87 | | - + "; its transitive dependencies will be included"); |
88 | | - } |
89 | | - |
90 | | - return false; |
| 76 | + private static boolean checkForPomInDependencies(final MavenDependency coordinate, final List<MavenDependency> dependenciesForResolution) { |
| 77 | + for (MavenDependency dependency : dependenciesForResolution) { |
| 78 | + if (coordinate.getGroupId().equals(dependency.getGroupId()) |
| 79 | + && coordinate.getPackaging().equals(dependency.getPackaging()) |
| 80 | + && coordinate.getArtifactId().equals(dependency.getArtifactId()) |
| 81 | + && coordinate.getClassifier().equals(dependency.getClassifier())) { |
| 82 | + return true; |
91 | 83 | } |
92 | | - return true; |
93 | 84 | } |
94 | | - |
| 85 | + return false; |
95 | 86 | } |
96 | 87 |
|
97 | 88 | } |
0 commit comments