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