@@ -26,7 +26,7 @@ public class Installer {
2626
2727 private static final String userHome = System .getProperty ("user.home" );
2828 private static final String wpilibUrl = "http://first.wpi.edu/FRC/roborio/maven" ;
29- private static final String localMaven = userHome + "/.m2/repository" ;
29+ private static final String mavenLocal = userHome + "/.m2/repository" ;
3030 private static final Path tmpDir ;
3131 private static final Path unzippedDir ;
3232 private static boolean overwrite = true ;
@@ -158,12 +158,12 @@ private static void install(ArtifactType type) {
158158 URL remote = resolveRemote (artifactId , v );
159159 File local = resolveLocal (artifactId , v );
160160 File source ;
161+ if (!local .exists ()) {
162+ copyToMavenLocal (wpilibUrl , groupId , artifactId , v );
163+ }
161164 if (local .exists ()) {
162165 System .out .println ("Using local file at " + local .toURI ());
163166 source = local ;
164- } else if (urlExists (remote )) {
165- System .out .println ("Using remote file at " + remote );
166- source = download (remote , artifactId , v );
167167 } else {
168168 throw new NoSuchFileException ("Could not find artifacts. Looked in:\n " +
169169 " " + remote + "\n " +
@@ -190,47 +190,11 @@ private static URL resolveRemote(String artifactId, String version) throws Malfo
190190 }
191191
192192 private static File resolveLocal (String artifactId , String version ) {
193- return new File (resolveRelative (localMaven , artifactId , version ));
193+ return new File (resolveRelative (mavenLocal , artifactId , version ));
194194 }
195195
196196 private static String resolveRelative (String repo , String artifactId , String version ) {
197- return repo + '/' + groupId .replace ('.' , '/' ) + '/' + artifactId + '/' + version + '/' + artifactId + '-' + version + ".jar" ;
198- }
199-
200- /**
201- * Checks if the given URL exists
202- *
203- * @param url the url to check
204- * @return true if the url exists, false if it does not
205- * @throws IOException if the URL is malformed
206- */
207- private static boolean urlExists (URL url ) throws IOException {
208- HttpURLConnection c = (HttpURLConnection ) url .openConnection ();
209- c .setConnectTimeout (1000 );
210- c .setRequestMethod ("GET" );
211- c .setRequestProperty ("User-Agent" , "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)" );
212- c .connect ();
213- return c .getResponseCode () == HttpURLConnection .HTTP_OK ;
214- }
215-
216- /**
217- * Downloads the artifact at the given remote maven repository
218- *
219- * @param url the root repository url
220- * @param artifactId the id of the artifact to download
221- * @param version the version of the artifact to download
222- * @return the location of the downloaded artifact
223- * @throws IOException if the artifact could not be downloaded or a file could not be created for it
224- */
225- private static File download (URL url , String artifactId , String version ) throws IOException {
226- HttpURLConnection c = (HttpURLConnection ) url .openConnection ();
227- c .setConnectTimeout (1000 );
228- c .setRequestMethod ("GET" );
229- c .connect ();
230- InputStream is = c .getInputStream ();
231- Path downloadLocation = Files .createTempFile (tmpDir , artifactId + '-' + version , ".jar" );
232- Files .copy (is , Files .createTempFile (tmpDir , artifactId + '-' + version , ".jar" ));
233- return downloadLocation .toAbsolutePath ().toFile ();
197+ return String .format ("%s/%s/%s/%s/%s-%s.jar" , repo , groupId .replace ('.' , '/' ), artifactId , version , artifactId , version );
234198 }
235199
236200 /**
@@ -293,4 +257,28 @@ private static void unsafeCopy(Path src, Path dst) {
293257 }
294258 }
295259
260+ /**
261+ * Copies a remote library to the local maven repository. This only downloads the .jar and the .pom
262+ *
263+ * @return the path to the copied jar
264+ */
265+ private static Path copyToMavenLocal (String repo , String groupId , String artifactId , String version ) throws IOException {
266+ String remoteDir = String .format ("%s/%s/%s/%s/" , repo , groupId .replace ('.' , '/' ), artifactId , version );
267+ String name = String .format ("%s-%s" , artifactId , version );
268+ String dstDir = remoteDir .replace (repo , mavenLocal );
269+
270+ String jar = name + ".jar" ;
271+ String jarPath = remoteDir + jar ;
272+ System .out .println ("Copying " + jarPath + " to the local maven repository" );
273+ Files .deleteIfExists (Paths .get (dstDir , jar ));
274+ Files .copy (new URL (jarPath ).openStream (), Paths .get (dstDir , jar ));
275+
276+ String pom = name + ".pom" ;
277+ String pomPath = remoteDir + pom ;
278+ Files .deleteIfExists (Paths .get (dstDir , pom ));
279+ Files .copy (new URL (pomPath ).openStream (), Paths .get (dstDir , pom ));
280+
281+ return Paths .get (dstDir , jar );
282+ }
283+
296284}
0 commit comments