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 return Paths.get(distUrl.getPath()).getFileName().toString();
77 }
78
79 private Path getBaseDir(String base) {
80 if (MAVEN_USER_HOME_STRING.equals(base)) {
81 return mavenUserHome;
82 } else if (PROJECT_STRING.equals(base)) {
83 return Paths.get(System.getProperty("user.dir"));
84 } else {
85 throw new RuntimeException("Base: " + base + " is unknown");
86 }
87 }
88
89
90
91
92 public static class LocalDistribution {
93 private final Path distZip;
94
95 private final Path distDir;
96
97 public LocalDistribution(Path distDir, Path distZip) {
98 this.distDir = distDir;
99 this.distZip = distZip;
100 }
101
102
103
104
105
106
107 public Path getDistributionDir() {
108 return distDir;
109 }
110
111
112
113
114
115
116 public Path getZipFile() {
117 return distZip;
118 }
119 }
120 }