diff --git a/README.md b/README.md index 4509abf..390047e 100644 --- a/README.md +++ b/README.md @@ -80,3 +80,13 @@ This can be useful for example when processing some files in a loop so the user This API is usually not used by mojos but for IDE integration, if enabled as a maven-core extension plexus-build-api supply a way to communicate with the running maven build and get events. The default implementation open a tcp connections to a port specified by the system property `plexus.build.ipc.port` using key/value encoded message format. If no such value is given all messages are silently discarded. + +## Resources API + +`org.codehaus.plexus.build.resources.Resources` is a `Path`-based API for resource operations, separate from +`BuildContext`'s logging and scanner concerns. `DefaultResources` delegates to `BuildContext`, so it can be adopted +without waiting for the build tools that provide the new API. + +Use `hasDelta(Path)` as a hint for user-editable sources. For an input and an output, prefer `isUptodate(Path, Path)` +and `newOutputStream(Path)`, which update the file only when the content changes. `copy(Path, Path)` combines both +and marks generated files through `markDerived(Path)`. diff --git a/src/main/java/org/codehaus/plexus/build/BuildContext.java b/src/main/java/org/codehaus/plexus/build/BuildContext.java index ffefd24..62f0c5c 100644 --- a/src/main/java/org/codehaus/plexus/build/BuildContext.java +++ b/src/main/java/org/codehaus/plexus/build/BuildContext.java @@ -181,7 +181,7 @@ public interface BuildContext { /** *
addWarning.
* - * @deprecated Use addMessage with severity=SEVERITY_ERROR instead + * @deprecated Use {@link org.codehaus.plexus.build.messages.Messages} instead * @since 0.0.5 * @param file a {@link java.io.File} object. * @param line a int. @@ -189,12 +189,13 @@ public interface BuildContext { * @param message a {@link java.lang.String} object. * @param cause a {@link java.lang.Throwable} object. */ + @Deprecated void addWarning(File file, int line, int column, String message, Throwable cause); /** *addError.
* - * @deprecated Use addMessage with severity=SEVERITY_WARNING instead + * @deprecated Use {@link org.codehaus.plexus.build.messages.Messages} instead * @since 0.0.5 * @param file a {@link java.io.File} object. * @param line a int. @@ -202,6 +203,7 @@ public interface BuildContext { * @param message a {@link java.lang.String} object. * @param cause a {@link java.lang.Throwable} object. */ + @Deprecated void addError(File file, int line, int column, String message, Throwable cause); /** diff --git a/src/main/java/org/codehaus/plexus/build/resources/DefaultResources.java b/src/main/java/org/codehaus/plexus/build/resources/DefaultResources.java new file mode 100644 index 0000000..eafe605 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/resources/DefaultResources.java @@ -0,0 +1,129 @@ +/* +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.resources; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.build.BuildContext; + +/** + * A {@link Resources} implementation that delegates to the existing {@link BuildContext}, so plugins can adopt the + * new API without waiting for the build tools that provide it. + * + * @since 1.1.0 + */ +@Named("default") +@Singleton +public class DefaultResources implements Resources { + + private final BuildContext buildContext; + + private final LegacySupport legacySupport; + + @Inject + public DefaultResources(BuildContext buildContext, LegacySupport legacySupport) { + this.buildContext = buildContext; + this.legacySupport = legacySupport; + } + + @Override + public boolean hasDelta(Path file) { + return file != null && buildContext.hasDelta(file.toFile()); + } + + @Override + public void refresh(Path file) { + if (file != null) { + buildContext.refresh(file.toFile()); + } + } + + @Override + public OutputStream newOutputStream(Path file) throws IOException { + if (file == null) { + throw new IllegalArgumentException("file cannot be null"); + } + return buildContext.newFileOutputStream(file.toFile()); + } + + @Override + public OutputStream newOutputStream(Path file, boolean derived) throws IOException { + OutputStream stream = newOutputStream(file); + if (derived) { + markDerived(file); + } + return stream; + } + + @Override + public boolean isUptodate(Path target, Path source) { + return target != null && source != null && buildContext.isUptodate(target.toFile(), source.toFile()); + } + + @Override + public Path getPath(String relpath) { + if (relpath == null) { + throw new IllegalArgumentException("relpath cannot be null"); + } + if (legacySupport.getSession() != null) { + MavenProject project = legacySupport.getSession().getCurrentProject(); + if (project != null && project.getBasedir() != null) { + return project.getBasedir() + .toPath() + .resolve(relpath) + .toAbsolutePath() + .normalize(); + } + } + return Paths.get(relpath); + } + + @Override + public void markDerived(Path file) { + // The default build context does not track derived files; IDE integrations override this. + } + + @Override + public void copy(Path source, Path target) throws IOException { + if (source == null) { + throw new IllegalArgumentException("source cannot be null"); + } + if (target == null) { + throw new IllegalArgumentException("target cannot be null"); + } + if (isUptodate(target, source)) { + return; + } + Path parent = target.getParent(); + if (parent != null) { + Files.createDirectories(parent); + } + try (InputStream in = Files.newInputStream(source); + OutputStream out = newOutputStream(target)) { + byte[] buffer = new byte[8192]; + int read; + while ((read = in.read(buffer)) != -1) { + out.write(buffer, 0, read); + } + } + } +} diff --git a/src/main/java/org/codehaus/plexus/build/resources/Resources.java b/src/main/java/org/codehaus/plexus/build/resources/Resources.java new file mode 100644 index 0000000..4994e68 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/resources/Resources.java @@ -0,0 +1,102 @@ +/* +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.resources; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Path; + +/** + * A {@link Path}-based API for the resource operations of a build context. + * + *This API is deliberately separate from {@link org.codehaus.plexus.build.BuildContext}, which also carries + * logging and scanner concerns. It keeps only the operations that have a clear input/output relationship and makes + * their contract explicit.
+ * + *{@link #hasDelta(Path)} is a best effort hint: it is useful for user-editable sources, but a target that was + * deleted or modified outside the build may not be noticed. Where there is an input and an output, prefer + * {@link #isUptodate(Path, Path)} and {@link #newOutputStream(Path)}, which only update a file when its content + * actually changes.
+ * + * @since 1.1.0 + */ +public interface Resources { + + /** + * Returns whether the file has changed since the last build. This is a hint, not a guarantee; see the class + * documentation for when to use it. + * + * @param file the file to check + * @return {@code true} if the file has changed, or if it is outside the build context basedir + */ + boolean hasDelta(Path file); + + /** + * Signals that the file was modified during the build. + * + * @param file the file to refresh + */ + void refresh(Path file); + + /** + * Opens a stream that writes to the file, updating it only when the content changes. + * + * @param file the file to write + * @return the output stream + * @throws IOException if the stream cannot be opened + */ + OutputStream newOutputStream(Path file) throws IOException; + + /** + * Opens a stream that writes to the file and marks the file as derived. + * + * @param file the file to write + * @param derived whether the result is generated and may be overwritten by the next build + * @return the output stream + * @throws IOException if the stream cannot be opened + */ + OutputStream newOutputStream(Path file, boolean derived) throws IOException; + + /** + * Returns whether the target exists and is newer than the source, with no pending changes to either. + * + * @param target the output file + * @param source the input file + * @return {@code true} when the target is up to date + */ + boolean isUptodate(Path target, Path source); + + /** + * Resolves a build-context-relative path against the project basedir. + * + * @param relpath the relative path + * @return the resolved path, absolute when a project basedir is available + */ + Path getPath(String relpath); + + /** + * Marks a file as generated, so an IDE can warn before a user edits it. The default implementation does nothing; + * IDE integrations may override it. + * + * @param file the generated file + */ + void markDerived(Path file); + + /** + * Copies the source to the target when the target is not up to date. The parent directory is created if needed, + * and the copy goes through {@link #newOutputStream(Path)} so implementations can keep their change tracking. + * + * @param source the source file + * @param target the target file + * @throws IOException if the copy fails + */ + void copy(Path source, Path target) throws IOException; +} diff --git a/src/test/java/org/codehaus/plexus/build/resources/DefaultResourcesTest.java b/src/test/java/org/codehaus/plexus/build/resources/DefaultResourcesTest.java new file mode 100644 index 0000000..5fdfb53 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/build/resources/DefaultResourcesTest.java @@ -0,0 +1,169 @@ +/* +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.resources; + +import java.io.File; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.build.BuildContext; +import org.codehaus.plexus.build.RecordingProxy; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DefaultResourcesTest { + + @TempDir + Path tempDir; + + private final RecordingProxy