Skip to content

Commit 1eb321d

Browse files
committed
[MBUILDCACHE-73] Unify property value conversions in saving and restoration
1 parent 90ade09 commit 1eb321d

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
import javax.inject.Inject;
2323
import javax.inject.Named;
2424

25-
import java.io.File;
2625
import java.nio.file.Path;
2726
import java.util.HashSet;
2827
import java.util.List;
2928
import java.util.Map;
3029
import java.util.Set;
3130

32-
import org.apache.commons.lang3.ArrayUtils;
3331
import org.apache.commons.lang3.Strings;
3432
import org.apache.maven.SessionScoped;
3533
import org.apache.maven.buildcache.artifact.ArtifactRestorationReport;
@@ -381,19 +379,8 @@ boolean isParamsMatched(
381379
final String currentValue;
382380
try {
383381
Object value = ReflectionUtils.getValueIncludingSuperclasses(propertyName, mojo);
384-
385-
if (value instanceof File) {
386-
Path baseDirPath = project.getBasedir().toPath();
387-
Path path = ((File) value).toPath();
388-
currentValue = normalizedPath(path, baseDirPath);
389-
} else if (value instanceof Path) {
390-
Path baseDirPath = project.getBasedir().toPath();
391-
currentValue = normalizedPath(((Path) value), baseDirPath);
392-
} else if (value != null && value.getClass().isArray()) {
393-
currentValue = ArrayUtils.toString(value);
394-
} else {
395-
currentValue = String.valueOf(value);
396-
}
382+
Path baseDirPath = project.getBasedir().toPath();
383+
currentValue = DtoUtils.normalizeValue(value, baseDirPath);
397384
} catch (IllegalAccessException e) {
398385
LOGGER.error("Cannot extract plugin property {} from mojo {}", propertyName, mojo, e);
399386
return false;
@@ -419,33 +406,6 @@ boolean isParamsMatched(
419406
return true;
420407
}
421408

422-
/**
423-
* Best effort to normalize paths from Mojo fields.
424-
* - all absolute paths under project root to be relativized for portability
425-
* - redundant '..' and '.' to be removed to have consistent views on all paths
426-
* - all relative paths are considered portable and should not be touched
427-
* - absolute paths outside of project directory could not be deterministically
428-
* relativized and not touched
429-
*/
430-
private static String normalizedPath(Path path, Path baseDirPath) {
431-
boolean isProjectSubdir = path.isAbsolute() && path.startsWith(baseDirPath);
432-
if (LOGGER.isDebugEnabled()) {
433-
LOGGER.debug(
434-
"normalizedPath isProjectSubdir {} path '{}' - baseDirPath '{}', path.isAbsolute() {}, path.startsWith(baseDirPath) {}",
435-
isProjectSubdir,
436-
path,
437-
baseDirPath,
438-
path.isAbsolute(),
439-
path.startsWith(baseDirPath));
440-
}
441-
Path preparedPath = isProjectSubdir ? baseDirPath.relativize(path) : path;
442-
String normalizedPath = preparedPath.normalize().toString();
443-
if (LOGGER.isDebugEnabled()) {
444-
LOGGER.debug("normalizedPath '{}' - {} return {}", path, baseDirPath, normalizedPath);
445-
}
446-
return normalizedPath;
447-
}
448-
449409
private enum CacheRestorationStatus {
450410
SUCCESS,
451411
FAILURE,

src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ private void recordMojoProperties(CompletedExecution execution, MojoExecutionEve
685685
final Object mojo = executionEvent.getMojo();
686686

687687
final File baseDir = executionEvent.getProject().getBasedir();
688-
final String baseDirPath = FilenameUtils.normalizeNoEndSeparator(baseDir.getAbsolutePath()) + File.separator;
688+
final Path baseDirPath = baseDir.toPath();
689689

690690
final List<Parameter> parameters = mojoExecution.getMojoDescriptor().getParameters();
691691
for (Parameter parameter : parameters) {

src/main/java/org/apache/maven/buildcache/xml/DtoUtils.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import javax.annotation.Nonnull;
2222

23+
import java.io.File;
24+
import java.nio.file.Path;
2325
import java.util.List;
2426

2527
import org.apache.commons.lang3.ArrayUtils;
@@ -109,14 +111,11 @@ public static Dependency createDependency(Artifact artifact) {
109111
}
110112

111113
public static void addProperty(
112-
CompletedExecution execution, String propertyName, Object value, String baseDirPath, boolean tracked) {
114+
CompletedExecution execution, String propertyName, Object value, Path baseDirPath, boolean tracked) {
113115
final PropertyValue valueType = new PropertyValue();
114116
valueType.setName(propertyName);
115-
if (value != null && value.getClass().isArray()) {
116-
value = ArrayUtils.toString(value);
117-
}
118-
final String valueText = String.valueOf(value);
119-
valueType.setValue(Strings.CS.remove(valueText, baseDirPath));
117+
final String valueText = normalizeValue(value, baseDirPath);
118+
valueType.setValue(valueText);
120119
valueType.setTracked(tracked);
121120
execution.addProperty(valueType);
122121
}
@@ -173,4 +172,46 @@ public static Artifact copy(Artifact artifact) {
173172
copy.setFileSize(artifact.getFileSize());
174173
return copy;
175174
}
175+
176+
public static String normalizeValue(Object value, Path baseDirPath) {
177+
final String currentValue;
178+
if (value instanceof File) {
179+
Path path = ((File) value).toPath();
180+
currentValue = normalizedPath(path, baseDirPath);
181+
} else if (value instanceof Path) {
182+
currentValue = normalizedPath(((Path) value), baseDirPath);
183+
} else if (value != null && value.getClass().isArray()) {
184+
currentValue = ArrayUtils.toString(value);
185+
} else {
186+
currentValue = String.valueOf(value);
187+
}
188+
return currentValue;
189+
}
190+
191+
/**
192+
* Best effort to normalize paths from Mojo fields.
193+
* - all absolute paths under project root to be relativized for portability
194+
* - redundant '..' and '.' to be removed to have consistent views on all paths
195+
* - all relative paths are considered portable and should not be touched
196+
* - absolute paths outside of project directory could not be deterministically
197+
* relativized and not touched
198+
*/
199+
private static String normalizedPath(Path path, Path baseDirPath) {
200+
boolean isProjectSubdir = path.isAbsolute() && path.startsWith(baseDirPath);
201+
if (LOGGER.isDebugEnabled()) {
202+
LOGGER.debug(
203+
"normalizedPath isProjectSubdir {} path '{}' - baseDirPath '{}', path.isAbsolute() {}, path.startsWith(baseDirPath) {}",
204+
isProjectSubdir,
205+
path,
206+
baseDirPath,
207+
path.isAbsolute(),
208+
path.startsWith(baseDirPath));
209+
}
210+
Path preparedPath = isProjectSubdir ? baseDirPath.relativize(path) : path;
211+
String normalizedPath = preparedPath.normalize().toString();
212+
if (LOGGER.isDebugEnabled()) {
213+
LOGGER.debug("normalizedPath '{}' - {} return {}", path, baseDirPath, normalizedPath);
214+
}
215+
return normalizedPath;
216+
}
176217
}

0 commit comments

Comments
 (0)