Last active
November 4, 2022 14:18
-
-
Save ysb33r/5804364 to your computer and use it in GitHub Desktop.
The Groovy way to obtain a relative path given two paths.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def root= new File('/usr/share') | |
def full= new File('/usr/share/docs/rpm-4.4') | |
// Print the relative path of 'full' in relation to 'root' | |
// Notice that the full path is passed as a parameter to the root. | |
def relPath= new File( root.toURI().relativize( full.toURI() ).toString() ) |
@jgustie - Thx for that.
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.String.toPath() is applicable
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.String.toPath() is applicable
It is File::toPath, not String::toPath.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using URI is dangerous here because of the encoding (e.g. if
full
were/usr/share/docs with space/rpm-4.4
you end up withdocs%20with%20space/rpm-4.4
). You can use thePath.relativize
from Java 7 to avoid this: