-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
MavenLocalRepoFileInitializer.java
240 lines (209 loc) · 7.07 KB
/
MavenLocalRepoFileInitializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.start.fileinits;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.jetty.start.BaseHome;
import org.eclipse.jetty.start.FS;
import org.eclipse.jetty.start.FileInitializer;
import org.eclipse.jetty.start.StartLog;
import org.eclipse.jetty.start.Utils;
/**
* Attempt to download a <code>maven://</code> URI, by first attempting to find
* the resource in the maven repository system (starting with local, then
* central)
* <p>
* Valid URI Formats:
* <dl>
* <dt><code>maven://<groupId>/<artifactId>/<version></code></dt>
* <dd>minimum requirement (type defaults to <code>jar</code>, with no
* classifier)</dd>
* <dt><code>maven://<groupId>/<artifactId>/<version>/<type></code></dt>
* <dd>optional type requirement</dd>
* <dt>
* <code>maven://<groupId>/<artifactId>/<version>/<type>/<classifier></code>
* </dt>
* <dd>optional type and classifier requirement</dd>
* </dl>
*/
public class MavenLocalRepoFileInitializer extends FileInitializer
{
public static class Coordinates
{
public String groupId;
public String artifactId;
public String version;
public String type;
public String classifier;
private String mavenRepoUri = DEFAULT_REMOTE_REPO;
public String toPath()
{
StringBuilder pathlike = new StringBuilder();
pathlike.append(groupId.replace('.', '/'));
pathlike.append('/').append(artifactId);
pathlike.append('/').append(version);
pathlike.append('/').append(artifactId);
pathlike.append('-').append(version);
if (classifier != null)
{
pathlike.append('-').append(classifier);
}
pathlike.append('.').append(type);
return pathlike.toString();
}
public URI toCentralURI()
{
return URI.create(mavenRepoUri + toPath());
}
}
private static final String DEFAULT_REMOTE_REPO = "https://repo1.maven.org/maven2/";
private Path localRepositoryDir;
private final boolean readonly;
private String mavenRepoUri;
public MavenLocalRepoFileInitializer(BaseHome baseHome)
{
this(baseHome, null, true);
}
public MavenLocalRepoFileInitializer(BaseHome baseHome, Path localRepoDir, boolean readonly)
{
super(baseHome, "maven");
this.localRepositoryDir = localRepoDir;
this.readonly = readonly;
}
public MavenLocalRepoFileInitializer(BaseHome baseHome, Path localRepoDir, boolean readonly, String mavenRepoUri)
{
super(baseHome, "maven");
this.localRepositoryDir = localRepoDir;
this.readonly = readonly;
this.mavenRepoUri = mavenRepoUri;
}
@Override
public boolean create(URI uri, String location) throws IOException
{
Coordinates coords = getCoordinates(uri);
if (coords == null)
{
// Skip, not a maven:// URI
return false;
}
Path destination = getDestination(uri, location);
if (isFilePresent(destination))
return false;
// If using local repository
if (this.localRepositoryDir != null)
{
// Grab copy from local repository (download if needed to local
// repository)
Path localRepoFile = getLocalRepoFile(coords);
if (localRepoFile != null)
{
if (FS.ensureDirectoryExists(destination.getParent()))
StartLog.log("MKDIR", _basehome.toShortForm(destination.getParent()));
StartLog.log("COPY ", "%s to %s", localRepoFile, _basehome.toShortForm(destination));
Files.copy(localRepoFile, destination);
return true;
}
}
// normal non-local repo version
download(coords.toCentralURI(), destination);
return true;
}
private Path getLocalRepoFile(Coordinates coords) throws IOException
{
Path localFile = localRepositoryDir.resolve(coords.toPath());
if (FS.canReadFile(localFile))
return localFile;
// Download, if needed
if (!readonly)
{
download(coords.toCentralURI(), localFile);
return localFile;
}
return null;
}
public String getRemoteUri()
{
if (this.mavenRepoUri != null)
{
return this.mavenRepoUri;
}
else
{
return System.getProperty("maven.repo.uri", DEFAULT_REMOTE_REPO);
}
}
public Coordinates getCoordinates(URI uri)
{
if (!"maven".equalsIgnoreCase(uri.getScheme()))
{
return null;
}
String ssp = uri.getSchemeSpecificPart();
if (ssp.startsWith("//"))
{
ssp = ssp.substring(2);
}
String[] parts = ssp.split("/");
if (StartLog.isDebugEnabled())
{
StartLog.debug("ssp = %s", ssp);
StartLog.debug("parts = %d", parts.length);
for (int i = 0; i < parts.length; i++)
{
StartLog.debug(" part[%2d]: [%s]", i, parts[i]);
}
}
if (parts.length < 3)
{
throw new RuntimeException("Not a valid maven:// uri - " + uri);
}
Coordinates coords = new Coordinates();
coords.groupId = parts[0];
coords.artifactId = parts[1];
coords.version = parts[2];
coords.type = "jar";
coords.classifier = null;
coords.mavenRepoUri = getRemoteUri();
if (parts.length >= 4)
{
if (Utils.isNotBlank(parts[3]))
{
coords.type = parts[3];
}
if ((parts.length == 5) && (Utils.isNotBlank(parts[4])))
{
coords.classifier = parts[4];
}
}
return coords;
}
/**
* protected only for testing purpose
*
* @param uri the the uri to download
* @param destination the destination File
*/
@Override
protected void download(URI uri, Path destination)
throws IOException
{
super.download(uri, destination);
}
}