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