You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/08/26 15:50:51 UTC
(maven) branch feature/maven-3.9.x-try-with-resources created (now 09939e68c5)
This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a change to branch feature/maven-3.9.x-try-with-resources
in repository https://gitbox.apache.org/repos/asf/maven.git
at 09939e68c5 Use try-with-resources
This branch includes the following new commits:
new 09939e68c5 Use try-with-resources
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(maven) 01/01: Use try-with-resources
Posted by sl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch feature/maven-3.9.x-try-with-resources
in repository https://gitbox.apache.org/repos/asf/maven.git
commit 09939e68c5f72e1281bf7d6e8944d93dba024682
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Wed Aug 26 17:46:19 2026 +0200
Use try-with-resources
Closes #669
---
.../repository/legacy/LegacyRepositorySystem.java | 27 +++++++++++-----------
.../apache/maven/bridge/MavenRepositorySystem.java | 27 +++++++++++-----------
.../plugin/internal/DefaultMavenPluginManager.java | 10 ++++----
.../maven/project/ProjectBuildingException.java | 8 ++-----
4 files changed, 34 insertions(+), 38 deletions(-)
diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java
index f7bc2d2259..677c0ac8cf 100644
--- a/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java
+++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java
@@ -533,13 +533,14 @@ private Authentication getAuthentication(RepositorySystemSession session, Artifa
repo = new RemoteRepository.Builder(repo)
.setAuthentication(auth)
.build();
- AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
- Authentication result = new Authentication(
- authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
- result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
- result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
- authCtx.close();
- return result;
+ try (AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo)) {
+ Authentication result = new Authentication(
+ authCtx.get(AuthenticationContext.USERNAME),
+ authCtx.get(AuthenticationContext.PASSWORD));
+ result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
+ result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
+ return result;
+ }
}
}
}
@@ -627,12 +628,12 @@ private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repos
repo = new RemoteRepository.Builder(repo)
.setProxy(proxy)
.build();
- AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
- p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
- p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
- p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
- p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
- authCtx.close();
+ try (AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo)) {
+ p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
+ p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
+ p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
+ p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
+ }
}
return p;
}
diff --git a/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java b/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
index 59a1b0ca70..5dfbff671a 100644
--- a/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
+++ b/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
@@ -230,13 +230,14 @@ private Authentication getAuthentication(RepositorySystemSession session, Artifa
repo = new RemoteRepository.Builder(repo)
.setAuthentication(auth)
.build();
- AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
- Authentication result = new Authentication(
- authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
- result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
- result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
- authCtx.close();
- return result;
+ try (AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo)) {
+ Authentication result = new Authentication(
+ authCtx.get(AuthenticationContext.USERNAME),
+ authCtx.get(AuthenticationContext.PASSWORD));
+ result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
+ result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
+ return result;
+ }
}
}
}
@@ -266,12 +267,12 @@ private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repos
repo = new RemoteRepository.Builder(repo)
.setProxy(proxy)
.build();
- AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
- p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
- p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
- p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
- p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
- authCtx.close();
+ try (AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo)) {
+ p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
+ p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
+ p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
+ p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
+ }
}
return p;
}
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
index 794e67ac57..af86d7ce84 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
@@ -216,9 +216,9 @@ private PluginDescriptor extractPluginDescriptor(Artifact pluginArtifact, Plugin
ZipEntry pluginDescriptorEntry = pluginJar.getEntry(getPluginDescriptorLocation());
if (pluginDescriptorEntry != null) {
- InputStream is = pluginJar.getInputStream(pluginDescriptorEntry);
-
- pluginDescriptor = parsePluginDescriptor(is, plugin, pluginFile.getAbsolutePath());
+ try (InputStream is = pluginJar.getInputStream(pluginDescriptorEntry)) {
+ pluginDescriptor = parsePluginDescriptor(is, plugin, pluginFile.getAbsolutePath());
+ }
}
}
} else {
@@ -258,9 +258,7 @@ private String getPluginDescriptorLocation() {
private PluginDescriptor parsePluginDescriptor(InputStream is, Plugin plugin, String descriptorLocation)
throws PluginDescriptorParsingException {
- try {
- Reader reader = ReaderFactory.newXmlReader(is);
-
+ try (Reader reader = ReaderFactory.newXmlReader(is)) {
PluginDescriptor pluginDescriptor = builder.build(reader, descriptorLocation);
return pluginDescriptor;
diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java
index 7665acc746..4e79b86e4d 100644
--- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java
+++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java
@@ -106,10 +106,8 @@ private static String createMessage(String message, String projectId, File pomFi
private static String createMessage(List<ProjectBuildingResult> results) {
StringWriter buffer = new StringWriter(1024);
- PrintWriter writer = new PrintWriter(buffer);
- writer.println("Some problems were encountered while processing the POMs:");
- try {
-
+ try (PrintWriter writer = new PrintWriter(buffer)) {
+ writer.println("Some problems were encountered while processing the POMs:");
for (ProjectBuildingResult result : results) {
for (ModelProblem problem : result.getProblems()) {
writer.print("[");
@@ -120,8 +118,6 @@ private static String createMessage(List<ProjectBuildingResult> results) {
writer.println(ModelProblemUtils.formatLocation(problem, result.getProjectId()));
}
}
- } finally {
- writer.close();
}
return buffer.toString();
}