|
19 | 19 | import static java.nio.charset.StandardCharsets.UTF_8;
|
20 | 20 |
|
21 | 21 | import java.io.File;
|
| 22 | +import java.net.URISyntaxException; |
| 23 | +import java.nio.file.FileSystemNotFoundException; |
| 24 | +import java.nio.file.Path; |
22 | 25 | import java.nio.file.Paths;
|
23 | 26 | import java.security.MessageDigest;
|
24 | 27 | import java.security.NoSuchAlgorithmException;
|
@@ -119,21 +122,31 @@ private static byte[] hash(String value) {
|
119 | 122 | }
|
120 | 123 |
|
121 | 124 | 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); |
137 | 145 | }
|
138 | 146 | }
|
| 147 | + |
| 148 | + private static File findUserHome() { |
| 149 | + var home = Paths.get(System.getenv("user.home")); |
| 150 | + return home.resolve(".rome").toAbsolutePath().toFile(); |
| 151 | + } |
139 | 152 | }
|
0 commit comments