Skip to content

Commit 495f4ad

Browse files
Copilotlaeubi
andcommitted
Use LegacySupport to get Maven project basedir in Resources.getPath() and update README
Co-authored-by: laeubi <[email protected]>
1 parent b9f0758 commit 495f4ad

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,59 @@ The project was relocated from <https://github.com/sonatype/sisu-build-api>. Als
3232

3333
## Provided APIs
3434

35+
### Resources API
36+
37+
The Resources API provides a modern, Path-based interface for managing build resources. It separates resource management concerns from logging/messaging functionality and provides better control over file operations during the build process.
38+
39+
**Key Features:**
40+
- Modern `java.nio.file.Path` instead of `java.io.File`
41+
- Change detection with `hasDelta()` (best effort hint)
42+
- Reliable input/output freshness checking with `isUptodate()`
43+
- Optimized output streams that only update files when content changes
44+
- Support for marking generated/derived files for IDE integration
45+
- Convenient copy operation that respects up-to-date checks
46+
- Relative path conversion via `getPath()`
47+
48+
**Example Usage:**
49+
50+
```java
51+
@Inject
52+
private Resources resources;
53+
54+
public void execute() {
55+
// Convert relative paths to absolute paths
56+
Path source = resources.getPath("src/main/java/Example.java");
57+
Path target = resources.getPath("target/classes/Example.class");
58+
59+
// Smart copy - only copies when target is stale
60+
resources.copy(source, target);
61+
62+
// Check if file has changed (best effort hint)
63+
if (resources.hasDelta(source)) {
64+
// Process the file
65+
}
66+
67+
// Reliable check for input/output scenarios
68+
if (!resources.isUptodate(target, source)) {
69+
// Regenerate target from source
70+
}
71+
72+
// Write to a file with change detection
73+
Path generated = resources.getPath("target/generated/Proto.java");
74+
try (OutputStream out = resources.newOutputStream(generated, true)) {
75+
// Write content - file marked as derived for IDE warnings
76+
out.write(content);
77+
}
78+
79+
// Mark a file as generated/derived
80+
resources.markDerived(generated);
81+
}
82+
```
83+
84+
**When to use `hasDelta()` vs `isUptodate()`:**
85+
- Use `hasDelta()` as a hint for user-editable source files where you want to detect changes
86+
- Use `isUptodate()` when there's a clear input/output relationship, as it handles cases where target files may be deleted or modified outside the build process
87+
3588
### Messages API
3689

3790
The Messages API provides a modern, flexible way to create and manage build messages/markers that inform users in an IDE about issues in their files. It uses a builder pattern for constructing messages in a more convenient and extensible way compared to the legacy BuildContext message methods.

src/main/java/org/codehaus/plexus/build/resources/DefaultResources.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
import javax.inject.Named;
1717
import javax.inject.Singleton;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.io.OutputStream;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
2425

26+
import org.apache.maven.plugin.LegacySupport;
27+
import org.apache.maven.project.MavenProject;
2528
import org.codehaus.plexus.build.BuildContext;
2629

2730
/**
@@ -37,15 +40,18 @@
3740
public class DefaultResources implements Resources {
3841

3942
private final BuildContext buildContext;
43+
private final LegacySupport legacySupport;
4044

4145
/**
4246
* Creates a new DefaultResources instance.
4347
*
4448
* @param buildContext the BuildContext to which operations will be delegated
49+
* @param legacySupport the LegacySupport to get the current Maven project
4550
*/
4651
@Inject
47-
public DefaultResources(BuildContext buildContext) {
52+
public DefaultResources(BuildContext buildContext, LegacySupport legacySupport) {
4853
this.buildContext = buildContext;
54+
this.legacySupport = legacySupport;
4955
}
5056

5157
@Override
@@ -97,10 +103,16 @@ public Path getPath(String relpath) {
97103
if (relpath == null) {
98104
throw new IllegalArgumentException("relpath cannot be null");
99105
}
100-
// Note: The BuildContext API doesn't expose basedir directly.
101-
// This default implementation resolves paths relative to the current working directory.
102-
// Custom implementations (e.g., IDE integrations) should override this method
103-
// to resolve against the actual build context basedir.
106+
// Get the basedir from the current Maven project via LegacySupport
107+
MavenProject project =
108+
legacySupport.getSession() != null ? legacySupport.getSession().getCurrentProject() : null;
109+
if (project != null) {
110+
File basedir = project.getBasedir();
111+
if (basedir != null) {
112+
return basedir.toPath().resolve(relpath);
113+
}
114+
}
115+
// Fallback to current working directory if project is not available
104116
return java.nio.file.Paths.get(relpath);
105117
}
106118

0 commit comments

Comments
 (0)