-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDefaultResources.java
More file actions
154 lines (136 loc) · 5.37 KB
/
DefaultResources.java
File metadata and controls
154 lines (136 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright (c) 2008 Sonatype, Inc. All rights reserved.
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.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.build.BuildContext;
/**
* Default implementation of the Resources interface.
* <p>
* This implementation delegates to the BuildContext for compatibility with existing
* build infrastructure. It provides a transition path from the File-based API to the
* modern Path-based API.
* </p>
*/
@Named("default")
@Singleton
public class DefaultResources implements Resources {
private final BuildContext buildContext;
private final LegacySupport legacySupport;
/**
* Creates a new DefaultResources instance.
*
* @param buildContext the BuildContext to which operations will be delegated
* @param legacySupport the LegacySupport to get the current Maven project
*/
@Inject
public DefaultResources(BuildContext buildContext, LegacySupport legacySupport) {
this.buildContext = buildContext;
this.legacySupport = legacySupport;
}
@Override
public boolean hasDelta(Path file) {
if (file == null) {
return false;
}
return 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 outputStream = newOutputStream(file);
if (derived) {
// Mark the file as derived after creating the output stream.
// In the default implementation, markDerived is a no-op, so the timing doesn't matter.
// Custom implementations that track derived files should consider wrapping the stream
// to defer marking until successful close() to avoid marking files on failed writes.
markDerived(file);
}
return outputStream;
}
@Override
public boolean isUptodate(Path target, Path source) {
if (target == null || source == null) {
return false;
}
return buildContext.isUptodate(target.toFile(), source.toFile());
}
@Override
public Path getPath(String relpath) {
if (relpath == null) {
throw new IllegalArgumentException("relpath cannot be null");
}
// Get the basedir from the current Maven project via LegacySupport
MavenProject project =
legacySupport.getSession() != null ? legacySupport.getSession().getCurrentProject() : null;
if (project != null) {
File basedir = project.getBasedir();
if (basedir != null) {
return basedir.toPath().resolve(relpath);
}
}
// Fallback to current working directory if project is not available
return java.nio.file.Paths.get(relpath);
}
@Override
public void markDerived(Path file) {
// No-op in the default implementation
// Custom implementations (e.g., IDE integrations) can override this
// to provide actual derived file tracking
}
@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");
}
// Only copy if the target is not up-to-date with the source
if (!isUptodate(target, source)) {
// Ensure parent directory exists
Path parentDir = target.getParent();
if (parentDir != null && !Files.exists(parentDir)) {
Files.createDirectories(parentDir);
}
// Copy using Files API and newOutputStream to ensure proper change detection
try (InputStream in = Files.newInputStream(source);
OutputStream out = newOutputStream(target)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}
}