1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.wrapper;
20
21 import java.net.URI;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24
25
26
27
28 public class PathAssembler {
29 public static final String MAVEN_USER_HOME_STRING = "MAVEN_USER_HOME";
30
31 public static final String PROJECT_STRING = "PROJECT";
32
33 private Path mavenUserHome;
34
35 public PathAssembler() {}
36
37 public PathAssembler(Path mavenUserHome) {
38 this.mavenUserHome = mavenUserHome;
39 }
40
41
42
43
44
45
46
47 public LocalDistribution getDistribution(WrapperConfiguration configuration) {
48 String baseName = getBaseName(configuration.getDistribution());
49 String distName = removeExtension(baseName);
50 Path rootDirName = rootDirName(distName, configuration);
51 Path distDir = getBaseDir(configuration.getDistributionBase())
52 .resolve(configuration.getDistributionPath())
53 .resolve(rootDirName);
54 Path distZip = getBaseDir(configuration.getZipBase())
55 .resolve(configuration.getZipPath())
56 .resolve(rootDirName)
57 .resolve(baseName);
58 return new LocalDistribution(distDir, distZip);
59 }
60
61 private Path rootDirName(String distName, WrapperConfiguration configuration) {
62 String urlHash = getHash(configuration.getDistribution());
63 return Paths.get(distName, urlHash);
64 }
65
66 private String getHash(URI path) {
67 return Integer.toHexString(path.hashCode());
68 }
69
70 private String removeExtension(String name) {
71 int dot = name.lastIndexOf(".");
72 return dot > 0 ? name.substring(0, dot) : name;
73 }
74
75 private String getBaseName(URI distUrl) {
76 if (distUrl.getScheme().equals("file")) {
77 return Paths.get(distUrl).getFileName().toString();
78 } else {
79 return Paths.get(distUrl.getPath()).getFileName().toString();
80 }
81 }
82
83 private Path getBaseDir(String base) {
84 if (MAVEN_USER_HOME_STRING.equals(base)) {
85 return mavenUserHome;
86 } else if (PROJECT_STRING.equals(base)) {
87 return Paths.get(System.getProperty("user.dir"));
88 } else {
89 throw new RuntimeException("Base: " + base + " is unknown");
90 }
91 }
92
93
94
95
96 public static class LocalDistribution {
97 private final Path distZip;
98
99 private final Path distDir;
100
101 public LocalDistribution(Path distDir, Path distZip) {
102 this.distDir = distDir;
103 this.distZip = distZip;
104 }
105
106
107
108
109
110
111 public Path getDistributionDir() {
112 return distDir;
113 }
114
115
116
117
118
119
120 public Path getZipFile() {
121 return distZip;
122 }
123 }
124 }