2424import static java .util .concurrent .TimeUnit .MINUTES ;
2525
2626import com .github .bazelbuild .rules_jvm_external .ByteStreams ;
27- import com .google .api .client .http .GenericUrl ;
28- import com .google .api .client .http .HttpRequest ;
29- import com .google .api .client .http .HttpResponse ;
30- import com .google .api .client .http .HttpResponseException ;
31- import com .google .api .client .http .HttpTransport ;
32- import com .google .api .client .http .javanet .NetHttpTransport ;
27+ import com .github .bazelbuild .rules_jvm_external .resolver .netrc .Netrc ;
28+ import com .github .bazelbuild .rules_jvm_external .resolver .remote .HttpDownloader ;
3329import com .google .auth .Credentials ;
3430import com .google .auth .oauth2 .GoogleCredentials ;
3531import com .google .cloud .WriteChannel ;
4844import java .math .BigInteger ;
4945import java .net .HttpURLConnection ;
5046import java .net .URI ;
51- import java .net .URISyntaxException ;
5247import java .net .URL ;
5348import java .nio .channels .Channels ;
5449import java .nio .charset .StandardCharsets ;
@@ -92,16 +87,25 @@ public class MavenPublisher {
9287 private static final String [] SUPPORTED_SCHEMES = {
9388 "file:/" , "https://" , "gs://" , "s3://" , "artifactregistry://"
9489 };
90+ private static final String [] SUPPORTED_UPLOAD_SCHEMES = {"file:/" , "https://" , "s3://" };
9591
9692 public static void main (String [] args )
9793 throws IOException , InterruptedException , ExecutionException , TimeoutException {
9894 String repo = System .getenv ("MAVEN_REPO" );
95+ boolean publishMavenMetadata = Boolean .parseBoolean (args [3 ]);
96+
9997 if (!isSchemeSupported (repo )) {
10098 throw new IllegalArgumentException (
10199 "Repository must be accessed via the supported schemes: "
102100 + Arrays .toString (SUPPORTED_SCHEMES ));
103101 }
104102
103+ if (!isUploadSchemeSupported (repo )) {
104+ throw new IllegalArgumentException (
105+ "Repository must be uploaded to via the supported schemes: "
106+ + Arrays .toString (SUPPORTED_UPLOAD_SCHEMES ));
107+ }
108+
105109 boolean gpgSign = Boolean .parseBoolean (System .getenv ("GPG_SIGN" ));
106110 Credentials credentials =
107111 new BasicAuthCredentials (System .getenv ("MAVEN_USER" ), System .getenv ("MAVEN_PASSWORD" ));
@@ -133,8 +137,6 @@ public static void main(String[] args)
133137 futures .add (upload (repo , credentials , coords , "." + ext , mainArtifact , signingMetadata ));
134138 }
135139
136- boolean publishMavenMetadata = Boolean .valueOf (args [3 ]);
137-
138140 if (args .length > 4 && !args [4 ].isEmpty ()) {
139141 List <String > extraArtifactTuples = Splitter .onPattern ("," ).splitToList (args [4 ]);
140142 for (String artifactTuple : extraArtifactTuples ) {
@@ -158,7 +160,9 @@ public static void main(String[] args)
158160
159161 // uploading the maven-metadata.xml signals to cut over to the new version, so it must be at
160162 // the end.
161- if (publishMavenMetadata ) {
163+ // publishing the file is opt-in for remote repositories, but always done for local file
164+ // repositories.
165+ if (publishMavenMetadata || repo .startsWith ("file:/" )) {
162166 all = all .thenCompose (Void -> uploadMavenMetadata (repo , credentials , coords ));
163167 }
164168
@@ -184,18 +188,27 @@ private static boolean isSchemeSupported(String repo) {
184188 return false ;
185189 }
186190
191+ private static boolean isUploadSchemeSupported (String repo ) {
192+ for (String scheme : SUPPORTED_UPLOAD_SCHEMES ) {
193+ if (repo .startsWith (scheme )) {
194+ return true ;
195+ }
196+ }
197+ return false ;
198+ }
199+
187200 /**
188201 * Download the pre-existing maven-metadata.xml file if it exists. If no such file exists, create
189202 * a default Metadata with the Coordinates provided.
190203 */
191204 private static CompletableFuture <Metadata > downloadExistingMavenMetadata (
192- String repo , Credentials credentials , Coordinates coords ) {
205+ String repo , Coordinates coords ) {
193206 String mavenMetadataUrl =
194207 String .format (
195208 "%s/%s/%s/maven-metadata.xml" ,
196209 repo .replaceAll ("/$" , "" ), coords .groupId .replace ('.' , '/' ), coords .artifactId );
197210
198- return download (mavenMetadataUrl , credentials )
211+ return download (mavenMetadataUrl )
199212 .thenApply (
200213 optionalFileContents -> {
201214 try {
@@ -229,7 +242,7 @@ private static CompletableFuture<Void> uploadMavenMetadata(
229242 String .format (
230243 "%s/%s/%s/maven-metadata.xml" ,
231244 repo .replaceAll ("/$" , "" ), coords .groupId .replace ('.' , '/' ), coords .artifactId );
232- return downloadExistingMavenMetadata (repo , credentials , coords )
245+ return downloadExistingMavenMetadata (repo , coords )
233246 .thenCompose (
234247 metadata -> {
235248 try {
@@ -301,44 +314,13 @@ private static CompletableFuture<Void> upload(
301314 MavenSigning .SigningMethod signingMethod = signingMetadata .signingMethod ;
302315 if (signingMethod .equals (MavenSigning .SigningMethod .GPG )) {
303316 uploads .add (upload (String .format ("%s%s.asc" , base , append ), credentials , gpg_sign (item )));
304- uploads .add (upload (String .format ("%s%s.md5.asc" , base , append ), credentials , gpg_sign (md5 )));
305- uploads .add (
306- upload (String .format ("%s%s.sha1.asc" , base , append ), credentials , gpg_sign (sha1 )));
307- uploads .add (
308- upload (String .format ("%s%s.sha256.asc" , base , append ), credentials , gpg_sign (sha256 )));
309- uploads .add (
310- upload (String .format ("%s%s.sha512.asc" , base , append ), credentials , gpg_sign (sha512 )));
311317 } else if (signingMethod .equals (MavenSigning .SigningMethod .PGP )) {
312318 uploads .add (
313319 upload (
314320 String .format ("%s%s.asc" , base , append ),
315321 credentials ,
316322 in_memory_pgp_sign (
317323 item , signingMetadata .signingKey , signingMetadata .signingPassword )));
318- uploads .add (
319- upload (
320- String .format ("%s%s.md5.asc" , base , append ),
321- credentials ,
322- in_memory_pgp_sign (
323- md5 , signingMetadata .signingKey , signingMetadata .signingPassword )));
324- uploads .add (
325- upload (
326- String .format ("%s%s.sha1.asc" , base , append ),
327- credentials ,
328- in_memory_pgp_sign (
329- sha1 , signingMetadata .signingKey , signingMetadata .signingPassword )));
330- uploads .add (
331- upload (
332- String .format ("%s%s.sha256.asc" , base , append ),
333- credentials ,
334- in_memory_pgp_sign (
335- sha256 , signingMetadata .signingKey , signingMetadata .signingPassword )));
336- uploads .add (
337- upload (
338- String .format ("%s%s.sha512.asc" , base , append ),
339- credentials ,
340- in_memory_pgp_sign (
341- sha512 , signingMetadata .signingKey , signingMetadata .signingPassword )));
342324 }
343325
344326 return CompletableFuture .allOf (uploads .toArray (new CompletableFuture <?>[0 ]));
@@ -374,16 +356,15 @@ private static String toHexS(String fmt, String algorithm, byte[] toHash) {
374356 * Attempts to download the file at the given targetUrl. Valid protocols are: http(s), file, and
375357 * s3 at the moment.
376358 */
377- private static CompletableFuture <Optional <String >> download (
378- String targetUrl , Credentials credentials ) {
359+ private static CompletableFuture <Optional <String >> download (String targetUrl ) {
379360 if (targetUrl .startsWith ("http" )) {
380- return httpDownload (targetUrl , credentials );
381- } else if (targetUrl .startsWith ("file:// " )) {
361+ return httpDownload (targetUrl );
362+ } else if (targetUrl .startsWith ("file:/" )) {
382363 return fileDownload (targetUrl );
383364 } else if (targetUrl .startsWith ("s3://" )) {
384365 return s3Download (targetUrl );
385366 } else {
386- throw new IllegalArgumentException ("Unsupported protocol for download." );
367+ throw new IllegalArgumentException ("Unsupported protocol for download: " + targetUrl );
387368 }
388369 }
389370
@@ -392,7 +373,7 @@ private static CompletableFuture<Optional<String>> s3Download(String targetUrl)
392373 () -> {
393374 S3Client s3Client = S3Client .create ();
394375 try {
395- URI s3Uri = new URI (targetUrl );
376+ URI s3Uri = URI . create (targetUrl );
396377 String bucketName = s3Uri .getHost ();
397378 String key = s3Uri .getPath ().substring (1 );
398379 GetObjectRequest request =
@@ -402,8 +383,6 @@ private static CompletableFuture<Optional<String>> s3Download(String targetUrl)
402383 CharStreams .toString (new InputStreamReader (s3Object , StandardCharsets .UTF_8 )));
403384 } catch (IOException e ) {
404385 throw new UncheckedIOException (e );
405- } catch (URISyntaxException e ) {
406- throw new RuntimeException (e );
407386 } catch (S3Exception e ) {
408387 if (e .statusCode () == 404 ) {
409388 return Optional .empty ();
@@ -418,48 +397,28 @@ private static CompletableFuture<Optional<String>> fileDownload(String targetUrl
418397 return CompletableFuture .supplyAsync (
419398 () -> {
420399 try {
421- Path path = Paths .get (new URL ( targetUrl ). toURI ( ));
400+ Path path = Paths .get (URI . create ( targetUrl ));
422401 if (!Files .exists (path )) {
423402 return Optional .empty ();
424403 }
425404 return Optional .of (Files .readString (path , StandardCharsets .UTF_8 ));
426405 } catch (IOException e ) {
427406 throw new UncheckedIOException (e );
428- } catch (URISyntaxException e ) {
429- throw new RuntimeException (e );
430407 }
431408 });
432409 }
433410
434- private static CompletableFuture <Optional <String >> httpDownload (
435- String targetUrl , Credentials credentials ) {
411+ private static CompletableFuture <Optional <String >> httpDownload (String targetUrl ) {
436412 return CompletableFuture .supplyAsync (
437413 () -> {
414+ HttpDownloader downloader = new HttpDownloader (Netrc .fromUserHome ());
415+
416+ Path path = downloader .get (URI .create (targetUrl ));
417+ if (path == null || !Files .exists (path )) {
418+ return Optional .empty ();
419+ }
438420 try {
439- HttpTransport httpTransport = new NetHttpTransport ();
440- HttpRequest request =
441- httpTransport
442- .createRequestFactory (
443- initRequest -> {
444- Map <String , List <String >> authHeaders = credentials .getRequestMetadata ();
445- // Add the authorization headers to the HTTP request
446- if (authHeaders != null ) {
447- for (Map .Entry <String , List <String >> entry : authHeaders .entrySet ()) {
448- initRequest .getHeaders ().put (entry .getKey (), entry .getValue ());
449- }
450- }
451- })
452- .buildGetRequest (new GenericUrl (targetUrl ));
453- HttpResponse response = request .execute ();
454- return Optional .of (
455- CharStreams .toString (
456- new InputStreamReader (response .getContent (), StandardCharsets .UTF_8 )));
457- } catch (HttpResponseException e ) {
458- if (e .getStatusCode () == 404 ) {
459- return Optional .empty ();
460- } else {
461- throw new UncheckedIOException (e );
462- }
421+ return Optional .of (Files .readString (path , StandardCharsets .UTF_8 ));
463422 } catch (IOException e ) {
464423 throw new UncheckedIOException (e );
465424 }
@@ -551,7 +510,7 @@ private static Callable<Void> httpUpload(
551510 private static Callable <Void > writeFile (String targetUrl , Path toUpload ) {
552511 return () -> {
553512 LOG .info (String .format ("Copying %s to %s" , toUpload , targetUrl ));
554- Path path = Paths .get (new URL ( targetUrl ). toURI ( ));
513+ Path path = Paths .get (URI . create ( targetUrl ));
555514 Files .createDirectories (path .getParent ());
556515 Files .deleteIfExists (path );
557516 Files .copy (toUpload , path );
@@ -563,7 +522,7 @@ private static Callable<Void> writeFile(String targetUrl, Path toUpload) {
563522 private static Callable <Void > gcsUpload (String targetUrl , Path toUpload ) {
564523 return () -> {
565524 Storage storage = StorageOptions .getDefaultInstance ().getService ();
566- URI gsUri = new URI (targetUrl );
525+ URI gsUri = URI . create (targetUrl );
567526 String bucketName = gsUri .getHost ();
568527 String path = gsUri .getPath ().substring (1 );
569528
@@ -581,12 +540,12 @@ private static Callable<Void> gcsUpload(String targetUrl, Path toUpload) {
581540 private static Callable <Void > s3upload (String targetUrl , Path toUpload ) {
582541 return () -> {
583542 S3Client s3Client = S3Client .create ();
584- URI s3Uri = new URI (targetUrl );
543+ URI s3Uri = URI . create (targetUrl );
585544 String bucketName = s3Uri .getHost ();
586- String key = s3Uri .getPath ().substring (1 );
545+ String path = s3Uri .getPath ().substring (1 );
587546
588- LOG .info (String .format ("Copying %s to s3://%s/%s" , toUpload , bucketName , key ));
589- s3Client .putObject (PutObjectRequest .builder ().bucket (bucketName ).key (key ).build (), toUpload );
547+ LOG .info (String .format ("Copying %s to s3://%s/%s" , toUpload , bucketName , path ));
548+ s3Client .putObject (PutObjectRequest .builder ().bucket (bucketName ).key (path ).build (), toUpload );
590549
591550 return null ;
592551 };
0 commit comments