1 | |
package org.apache.maven.plugin.assembly.utils; |
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.artifact.resolver.filter.AndArtifactFilter; |
24 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
25 | |
import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException; |
26 | |
import org.apache.maven.project.MavenProject; |
27 | |
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter; |
28 | |
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter; |
29 | |
import org.apache.maven.shared.artifact.filter.StatisticsReportingArtifactFilter; |
30 | |
import org.codehaus.plexus.logging.Logger; |
31 | |
|
32 | |
import java.util.ArrayList; |
33 | |
import java.util.Collection; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.List; |
36 | |
import java.util.Set; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public final class FilterUtils |
42 | |
{ |
43 | |
|
44 | |
private FilterUtils() |
45 | 0 | { |
46 | 0 | } |
47 | |
|
48 | |
public static void filterProjects( final Set<MavenProject> projects, final List<String> includes, |
49 | |
final List<String> excludes, final boolean actTransitively, final Logger logger ) |
50 | |
{ |
51 | 16 | final List<PatternIncludesArtifactFilter> allFilters = new ArrayList<PatternIncludesArtifactFilter>(); |
52 | |
|
53 | 16 | final AndArtifactFilter filter = new AndArtifactFilter(); |
54 | |
|
55 | 16 | if ( !includes.isEmpty() ) |
56 | |
{ |
57 | 5 | final PatternIncludesArtifactFilter includeFilter = |
58 | |
new PatternIncludesArtifactFilter( includes, actTransitively ); |
59 | |
|
60 | 5 | filter.add( includeFilter ); |
61 | 5 | allFilters.add( includeFilter ); |
62 | |
} |
63 | 16 | if ( !excludes.isEmpty() ) |
64 | |
{ |
65 | 4 | final PatternExcludesArtifactFilter excludeFilter = |
66 | |
new PatternExcludesArtifactFilter( excludes, actTransitively ); |
67 | |
|
68 | 4 | filter.add( excludeFilter ); |
69 | 4 | allFilters.add( excludeFilter ); |
70 | |
} |
71 | |
|
72 | 16 | for ( final Iterator<MavenProject> it = projects.iterator(); it.hasNext(); ) |
73 | |
{ |
74 | 21 | final MavenProject project = it.next(); |
75 | 21 | final Artifact artifact = project.getArtifact(); |
76 | |
|
77 | 21 | if ( !filter.include( artifact ) ) |
78 | |
{ |
79 | 9 | it.remove(); |
80 | |
} |
81 | 21 | } |
82 | |
|
83 | 16 | for ( final Iterator<PatternIncludesArtifactFilter> it = allFilters.iterator(); it.hasNext(); ) |
84 | |
{ |
85 | 9 | final ArtifactFilter f = it.next(); |
86 | |
|
87 | 9 | if ( f instanceof StatisticsReportingArtifactFilter ) |
88 | |
{ |
89 | 9 | ( (StatisticsReportingArtifactFilter) f ).reportMissedCriteria( logger ); |
90 | |
} |
91 | 9 | } |
92 | 16 | } |
93 | |
|
94 | |
public static void filterArtifacts( final Set<Artifact> artifacts, final List<String> includes, |
95 | |
final List<String> excludes, final boolean strictFiltering, |
96 | |
final boolean actTransitively, final Logger logger, |
97 | |
final ArtifactFilter... additionalFilters ) |
98 | |
throws InvalidAssemblerConfigurationException |
99 | |
{ |
100 | 21 | final List<ArtifactFilter> allFilters = new ArrayList<ArtifactFilter>(); |
101 | |
|
102 | 21 | final AndArtifactFilter filter = new AndArtifactFilter(); |
103 | |
|
104 | 21 | if ( additionalFilters != null && additionalFilters.length > 0 ) |
105 | |
{ |
106 | 40 | for ( final ArtifactFilter additionalFilter : additionalFilters ) |
107 | |
{ |
108 | 20 | if ( additionalFilter != null ) |
109 | |
{ |
110 | 12 | filter.add( additionalFilter ); |
111 | |
} |
112 | |
} |
113 | |
} |
114 | |
|
115 | 21 | if ( !includes.isEmpty() ) |
116 | |
{ |
117 | 7 | final ArtifactFilter includeFilter = new PatternIncludesArtifactFilter( includes, actTransitively ); |
118 | |
|
119 | 7 | filter.add( includeFilter ); |
120 | |
|
121 | 7 | allFilters.add( includeFilter ); |
122 | |
} |
123 | |
|
124 | 21 | if ( !excludes.isEmpty() ) |
125 | |
{ |
126 | 3 | final ArtifactFilter excludeFilter = new PatternExcludesArtifactFilter( excludes, actTransitively ); |
127 | |
|
128 | 3 | filter.add( excludeFilter ); |
129 | |
|
130 | 3 | allFilters.add( excludeFilter ); |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | 21 | for ( final Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); ) |
140 | |
{ |
141 | 22 | final Artifact artifact = it.next(); |
142 | |
|
143 | 22 | if ( !filter.include( artifact ) ) |
144 | |
{ |
145 | 7 | it.remove(); |
146 | |
|
147 | 7 | if ( logger.isDebugEnabled() ) |
148 | |
{ |
149 | 7 | logger.debug( artifact.getId() + " was removed by one or more filters." ); |
150 | |
} |
151 | |
} |
152 | 22 | } |
153 | |
|
154 | 21 | reportFilteringStatistics( allFilters, logger ); |
155 | |
|
156 | 21 | for ( final Iterator<ArtifactFilter> it = allFilters.iterator(); it.hasNext(); ) |
157 | |
{ |
158 | 10 | final ArtifactFilter f = it.next(); |
159 | |
|
160 | 10 | if ( f instanceof StatisticsReportingArtifactFilter ) |
161 | |
{ |
162 | 10 | final StatisticsReportingArtifactFilter sFilter = (StatisticsReportingArtifactFilter) f; |
163 | |
|
164 | 10 | if ( strictFiltering && sFilter.hasMissedCriteria() ) |
165 | |
{ |
166 | 1 | throw new InvalidAssemblerConfigurationException( |
167 | |
"One or more filters had unmatched criteria. Check debug log for more information." ); |
168 | |
} |
169 | |
} |
170 | 9 | } |
171 | 20 | } |
172 | |
|
173 | |
public static void reportFilteringStatistics( final Collection<ArtifactFilter> filters, final Logger logger ) |
174 | |
{ |
175 | 21 | for ( final Iterator<ArtifactFilter> it = filters.iterator(); it.hasNext(); ) |
176 | |
{ |
177 | 10 | final ArtifactFilter f = it.next(); |
178 | |
|
179 | 10 | if ( f instanceof StatisticsReportingArtifactFilter ) |
180 | |
{ |
181 | 10 | final StatisticsReportingArtifactFilter sFilter = (StatisticsReportingArtifactFilter) f; |
182 | |
|
183 | 10 | if ( logger.isDebugEnabled() ) |
184 | |
{ |
185 | 10 | logger.debug( "Statistics for " + sFilter + "\n" ); |
186 | |
} |
187 | |
|
188 | 10 | sFilter.reportMissedCriteria( logger ); |
189 | 10 | sFilter.reportFilteredArtifacts( logger ); |
190 | |
} |
191 | 10 | } |
192 | 21 | } |
193 | |
|
194 | |
} |