You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2021/01/11 07:57:36 UTC

[jackrabbit-filevault-package-maven-plugin] 01/01: JCRVLT-490 only relativize path if it starts with the given base

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/JCRVLT-490-make-relativize-paths-more-robust
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault-package-maven-plugin.git

commit 204e94b4a29a0a4eb342d469f49d6838923c457d
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Mon Jan 11 08:57:19 2021 +0100

    JCRVLT-490 only relativize path if it starts with the given base
    
    Also fixes issue with IAE on paths with different roots (e.g. Windows,
    different drive letters)
---
 .../packaging/AbstractMetadataPackageMojo.java     | 11 ++++-
 .../packaging/AbstractMetadataPackageMojoTest.java | 51 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/AbstractMetadataPackageMojo.java b/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/AbstractMetadataPackageMojo.java
index a3a1a43..5851a89 100644
--- a/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/AbstractMetadataPackageMojo.java
+++ b/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/AbstractMetadataPackageMojo.java
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.filevault.maven.packaging;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.Collections;
 import java.util.Map;
 import java.util.jar.JarFile;
@@ -38,7 +39,15 @@ public abstract class AbstractMetadataPackageMojo extends AbstractMojo {
     private static final String PROPERTIES_EMBEDDEDFILESMAP_KEY = "embeddedfiles.map";
 
     protected String getProjectRelativeFilePath(File file) {
-        return "'" + project.getBasedir().toPath().relativize(file.toPath()).toString() + "'";
+        return getRelativePath(project.getBasedir().toPath(), file.toPath());
+    }
+
+    protected static String getRelativePath(Path base, Path file) {
+        if (file.startsWith(base)) {
+            return "'" + base.relativize(file).toString() + "'"; 
+        } else {
+            return "'" + file.toString() + "'";
+        }
     }
 
     protected static File getFirstExistingDirectory(File[] directories) {
diff --git a/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/AbstractMetadataPackageMojoTest.java b/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/AbstractMetadataPackageMojoTest.java
new file mode 100644
index 0000000..9255fdc
--- /dev/null
+++ b/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/AbstractMetadataPackageMojoTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.filevault.maven.packaging;
+
+import java.nio.file.FileSystems;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class AbstractMetadataPackageMojoTest {
+
+    @Test
+    public void testGetRelativePath() {
+        // absolute and relative path
+        Path base = Paths.get(".");
+        Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
+        Assert.assertEquals("'" + FileSystems.getDefault().getPath("").toAbsolutePath() + "'", AbstractMetadataPackageMojo.getRelativePath(base, path));
+
+        // one path is parent of the other
+        base = Paths.get("my", "base");
+        path = Paths.get("my", "base", "child");
+        Assert.assertEquals("'child'", AbstractMetadataPackageMojo.getRelativePath(base, path));
+
+        // no common parent
+        base = Paths.get("my", "base");
+        path = Paths.get("other", "file");
+        Assert.assertEquals("'" +path.toString() + "'", AbstractMetadataPackageMojo.getRelativePath(base, path));
+ 
+        // paths have common parent (but one does not start with the other)
+        base = Paths.get("my", "base");
+        path = Paths.get("my", "other");
+        Assert.assertEquals("'" + path + "'", AbstractMetadataPackageMojo.getRelativePath(base, path));
+    }
+}