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 public boolean onlyWhenRelease = false;
52
53
54
55
56
57
58 public boolean failWhenParentIsSnapshot = true;
59
60
61
62
63
64 public List<String> excludes = null;
65
66
67
68
69
70 public List<String> includes = null;
71
72
73
74
75 public void execute( EnforcerRuleHelper helper )
76 throws EnforcerRuleException
77 {
78 boolean callSuper;
79 MavenProject project = null;
80 if ( onlyWhenRelease )
81 {
82
83 project = getProject( helper );
84
85
86 callSuper = !project.getArtifact().isSnapshot();
87 }
88 else
89 {
90 callSuper = true;
91 }
92 if ( callSuper )
93 {
94 super.execute( helper );
95 if ( failWhenParentIsSnapshot )
96 {
97 if ( project == null )
98 {
99 project = getProject( helper );
100 }
101 Artifact parentArtifact = project.getParentArtifact();
102 if ( parentArtifact != null && parentArtifact.isSnapshot() )
103 {
104 throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
105 }
106 }
107 }
108 }
109
110
111
112
113
114
115 private MavenProject getProject( EnforcerRuleHelper helper )
116 throws EnforcerRuleException
117 {
118 try
119 {
120 return (MavenProject) helper.evaluate( "${project}" );
121 }
122 catch ( ExpressionEvaluationException eee )
123 {
124 throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
125 }
126 }
127
128
129
130
131
132 protected Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log )
133 throws EnforcerRuleException
134 {
135 Set<Artifact> foundSnapshots = new HashSet<Artifact>();
136
137 Set<Artifact> filteredDependencies = filterArtifacts( dependencies );
138
139 for ( Artifact artifact : filteredDependencies )
140 {
141 if ( artifact.isSnapshot() )
142 {
143 foundSnapshots.add( artifact );
144 }
145 }
146
147 return foundSnapshots;
148 }
149
150
151
152
153
154
155
156
157 public Set<Artifact> filterArtifacts( Set<Artifact> dependencies )
158 {
159 if ( includes == null && excludes == null )
160 {
161 return dependencies;
162 }
163
164 AndArtifactFilter filter = new AndArtifactFilter( );
165 if ( includes != null )
166 {
167 filter.add( new StrictPatternIncludesArtifactFilter( includes ) );
168 }
169 if ( excludes != null )
170 {
171 filter.add( new StrictPatternExcludesArtifactFilter( excludes ) );
172 }
173
174 Set<Artifact> result = new HashSet<Artifact>();
175 for ( Artifact artifact : dependencies )
176 {
177 if ( filter.include( artifact ) )
178 {
179 result.add( artifact );
180 }
181 }
182 return result;
183 }
184
185 public boolean isOnlyWhenRelease()
186 {
187 return onlyWhenRelease;
188 }
189
190 public void setOnlyWhenRelease( boolean onlyWhenRelease )
191 {
192 this.onlyWhenRelease = onlyWhenRelease;
193 }
194
195 public boolean isFailWhenParentIsSnapshot()
196 {
197 return failWhenParentIsSnapshot;
198 }
199
200 public void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
201 {
202 this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
203 }
204 }