Skip to content

Commit 20729b8

Browse files
committed
Fix conversion of file URL to Path, improve exception handling
1 parent 071df53 commit 20729b8

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020

2121
import java.io.File;
22+
import java.net.URISyntaxException;
23+
import java.nio.file.FileSystemNotFoundException;
24+
import java.nio.file.Path;
2225
import java.nio.file.Paths;
2326
import java.security.MessageDigest;
2427
import java.security.NoSuchAlgorithmException;
@@ -119,21 +122,31 @@ private static byte[] hash(String value) {
119122
}
120123

121124
private static File findDataDir() {
122-
// JAR path is e.g.
123-
// ~/.m2/repository/com/diffplug/spotless/spotless-plugin-maven/1.2.3/spotless-plugin-maven-1.2.3.jar
124-
var codeSource = FileLocator.class.getProtectionDomain().getCodeSource();
125-
var location = codeSource != null ? codeSource.getLocation() : null;
126-
var path = location != null ? location.getPath() : null;
127-
var jarPath = path != null && !path.isBlank() ? Paths.get(path) : null;
128-
var parent1 = jarPath != null ? jarPath.getParent() : null;
129-
var parent2 = parent1 != null ? parent1.getParent() : null;
130-
var base = parent2 != null ? parent2.getParent() : null;
131-
var sub = base != null ? base.resolve("spotless-data") : null;
132-
if (sub != null) {
133-
return sub.toAbsolutePath().toFile();
134-
} else {
135-
var home = Paths.get(System.getenv("user.home"));
136-
return home.resolve(".rome").toAbsolutePath().toFile();
125+
try {
126+
// JAR path is e.g.
127+
// ~/.m2/repository/com/diffplug/spotless/spotless-plugin-maven/1.2.3/spotless-plugin-maven-1.2.3.jar
128+
var codeSource = FileLocator.class.getProtectionDomain().getCodeSource();
129+
var location = codeSource != null ? codeSource.getLocation() : null;
130+
var locationUri = location != null ? location.toURI() : null;
131+
var jarPath = locationUri != null && "file".equals(locationUri.getScheme()) ? Path.of(locationUri) : null;
132+
var parent1 = jarPath != null ? jarPath.getParent() : null;
133+
var parent2 = parent1 != null ? parent1.getParent() : null;
134+
var base = parent2 != null ? parent2.getParent() : null;
135+
var sub = base != null ? base.resolve("spotless-data") : null;
136+
if (sub != null) {
137+
return sub.toAbsolutePath().toFile();
138+
} else {
139+
return findUserHome();
140+
}
141+
} catch (final SecurityException e) {
142+
return findUserHome();
143+
} catch (final URISyntaxException | FileSystemNotFoundException | IllegalArgumentException e) {
144+
throw new RuntimeException("Unable to determine data directory in local Maven repository", e);
137145
}
138146
}
147+
148+
private static File findUserHome() {
149+
var home = Paths.get(System.getenv("user.home"));
150+
return home.resolve(".rome").toAbsolutePath().toFile();
151+
}
139152
}

0 commit comments

Comments
 (0)