-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Introduce TempFileService and lifecycle cleanup participant for Maven 4. #11389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
api/maven-api-core/src/main/java/org/apache/maven/api/services/TempFileService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.maven.api.services; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.apache.maven.api.Service; | ||
| import org.apache.maven.api.Session; | ||
| import org.apache.maven.api.annotations.Nonnull; | ||
|
|
||
| /** | ||
| * Service to create and track temporary files/directories for a Maven build. | ||
| * All created paths are deleted automatically when the session ends. | ||
| */ | ||
| public interface TempFileService extends Service { | ||
|
|
||
| /** | ||
| * Creates a temp file in the default temp directory. | ||
| */ | ||
| @Nonnull | ||
| Path createTempFile(Session session, String prefix, String suffix) throws IOException; | ||
|
|
||
| /** | ||
| * Creates a temp file in the given directory. | ||
| */ | ||
| @Nonnull | ||
| Path createTempFile(Session session, String prefix, String suffix, Path directory) throws IOException; | ||
|
|
||
| /** | ||
| * Creates a temp directory in the default temp directory. | ||
| */ | ||
| @Nonnull | ||
| Path createTempDirectory(Session session, String prefix) throws IOException; | ||
|
|
||
| /** | ||
| * Creates a temp directory in the given directory. | ||
| */ | ||
| @Nonnull | ||
| Path createTempDirectory(Session session, String prefix, Path directory) throws IOException; | ||
|
|
||
| /** | ||
| * Registers an externally created path for cleanup at session end. | ||
| */ | ||
| @Nonnull | ||
| void register(Session session, Path path); | ||
|
|
||
| /** | ||
| * Forces cleanup for the given session (normally called by lifecycle). | ||
| */ | ||
| @Nonnull | ||
| void cleanup(Session session) throws IOException; | ||
| } |
176 changes: 176 additions & 0 deletions
176
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultTempFileService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.maven.internal.impl; | ||
|
|
||
| import static org.apache.maven.api.Constants.KEEP_PROP; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.FileVisitOption; | ||
| import java.nio.file.FileVisitResult; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.SimpleFileVisitor; | ||
| import java.nio.file.attribute.BasicFileAttributes; | ||
| import java.util.Collections; | ||
| import java.util.EnumSet; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import org.apache.maven.api.Session; | ||
| import org.apache.maven.api.SessionData; | ||
| import org.apache.maven.api.services.TempFileService; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Default TempFileService implementation. | ||
| * Stores tracked paths in Session-scoped data and removes them after the build. | ||
| */ | ||
| @Named | ||
| @Singleton | ||
| public final class DefaultTempFileService implements TempFileService { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTempFileService.class); | ||
|
|
||
|
|
||
|
|
||
| // unique, typed session key (uses factory; one narrow unchecked cast) | ||
| @SuppressWarnings({"unchecked", "rawtypes"}) | ||
| private static final SessionData.Key<Set<Path>> TMP_KEY = | ||
| (SessionData.Key) SessionData.key(Set.class, DefaultTempFileService.class); | ||
|
|
||
| // supplier with concrete types (avoids inference noise) | ||
| private static final Supplier<Set<Path>> TMP_SUPPLIER = | ||
| () -> Collections.newSetFromMap(new ConcurrentHashMap<Path, Boolean>()); | ||
|
|
||
| @Override | ||
| public Path createTempFile(final Session session, final String prefix, final String suffix) throws IOException { | ||
| Objects.requireNonNull(session, "session"); | ||
| final Path file = Files.createTempFile(prefix, suffix); | ||
| register(session, file); | ||
| return file; | ||
| } | ||
|
|
||
| @Override | ||
| public Path createTempFile(final Session session, final String prefix, final String suffix, final Path directory) | ||
| throws IOException { | ||
| Objects.requireNonNull(session, "session"); | ||
| Objects.requireNonNull(directory, "directory"); | ||
| final Path file = Files.createTempFile(directory, prefix, suffix); | ||
| register(session, file); | ||
| return file; | ||
| } | ||
|
|
||
| @Override | ||
| public Path createTempDirectory(final Session session, final String prefix) throws IOException { | ||
| Objects.requireNonNull(session, "session"); | ||
| final Path dir = Files.createTempDirectory(prefix); | ||
| register(session, dir); | ||
| return dir; | ||
| } | ||
|
|
||
| @Override | ||
| public Path createTempDirectory(final Session session, final String prefix, final Path directory) | ||
| throws IOException { | ||
| Objects.requireNonNull(session, "session"); | ||
| Objects.requireNonNull(directory, "directory"); | ||
| final Path dir = Files.createTempDirectory(directory, prefix); | ||
| register(session, dir); | ||
| return dir; | ||
| } | ||
|
|
||
| @Override | ||
| public void register(final Session session, final Path path) { | ||
| Objects.requireNonNull(session, "session"); | ||
| Objects.requireNonNull(path, "path"); | ||
| final Set<Path> bucket = sessionPaths(session); | ||
| bucket.add(path); | ||
| if (LOGGER.isDebugEnabled()) { | ||
| LOGGER.debug("Temp path registered for cleanup: {}", path); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void cleanup(final Session session) throws IOException { | ||
| Objects.requireNonNull(session, "session"); | ||
|
|
||
| if (Boolean.getBoolean(KEEP_PROP)) { | ||
| if (LOGGER.isInfoEnabled()) { | ||
| LOGGER.info("Skipping temp cleanup due to -D{}=true", KEEP_PROP); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| final Set<Path> bucket = sessionPaths(session); | ||
| IOException first = null; | ||
|
|
||
| for (final Path path : bucket) { | ||
| try { | ||
| deleteTree(path); | ||
| } catch (final IOException e) { | ||
| if (first == null) { | ||
| first = e; | ||
| } else if (e != first) { | ||
| first.addSuppressed(e); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other exceptions could be added as suppressed ones to the first. |
||
| LOGGER.warn("Failed to delete temp path {}", path, e); | ||
| } | ||
| } | ||
| bucket.clear(); | ||
|
|
||
| if (first != null) { | ||
| throw first; | ||
| } | ||
| } | ||
|
|
||
| // ---- internals --------------------------------------------------------- | ||
|
|
||
| private Set<Path> sessionPaths(final Session session) { | ||
| return session.getData().computeIfAbsent(TMP_KEY, TMP_SUPPLIER); | ||
| } | ||
|
|
||
| private static void deleteTree(final Path path) throws IOException { | ||
| if (path == null || Files.notExists(path)) { | ||
| return; | ||
| } | ||
| // Walk depth-first and delete files, then directories. | ||
| Files.walkFileTree( | ||
| path, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { | ||
| @Override | ||
| public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) | ||
| throws IOException { | ||
| Files.deleteIfExists(file); | ||
| return FileVisitResult.CONTINUE; | ||
| } | ||
|
|
||
| @Override | ||
| public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) | ||
| throws IOException { | ||
| Files.deleteIfExists(dir); | ||
| return FileVisitResult.CONTINUE; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
impl/maven-core/src/main/java/org/apache/maven/internal/impl/TempFileCleanupParticipant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.maven.internal.impl; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import org.apache.maven.AbstractMavenLifecycleParticipant; | ||
| import org.apache.maven.api.Session; | ||
| import org.apache.maven.api.services.TempFileService; | ||
| import org.apache.maven.execution.MavenSession; | ||
|
|
||
| /** | ||
| * Hooks into the Maven lifecycle and removes all temp material after the session. | ||
| */ | ||
| @Named | ||
| @Singleton | ||
| public final class TempFileCleanupParticipant extends AbstractMavenLifecycleParticipant { | ||
|
|
||
| private final TempFileService tempFileService; | ||
|
|
||
| @Inject | ||
| public TempFileCleanupParticipant(final TempFileService tempFileService) { | ||
| this.tempFileService = tempFileService; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterSessionEnd(final MavenSession mavenSession) { | ||
| // Bridge to the API Session (available in Maven 4). | ||
| final Session apiSession = mavenSession.getSession(); | ||
| try { | ||
| tempFileService.cleanup(apiSession); | ||
| } catch (final Exception e) { | ||
| // We’re at session end; just log. Maven already reported build result. | ||
| // Use slf4j directly to avoid throwing from the lifecycle callback. | ||
| org.slf4j.LoggerFactory.getLogger(TempFileCleanupParticipant.class) | ||
| .warn("Temp cleanup failed: {}", e.getMessage()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
No need to use final on arguments or variables.