Visitar URL original
Add ignoreOptionals option to bannedDependencies and requireReleaseDeps by slawekjaranowski · Pull Request #1078 · apache/maven-enforcer · GitHub
Skip to content

Add ignoreOptionals option to bannedDependencies and requireReleaseDeps - #1078

Open
slawekjaranowski wants to merge 2 commits into
apache:masterfrom
slawekjaranowski:1077-banned-dependencies-ignore-optionals
Open

slawekjaranowski wants to merge 2 commits into
apache:masterfrom
slawekjaranowski:1077-banned-dependencies-ignore-optionals

Conversation

@slawekjaranowski

Copy link
Copy Markdown
Member

Since 3.7.0, bannedDependencies (and requireReleaseDeps) also check direct optional dependencies
and their transitive dependencies. This restores the behavior lost in 3.3.0, but builds that passed
with 3.3.x–3.6.x can now fail, e.g. with an exclude like *:*:*:jar:compile. There was no way
to opt out, because include patterns cannot match optional dependencies.

This PR adds a new ignoreOptionals parameter to BannedDependenciesBase, so it is available
for both bannedDependencies and requireReleaseDeps. Default is false, so the 3.7.0 behavior is kept.

When ignoreOptionals is true:

  • with searchTransitive=true (default): direct optional dependencies of the project are not collected,
    so they and their transitive dependencies are not checked
  • with searchTransitive=false: direct optional dependencies are skipped

Closes #1077


To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
check one of the following:

🤖 Generated with Claude Code

Closes apache#1077

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@slawekjaranowski slawekjaranowski self-assigned this Nov 7, 2026
@slawekjaranowski slawekjaranowski added the enhancement New feature or request label Nov 7, 2026

@ascheman ascheman left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — this closes a real gap, and thanks for handling the release-notes half separately.

I checked the thing that worried me, and it holds: the flag means the same thing in both modes. searchTransitive=false filters getDependencyArtifacts() with !(ignoreOptionals && a.isOptional()), and searchTransitive=true hands it to ResolverUtil.resolveTransitiveDependencies(…, excludeOptional, …), which filters project.getDependencies() with the identical predicate. Same scope — direct dependencies of the project — so there is no mode where the parameter surprises you. Default false keeps the 3.7.0 behaviour, which seems right.

I also like that banned-dependencies-fail was extended to assert that the transitive of an optional is banned. That pins the 3.7.0 behaviour which caused #1077, so a regression in either direction gets caught.

Verified locally on d4ec185: enforcer-rules green at 303 tests.

One non-blocking suggestion: the new banned-dependencies-ignore-optionals IT has no assertion. It contains only pom.xml — no verify.groovy (71 of the 117 IT projects have one, and its sibling banned-dependencies-fail does). It does discriminate on the feature, since a broken ignoreOptionals would ban menforcer138_io and fail the build; but it passes on "build succeeded", so it would also pass if the enforcer execution never ran at all. Two lines would close that:

def buildLog = new File( basedir, 'build.log' ).text
assert !buildLog.contains( 'banned via the exclude/include list' )

And two cosmetic notes, take or leave:

  • The rule parameter is ignoreOptionals while the resolver method it delegates to calls the same concept excludeOptional — two names for one thing in the same package.
  • The site docs say "optional dependencies of the project (and their transitive dependencies)"; the PR description more precisely says direct optional dependencies. Adding "direct" would stop a reader expecting optionals to be skipped anywhere in the tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@slawekjaranowski

Copy link
Copy Markdown
Member Author

Thanks for the review!

  • Added verify.groovy to banned-dependencies-ignore-optionals. It asserts that BannedDependencies ran and passed, and that nothing was reported as banned.
  • Docs and Javadoc now say direct optional dependencies.
  • Naming: I kept ignoreOptionals because enforceBytecodeVersion uses the same name for this concept. excludeOptional in ResolverUtil is internal and shared with other rules, so I'd rather not rename it in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document bannedDependencies behavior change in 3.7.0 and provide an opt-out for optional dependencies

2 participants