GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use try-with-resources by garydgregory · Pull Request #669 · apache/maven

Conversation

@garydgregory

@garydgregory garydgregory commented Jan 30, 2022

Copy link
Copy Markdown
Member

Trivial: Make sure resources are closed even when an exception is thrown by using try-with-resources.

  • I hereby declare this contribution to be licenced under the Apache License Version 2.0, January 2004

mvn clean package OK

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@garydgregory
Use try-with-resources.
22874de
slachiewicz
slachiewicz approved these changes Jan 30, 2022
View reviewed changes
gnodet
gnodet approved these changes Feb 1, 2022
View reviewed changes
olamy
olamy approved these changes Feb 1, 2022
View reviewed changes
@garydgregory

garydgregory commented Feb 5, 2022

Copy link
Copy Markdown
Member Author

ping :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Tibor17
Tibor17 self-requested a review February 5, 2022 19:13
Tibor17
Tibor17 reviewed Feb 5, 2022
View reviewed changes
Comment thread maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
InputStream is = pluginJar.getInputStream( pluginDescriptorEntry );

pluginDescriptor = parsePluginDescriptor( is, plugin, pluginFile.getAbsolutePath() );
try ( InputStream is = pluginJar.getInputStream( pluginDescriptorEntry ) )

@Tibor17 Tibor17 Feb 5, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is wrong.
The stream is closed twice.
The callers must not close the stream. It can be done only within the private method parsePluginDescriptor() and not here. The method parsePluginDescriptor() wraps the stream and so the Reader has to close it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

michael-o reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@michael-o michael-o Feb 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Agree here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@michael-o michael-o Feb 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In line 232 is the bug @Tibor17 has described.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gnodet gnodet Feb 6, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I tend to disagree because the Closeable#close() method is explicitely described as being idempotent. Else, the same applies to the call to builder.build() inside the parsePluginDescriptor which also closes the given stream.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@michael-o michael-o Feb 6, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@gnodet You are right. Let's make it consistently correct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Tibor17 Tibor17 Feb 6, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@gnodet
It is not about the low level of close method.
The problem is that the second close is redundant.
And the most important thing is that the straem is wrapped by Reader in the method parsePluginDescriptor() and the method should close the Reader. The caller is only dispatching the stream and that's the whole thing and the caller of the method parsePluginDescriptor() does not need to do anything about closing the nested stream. Why to do it twice, makes no sense!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@garydgregory garydgregory Feb 6, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The issue, from my POV, is that I think it is best for the method that allocates a resource to release it, and to so do "finally" with a try-with-resources statement. There are exceptions of course. What I personally do not favor is the style that I see here and there in Maven where one method allocates a stream and another method deep in the code path closes the stream; to me, this is an anti-pattern.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

michael-o reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@Tibor17 Tibor17 Feb 6, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@garydgregory
It does not matter that one method is producer and another is consumer. The reason is that both are private methods and still such private methods have to by analysed. The private methods only simplify the reading abilities of the code because human brain is not able to analyse whole inlined code. Of course you can parse pluginJar as a method parameter into the method parsePluginDescriptor() but in that case we are not talking only about try with resource but we are talking about refactoring shich is a subject of another PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
michael-o
michael-o requested changes Feb 5, 2022
View reviewed changes
Comment thread maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
InputStream is = pluginJar.getInputStream( pluginDescriptorEntry );

pluginDescriptor = parsePluginDescriptor( is, plugin, pluginFile.getAbsolutePath() );
try ( InputStream is = pluginJar.getInputStream( pluginDescriptorEntry ) )

@michael-o michael-o Feb 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Agree here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
InputStream is = pluginJar.getInputStream( pluginDescriptorEntry );

pluginDescriptor = parsePluginDescriptor( is, plugin, pluginFile.getAbsolutePath() );
try ( InputStream is = pluginJar.getInputStream( pluginDescriptorEntry ) )

@michael-o michael-o Feb 5, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In line 232 is the bug @Tibor17 has described.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Tibor17
Tibor17 requested changes Feb 6, 2022
View reviewed changes

@Tibor17 Tibor17 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The first commit should be fixed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@hboutemy
hboutemy force-pushed the master branch from a075afa to 6e50886 Compare March 24, 2024 21:16
@jira-importer

jira-importer commented Jul 6, 2025

Copy link
Copy Markdown

Resolve #8447

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@elharo

elharo commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

still in play or should this be closed?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gnodet
gnodet reviewed Jul 10, 2026
View reviewed changes

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Review: Use try-with-resources

This PR is over 4 years old and the codebase has evolved substantially. In its current form, it cannot merge — all file paths are stale and 2 of the 4 conversions target code that no longer exists.

Status of each conversion

File Status Details
ProjectBuildingException ❌ Obsolete createMessage() was rewritten to use StringBuilder — no more StringWriter/PrintWriter
DefaultMavenPluginManager ❌ Obsolete parsePluginDescriptor now takes a StreamSupplier instead of InputStream — stream lifecycle managed internally
LegacyRepositorySystem ✅ Still valid AuthenticationContext.close() pattern still exists at compat/maven-compat/... (new path)
MavenRepositorySystem ✅ Still valid AuthenticationContext.close() pattern still exists at impl/maven-core/... (new path)

Path

All four files were moved during the MNG-8346 reorganization (#1837):

  • maven-compat/... → compat/maven-compat/...
  • maven-core/... → impl/maven-core/...

The PR merge state is CONFLICTING.

Recommendation

Given the extent of changes needed (rebase, remove 2 obsolete files, update paths for the remaining 2), it may be simpler to close this PR and open a fresh one with only the two still-applicable AuthenticationContext try-with-resources conversions. The two surviving conversions are genuinely beneficial — they ensure authCtx.close() is called even if an authCtx.get() call throws.

The prior review discussion about double-closing (Tibor17, michael-o) is now moot since the affected methods have been rewritten.


This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of Guillaume Nodet

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
gnodet added a commit to gnodet/maven that referenced this pull request Jul 10, 2026
@gnodet @claude
chore: update PR review loop state
95f4e50
Reviewed 3 PRs: apache#11185 (namespace validation, needs modello approach),
apache#10906 (MSYS path fix, System.out.println + over-broad detection),
apache#669 (stale try-with-resources, recommend close). 21 reviews total.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
gnodet
gnodet reviewed Jul 25, 2026
View reviewed changes

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Code Review

This 4.5-year-old PR has good intent (try-with-resources for resource safety), but cannot merge in its current state.

Structural issues

1. All 4 file paths have moved (high)
MNG-8346 reorganized subprojects: maven-compat/ → compat/maven-compat/ and maven-core/ → impl/maven-core/. The PR targets paths that no longer exist. Merge state is CONFLICTING.

2. ProjectBuildingException conversion is obsolete (high)
The createMessage() method was rewritten to use StringBuilder — no more StringWriter/PrintWriter to wrap.

3. DefaultMavenPluginManager conversion is obsolete (high)
parsePluginDescriptor now takes a StreamSupplier instead of InputStream — stream lifecycle is managed internally by the builder.

Still valid

The 4 AuthenticationContext.close() patterns in LegacyRepositorySystem and MavenRepositorySystem are still valid at their new paths. AuthenticationContext implements Closeable, and the current code calls authCtx.close() manually without exception safety — if any authCtx.get() call throws, the context leaks.

Recommendation

Consider closing this PR and opening a fresh one targeting only the 4 AuthenticationContext patterns at the current file paths (compat/maven-compat/... and impl/maven-core/...).

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of gnodet

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
This was referenced Aug 26, 2026
Use try-with-resources for AuthenticationContext #12843
Merged
[4.0.x] Use try-with-resources for AuthenticationContext #12844
Merged
[3.10.x] Use try-with-resources #12845
Merged
[3.9.x] Use try-with-resources #12846
Merged
@slachiewicz

slachiewicz commented Aug 26, 2026

Copy link
Copy Markdown
Member

Thank you @garydgregory!

This PR has been superseded and adapted for all active branches:

  • master (4.1.x): Use try-with-resources for AuthenticationContext #12843
  • maven-4.0.x: [4.0.x] Use try-with-resources for AuthenticationContext #12844
  • maven-3.10.x: [3.10.x] Use try-with-resources #12845
  • maven-3.9.x: [3.9.x] Use try-with-resources #12846
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@slachiewicz slachiewicz closed this Aug 26, 2026
slachiewicz added a commit that referenced this pull request Aug 26, 2026
@slachiewicz
Use try-with-resources for AuthenticationContext
cee6a96
Closes #669
slachiewicz added a commit that referenced this pull request Aug 26, 2026
@slachiewicz
Use try-with-resources for AuthenticationContext
b273836
Closes #669
slachiewicz added a commit that referenced this pull request Aug 26, 2026
@slachiewicz
Use try-with-resources
3f12476
Closes #669
@slachiewicz slachiewicz added the maintenance label Aug 26, 2026
slachiewicz added a commit that referenced this pull request Aug 26, 2026
@slachiewicz
Use try-with-resources
6f49fdc
Closes #669
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gnodet gnodet gnodet approved these changes
@michael-o michael-o michael-o requested changes
@Tibor17 Tibor17 Tibor17 requested changes
@olamy olamy olamy approved these changes
@slachiewicz slachiewicz slachiewicz approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

maintenance

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@garydgregory @jira-importer @elharo @slachiewicz @olamy @gnodet @michael-o @Tibor17
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.