/
RequireReleaseDeps.java
244 lines (213 loc) · 7.18 KB
/
RequireReleaseDeps.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package org.apache.maven.plugins.enforcer;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.enforcer.utils.ArtifactUtils;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
/**
* This rule checks that no snapshots are included.
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
public class RequireReleaseDeps
extends AbstractBanDependencies
{
/**
* Allows this rule to execute only when this project is a release.
*
* @see {@link #setOnlyWhenRelease(boolean)}
* @see {@link #isOnlyWhenRelease()}
*/
private boolean onlyWhenRelease = false;
/**
* Allows this rule to fail when the parent is defined as a snapshot.
*
* @see {@link #setFailWhenParentIsSnapshot(boolean)}
* @see {@link #isFailWhenParentIsSnapshot()}
*/
private boolean failWhenParentIsSnapshot = true;
/**
* Dependencies to ignore when checking for release versions. For example, inter-module dependencies
* can be excluded from the check and therefore allowed to contain snapshot versions.
*
* @see {@link #setExcludes(List)}
* @see {@link #getExcludes()}
*/
private List<String> excludes = null;
/**
* Dependencies to include when checking for release versions. If any of the included dependencies
* have snapshot versions, the rule will fail.
*
* @see {@link #setIncludes(List)}
* @see {@link #getIncludes()}
*/
private List<String> includes = null;
// Override parent to allow optional ignore of this rule.
@Override
public void execute( EnforcerRuleHelper helper )
throws EnforcerRuleException
{
boolean callSuper;
MavenProject project = null;
if ( onlyWhenRelease )
{
// get the project
project = getProject( helper );
// only call super if this project is a release
callSuper = !project.getArtifact().isSnapshot();
}
else
{
callSuper = true;
}
if ( callSuper )
{
super.execute( helper );
if ( failWhenParentIsSnapshot )
{
if ( project == null )
{
project = getProject( helper );
}
Artifact parentArtifact = project.getParentArtifact();
if ( parentArtifact != null )
{
Set<Artifact> artifacts = filterArtifacts( Collections.singleton( parentArtifact ) );
parentArtifact = Optional.ofNullable( artifacts )
.flatMap( s -> s.stream().findFirst() )
.orElse( null );
}
if ( parentArtifact != null && parentArtifact.isSnapshot() )
{
throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
}
}
}
}
/**
* @param helper
* @return The evaluated {@link MavenProject}.
* @throws EnforcerRuleException
*/
private MavenProject getProject( EnforcerRuleHelper helper )
throws EnforcerRuleException
{
try
{
return (MavenProject) helper.evaluate( "${project}" );
}
catch ( ExpressionEvaluationException eee )
{
throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
}
}
@Override
protected Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log )
throws EnforcerRuleException
{
Set<Artifact> foundSnapshots = new HashSet<>();
Set<Artifact> filteredDependencies = filterArtifacts( dependencies );
if ( filteredDependencies != null )
{
for ( Artifact artifact : filteredDependencies )
{
if ( artifact.isSnapshot() )
{
foundSnapshots.add( artifact );
}
}
}
return foundSnapshots;
}
/*
* Filter the dependency artifacts according to the includes and excludes
* If includes and excludes are both null, the original set is returned.
*
* @param dependencies the list of dependencies to filter
* @return the resulting set of dependencies
*/
protected Set<Artifact> filterArtifacts( Set<Artifact> dependencies ) throws EnforcerRuleException
{
if ( includes == null && excludes == null )
{
return dependencies;
}
Set<Artifact> included;
if ( includes != null )
{
included = ArtifactUtils.checkDependencies( dependencies, includes );
}
else
{
included = new HashSet<>( dependencies );
}
// anything specifically included should be removed
// from the ban list.
if ( included != null )
{
Set<Artifact> excluded = ArtifactUtils.checkDependencies( dependencies, excludes );
if ( excluded != null )
{
included.removeAll( excluded );
}
}
return included;
}
public final boolean isOnlyWhenRelease()
{
return onlyWhenRelease;
}
public final void setOnlyWhenRelease( boolean onlyWhenRelease )
{
this.onlyWhenRelease = onlyWhenRelease;
}
public final boolean isFailWhenParentIsSnapshot()
{
return failWhenParentIsSnapshot;
}
public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
{
this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
}
public final void setExcludes( List<String> excludes )
{
this.excludes = excludes;
}
public final List<String> getExcludes()
{
return excludes;
}
public void setIncludes( List<String> includes )
{
this.includes = includes;
}
public List<String> getIncludes()
{
return includes;
}
}