1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
28 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
30 import org.apache.maven.plugin.logging.Log;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
33 import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
34 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
35
36
37
38
39
40
41
42 public class RequireReleaseDeps
43 extends AbstractBanDependencies
44 {
45
46
47
48
49
50
51
52
53
54
55 private boolean onlyWhenRelease = false;
56
57
58
59
60
61
62
63
64
65 private boolean failWhenParentIsSnapshot = true;
66
67
68
69
70
71
72
73
74 private List<String> excludes = null;
75
76
77
78
79
80
81
82
83 private List<String> includes = null;
84
85
86
87
88
89
90
91 public void execute( EnforcerRuleHelper helper )
92 throws EnforcerRuleException
93 {
94 boolean callSuper;
95 MavenProject project = null;
96 if ( onlyWhenRelease )
97 {
98
99 project = getProject( helper );
100
101
102 callSuper = !project.getArtifact().isSnapshot();
103 }
104 else
105 {
106 callSuper = true;
107 }
108 if ( callSuper )
109 {
110 super.execute( helper );
111 if ( failWhenParentIsSnapshot )
112 {
113 if ( project == null )
114 {
115 project = getProject( helper );
116 }
117 Artifact parentArtifact = project.getParentArtifact();
118 if ( parentArtifact != null && parentArtifact.isSnapshot() )
119 {
120 throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
121 }
122 }
123 }
124 }
125
126
127
128
129
130
131 private MavenProject getProject( EnforcerRuleHelper helper )
132 throws EnforcerRuleException
133 {
134 try
135 {
136 return (MavenProject) helper.evaluate( "${project}" );
137 }
138 catch ( ExpressionEvaluationException eee )
139 {
140 throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
141 }
142 }
143
144
145
146
147
148 protected Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log )
149 throws EnforcerRuleException
150 {
151 Set<Artifact> foundSnapshots = new HashSet<Artifact>();
152
153 Set<Artifact> filteredDependencies = filterArtifacts( dependencies );
154
155 for ( Artifact artifact : filteredDependencies )
156 {
157 if ( artifact.isSnapshot() )
158 {
159 foundSnapshots.add( artifact );
160 }
161 }
162
163 return foundSnapshots;
164 }
165
166
167
168
169
170
171
172
173 public Set<Artifact> filterArtifacts( Set<Artifact> dependencies )
174 {
175 if ( includes == null && excludes == null )
176 {
177 return dependencies;
178 }
179
180 AndArtifactFilter filter = new AndArtifactFilter( );
181 if ( includes != null )
182 {
183 filter.add( new StrictPatternIncludesArtifactFilter( includes ) );
184 }
185 if ( excludes != null )
186 {
187 filter.add( new StrictPatternExcludesArtifactFilter( excludes ) );
188 }
189
190 Set<Artifact> result = new HashSet<Artifact>();
191 for ( Artifact artifact : dependencies )
192 {
193 if ( filter.include( artifact ) )
194 {
195 result.add( artifact );
196 }
197 }
198 return result;
199 }
200
201 public final boolean isOnlyWhenRelease()
202 {
203 return onlyWhenRelease;
204 }
205
206 public final void setOnlyWhenRelease( boolean onlyWhenRelease )
207 {
208 this.onlyWhenRelease = onlyWhenRelease;
209 }
210
211 public final boolean isFailWhenParentIsSnapshot()
212 {
213 return failWhenParentIsSnapshot;
214 }
215
216 public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
217 {
218 this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
219 }
220
221 public final void setExcludes( List<String> excludes )
222 {
223 this.excludes = excludes;
224 }
225
226 public final List<String> getExcludes()
227 {
228 return excludes;
229 }
230
231 public void setIncludes( List<String> includes )
232 {
233 this.includes = includes;
234 }
235
236 public List<String> getIncludes()
237 {
238 return includes;
239 }
240 }