diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelPathTranslator.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelPathTranslator.java index cbf3263b7a7e..3d08148c1389 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelPathTranslator.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelPathTranslator.java @@ -94,7 +94,7 @@ private List map(List resources, Function mapper) { for (int i = 0; i < resources.size(); i++) { T resource = resources.get(i); T newResource = mapper.apply(resource); - if (newResource != null) { + if (newResource != resource) { if (newResources == null) { newResources = new ArrayList<>(resources); } @@ -105,18 +105,39 @@ private List map(List resources, Function mapper) { return newResources; } + /** + * Returns a resource with all properties identical to the given resource, except the paths + * which are resolved according the given {@code basedir}. If the paths are unchanged, then + * this method returns the previous instance. + * + * @param resource the resource to relocate, or {@code null} + * @param basedir the new base directory + * @return relocated resource, or {@code null} if the given resource was null + */ + @SuppressWarnings("StringEquality") // Identity comparison is ok in this method. private Resource alignToBaseDirectory(Resource resource, Path basedir) { if (resource != null) { - String newDir = alignToBaseDirectory(resource.getDirectory(), basedir); - if (newDir != null) { - return resource.withDirectory(newDir); + String oldDir = resource.getDirectory(); + String newDir = alignToBaseDirectory(oldDir, basedir); + if (newDir != oldDir) { + return resource.with().directory(newDir).build(); } } return resource; } + /** + * Returns a path relocated to the given base directory. If the result of this operation + * is the same path as before, then this method returns the old {@code path} instance. + * It is okay for the caller to compare the {@link String} instances using the identity + * comparator for detecting changes. + * + * @param path the path to relocate, or {@code null} + * @param basedir the new base directory + * @return relocated path, or {@code null} if the given path was null + */ private String alignToBaseDirectory(String path, Path basedir) { String newPath = pathTranslator.alignToBaseDirectory(path, basedir); - return Objects.equals(path, newPath) ? null : newPath; + return Objects.equals(path, newPath) ? path : newPath; } } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java index 50e6ab2e96ed..f42b50e8dacf 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java @@ -116,7 +116,7 @@ private List map(List resources, Function mapper) { private Resource alignToBaseDirectory(Resource resource, Path basedir) { if (resource != null) { - String newDir = alignToBaseDirectory(resource.getDirectory(), basedir); + String newDir = mayAlignToBaseDirectoryOrNull(resource.getDirectory(), basedir); if (newDir != null) { return resource.withDirectory(newDir); } @@ -125,6 +125,17 @@ private Resource alignToBaseDirectory(Resource resource, Path basedir) { } private String alignToBaseDirectory(String path, Path basedir) { + String newPath = mayAlignToBaseDirectoryOrNull(path, basedir); + if (newPath != null) { + return newPath; + } + return path; + } + + /** + * Returns aligned path or {@code null} if no need for change. + */ + private String mayAlignToBaseDirectoryOrNull(String path, Path basedir) { String newPath = pathTranslator.alignToBaseDirectory(path, basedir); return Objects.equals(path, newPath) ? null : newPath; }