Skip to content

Commit 1e01c86

Browse files
committed
[MNG-8578]add exclusion scope to repo, allow exclude dependencies from build(including parent, dependencies, etc)
1 parent 37ea5a8 commit 1e01c86

File tree

5 files changed

+90
-31
lines changed

5 files changed

+90
-31
lines changed

api/maven-api-model/src/main/mdo/maven.mdo

+20
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,16 @@
603603
<multiplicity>*</multiplicity>
604604
</association>
605605
</field>
606+
<field>
607+
<name>exclusions</name>
608+
<version>4.0.0+</version>
609+
<description>Lists a set of artifacts that should be excluded from this repo's
610+
dependency list when it comes to calculating transitive dependencies.</description>
611+
<association>
612+
<type>Exclusion</type>
613+
<multiplicity>*</multiplicity>
614+
</association>
615+
</field>
606616
<field xdoc.separator="blank">
607617
<name>repositories</name>
608618
<version>4.0.0+</version>
@@ -1810,6 +1820,16 @@
18101820
</description>
18111821
<type>String</type>
18121822
</field>
1823+
<field>
1824+
<name>exclusions</name>
1825+
<version>4.0.0+</version>
1826+
<description>Lists a set of artifacts that should be excluded from this repo's
1827+
dependency list when it comes to calculating transitive dependencies.</description>
1828+
<association>
1829+
<type>Exclusion</type>
1830+
<multiplicity>*</multiplicity>
1831+
</association>
1832+
</field>
18131833
</fields>
18141834
<codeSegments>
18151835
<codeSegment>

impl/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java

+57-30
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Optional;
3030
import java.util.stream.Collectors;
3131

32+
import org.apache.maven.api.annotations.Nonnull;
33+
import org.apache.maven.api.annotations.Nullable;
3234
import org.apache.maven.artifact.handler.ArtifactHandler;
3335
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
3436
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
@@ -65,11 +67,13 @@
6567
*/
6668
public class RepositoryUtils {
6769

68-
private static String nullify(String string) {
70+
@Nullable
71+
private static String nullify(@Nullable String string) {
6972
return (string == null || string.isEmpty()) ? null : string;
7073
}
7174

72-
public static org.apache.maven.artifact.Artifact toArtifact(Dependency dependency) {
75+
@Nullable
76+
public static org.apache.maven.artifact.Artifact toArtifact(@Nullable Dependency dependency) {
7377
if (dependency == null) {
7478
return null;
7579
}
@@ -81,7 +85,8 @@ public static org.apache.maven.artifact.Artifact toArtifact(Dependency dependenc
8185
return result;
8286
}
8387

84-
public static org.apache.maven.artifact.Artifact toArtifact(Artifact artifact) {
88+
@Nullable
89+
public static org.apache.maven.artifact.Artifact toArtifact(@Nullable Artifact artifact) {
8590
if (artifact == null) {
8691
return null;
8792
}
@@ -112,10 +117,10 @@ public static org.apache.maven.artifact.Artifact toArtifact(Artifact artifact) {
112117
}
113118

114119
public static void toArtifacts(
115-
Collection<org.apache.maven.artifact.Artifact> artifacts,
116-
Collection<? extends DependencyNode> nodes,
117-
List<String> trail,
118-
DependencyFilter filter) {
120+
@Nonnull Collection<org.apache.maven.artifact.Artifact> artifacts,
121+
@Nonnull Collection<? extends DependencyNode> nodes,
122+
@Nonnull List<String> trail,
123+
@Nullable DependencyFilter filter) {
119124
for (DependencyNode node : nodes) {
120125
org.apache.maven.artifact.Artifact artifact = toArtifact(node.getDependency());
121126

@@ -132,7 +137,8 @@ public static void toArtifacts(
132137
}
133138
}
134139

135-
public static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
140+
@Nullable
141+
public static Artifact toArtifact(@Nullable org.apache.maven.artifact.Artifact artifact) {
136142
if (artifact == null) {
137143
return null;
138144
}
@@ -162,7 +168,8 @@ public static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
162168
}
163169

164170
public static Dependency toDependency(
165-
org.apache.maven.artifact.Artifact artifact, Collection<org.apache.maven.model.Exclusion> exclusions) {
171+
@Nullable org.apache.maven.artifact.Artifact artifact,
172+
@Nullable Collection<org.apache.maven.model.Exclusion> exclusions) {
166173
if (artifact == null) {
167174
return null;
168175
}
@@ -175,13 +182,15 @@ public static Dependency toDependency(
175182
return new Dependency(result, artifact.getScope(), artifact.isOptional(), excl);
176183
}
177184

178-
public static List<RemoteRepository> toRepos(List<ArtifactRepository> repos) {
185+
@Nonnull
186+
public static List<RemoteRepository> toRepos(@Nullable List<ArtifactRepository> repos) {
179187
return Optional.ofNullable(repos).orElse(Collections.emptyList()).stream()
180188
.map(RepositoryUtils::toRepo)
181189
.collect(Collectors.toList());
182190
}
183191

184-
public static RemoteRepository toRepo(ArtifactRepository repo) {
192+
@Nullable
193+
public static RemoteRepository toRepo(@Nullable ArtifactRepository repo) {
185194
RemoteRepository result = null;
186195
if (repo != null) {
187196
RemoteRepository.Builder builder =
@@ -197,7 +206,8 @@ public static RemoteRepository toRepo(ArtifactRepository repo) {
197206
return result;
198207
}
199208

200-
public static String getLayout(ArtifactRepository repo) {
209+
@Nonnull
210+
public static String getLayout(@Nonnull ArtifactRepository repo) {
201211
try {
202212
return repo.getLayout().getId();
203213
} catch (LinkageError e) {
@@ -216,15 +226,17 @@ public static String getLayout(ArtifactRepository repo) {
216226
}
217227
}
218228

219-
private static RepositoryPolicy toPolicy(ArtifactRepositoryPolicy policy) {
229+
@Nullable
230+
private static RepositoryPolicy toPolicy(@Nullable ArtifactRepositoryPolicy policy) {
220231
RepositoryPolicy result = null;
221232
if (policy != null) {
222233
result = new RepositoryPolicy(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy());
223234
}
224235
return result;
225236
}
226237

227-
private static Authentication toAuthentication(org.apache.maven.artifact.repository.Authentication auth) {
238+
@Nullable
239+
private static Authentication toAuthentication(@Nullable org.apache.maven.artifact.repository.Authentication auth) {
228240
Authentication result = null;
229241
if (auth != null) {
230242
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
@@ -235,7 +247,8 @@ private static Authentication toAuthentication(org.apache.maven.artifact.reposit
235247
return result;
236248
}
237249

238-
private static Proxy toProxy(org.apache.maven.repository.Proxy proxy) {
250+
@Nullable
251+
private static Proxy toProxy(@Nullable org.apache.maven.repository.Proxy proxy) {
239252
Proxy result = null;
240253
if (proxy != null) {
241254
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
@@ -245,7 +258,8 @@ private static Proxy toProxy(org.apache.maven.repository.Proxy proxy) {
245258
return result;
246259
}
247260

248-
public static ArtifactHandler newHandler(Artifact artifact) {
261+
@Nonnull
262+
public static ArtifactHandler newHandler(@Nonnull Artifact artifact) {
249263
String type = artifact.getProperty(ArtifactProperties.TYPE, artifact.getExtension());
250264
return new DefaultArtifactHandler(
251265
type,
@@ -258,7 +272,8 @@ public static ArtifactHandler newHandler(Artifact artifact) {
258272
Boolean.parseBoolean(artifact.getProperty(MavenArtifactProperties.CONSTITUTES_BUILD_PATH, "")));
259273
}
260274

261-
public static ArtifactType newArtifactType(String id, ArtifactHandler handler) {
275+
@Nonnull
276+
public static ArtifactType newArtifactType(@Nonnull String id, @Nonnull ArtifactHandler handler) {
262277
return new DefaultArtifactType(
263278
id,
264279
handler.getExtension(),
@@ -268,8 +283,9 @@ public static ArtifactType newArtifactType(String id, ArtifactHandler handler) {
268283
handler.isIncludesDependencies());
269284
}
270285

286+
@Nonnull
271287
public static Dependency toDependency(
272-
org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes) {
288+
@Nonnull org.apache.maven.model.Dependency dependency, @Nonnull ArtifactTypeRegistry stereotypes) {
273289
ArtifactType stereotype = stereotypes.get(dependency.getType());
274290
if (stereotype == null) {
275291
stereotype = new DefaultArtifactType(dependency.getType());
@@ -303,38 +319,46 @@ public static Dependency toDependency(
303319
exclusions);
304320
}
305321

306-
private static Exclusion toExclusion(org.apache.maven.model.Exclusion exclusion) {
322+
@Nonnull
323+
public static Exclusion toExclusion(@Nonnull org.apache.maven.model.Exclusion exclusion) {
307324
return new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*");
308325
}
309326

310-
public static ArtifactTypeRegistry newArtifactTypeRegistry(ArtifactHandlerManager handlerManager) {
327+
@Nonnull
328+
public static ArtifactTypeRegistry newArtifactTypeRegistry(@Nonnull ArtifactHandlerManager handlerManager) {
311329
return new MavenArtifactTypeRegistry(handlerManager);
312330
}
313331

314332
static class MavenArtifactTypeRegistry implements ArtifactTypeRegistry {
315333

334+
@Nonnull
316335
private final ArtifactHandlerManager handlerManager;
317336

318-
MavenArtifactTypeRegistry(ArtifactHandlerManager handlerManager) {
337+
MavenArtifactTypeRegistry(@Nonnull ArtifactHandlerManager handlerManager) {
319338
this.handlerManager = handlerManager;
320339
}
321340

322-
public ArtifactType get(String stereotypeId) {
341+
@Nullable
342+
@Override
343+
public ArtifactType get(@Nonnull String stereotypeId) {
323344
ArtifactHandler handler = handlerManager.getArtifactHandler(stereotypeId);
324345
return newArtifactType(stereotypeId, handler);
325346
}
326347
}
327348

328-
public static Collection<Artifact> toArtifacts(Collection<org.apache.maven.artifact.Artifact> artifactsToConvert) {
349+
@Nonnull
350+
public static Collection<Artifact> toArtifacts(
351+
@Nonnull Collection<org.apache.maven.artifact.Artifact> artifactsToConvert) {
329352
return artifactsToConvert.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toList());
330353
}
331354

332-
public static WorkspaceRepository getWorkspace(RepositorySystemSession session) {
355+
@Nullable
356+
public static WorkspaceRepository getWorkspace(@Nonnull RepositorySystemSession session) {
333357
WorkspaceReader reader = session.getWorkspaceReader();
334358
return (reader != null) ? reader.getRepository() : null;
335359
}
336360

337-
public static boolean repositoriesEquals(List<RemoteRepository> r1, List<RemoteRepository> r2) {
361+
public static boolean repositoriesEquals(@Nonnull List<RemoteRepository> r1, @Nonnull List<RemoteRepository> r2) {
338362
if (r1.size() != r2.size()) {
339363
return false;
340364
}
@@ -348,16 +372,19 @@ public static boolean repositoriesEquals(List<RemoteRepository> r1, List<RemoteR
348372
return true;
349373
}
350374

351-
public static int repositoriesHashCode(List<RemoteRepository> repositories) {
375+
public static int repositoriesHashCode(@Nonnull List<RemoteRepository> repositories) {
352376
int result = 17;
353377
for (RemoteRepository repository : repositories) {
354378
result = 31 * result + repositoryHashCode(repository);
355379
}
356380
return result;
357381
}
358382

383+
@Nullable
359384
public static RepositorySystemSession overlay(
360-
ArtifactRepository repository, RepositorySystemSession session, RepositorySystem system) {
385+
@Nullable ArtifactRepository repository,
386+
@Nullable RepositorySystemSession session,
387+
@Nonnull RepositorySystem system) {
361388
if (repository == null || repository.getBasedir() == null) {
362389
return session;
363390
}
@@ -379,22 +406,22 @@ public static RepositorySystemSession overlay(
379406
return newSession;
380407
}
381408

382-
private static int repositoryHashCode(RemoteRepository repository) {
409+
private static int repositoryHashCode(@Nonnull RemoteRepository repository) {
383410
int result = 17;
384411
Object obj = repository.getUrl();
385412
result = 31 * result + (obj != null ? obj.hashCode() : 0);
386413
return result;
387414
}
388415

389-
private static boolean policyEquals(RepositoryPolicy p1, RepositoryPolicy p2) {
416+
private static boolean policyEquals(@Nonnull RepositoryPolicy p1, @Nonnull RepositoryPolicy p2) {
390417
if (p1 == p2) {
391418
return true;
392419
}
393420
// update policy doesn't affect contents
394421
return p1.isEnabled() == p2.isEnabled() && Objects.equals(p1.getChecksumPolicy(), p2.getChecksumPolicy());
395422
}
396423

397-
private static boolean repositoryEquals(RemoteRepository r1, RemoteRepository r2) {
424+
private static boolean repositoryEquals(@Nonnull RemoteRepository r1, @Nonnull RemoteRepository r2) {
398425
if (r1 == r2) {
399426
return true;
400427
}

impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectDependenciesResolver.java

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ public DependencyResolutionResult resolve(DependencyResolutionRequest request)
148148
}
149149
}
150150

151+
List<Exclusion> exclusions = project.getExclusions();
152+
if (exclusions != null) {
153+
for (Exclusion exclusion : exclusions) {
154+
collect.addExclusion(RepositoryUtils.toExclusion(exclusion));
155+
}
156+
}
157+
151158
DependencyRequest depRequest = new DependencyRequest(collect, filter);
152159
depRequest.setTrace(trace);
153160

impl/maven-core/src/main/java/org/apache/maven/project/MavenProject.java

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.apache.maven.model.DependencyManagement;
5959
import org.apache.maven.model.Developer;
6060
import org.apache.maven.model.DistributionManagement;
61+
import org.apache.maven.model.Exclusion;
6162
import org.apache.maven.model.Extension;
6263
import org.apache.maven.model.IssueManagement;
6364
import org.apache.maven.model.License;
@@ -312,6 +313,10 @@ public DependencyManagement getDependencyManagement() {
312313
return getModel().getDependencyManagement();
313314
}
314315

316+
public List<Exclusion> getExclusions() {
317+
return getModel().getExclusions();
318+
}
319+
315320
// ----------------------------------------------------------------------
316321
// Test and compile source roots.
317322
// ----------------------------------------------------------------------

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ under the License.
162162
<plexusInterpolationVersion>1.27</plexusInterpolationVersion>
163163
<plexusTestingVersion>1.4.0</plexusTestingVersion>
164164
<plexusXmlVersion>4.0.4</plexusXmlVersion>
165-
<resolverVersion>2.0.6</resolverVersion>
165+
<resolverVersion>2.0.7.exclusion</resolverVersion>
166166
<securityDispatcherVersion>4.1.0</securityDispatcherVersion>
167167
<sisuVersion>0.9.0.M3</sisuVersion>
168168
<slf4jVersion>2.0.16</slf4jVersion>

0 commit comments

Comments
 (0)