Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 534A6200828 for ; Fri, 13 May 2016 16:12:36 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 51D0916099F; Fri, 13 May 2016 14:12:36 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 735C11602BE for ; Fri, 13 May 2016 16:12:35 +0200 (CEST) Received: (qmail 73810 invoked by uid 500); 13 May 2016 14:12:34 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 73800 invoked by uid 99); 13 May 2016 14:12:34 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 May 2016 14:12:34 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 357FC180501 for ; Fri, 13 May 2016 14:12:34 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.799 X-Spam-Level: * X-Spam-Status: No, score=1.799 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id mqORjOVSTHig for ; Fri, 13 May 2016 14:12:33 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id D3E8B5FAFA for ; Fri, 13 May 2016 14:12:32 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 231BFE0185 for ; Fri, 13 May 2016 14:12:32 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 24F4F3A0113 for ; Fri, 13 May 2016 14:12:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1743678 - /commons/scripts/NexusGet.java Date: Fri, 13 May 2016 14:12:32 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160513141232.24F4F3A0113@svn01-us-west.apache.org> archived-at: Fri, 13 May 2016 14:12:36 -0000 Author: sebb Date: Fri May 13 14:12:31 2016 New Revision: 1743678 URL: http://svn.apache.org/viewvc?rev=1743678&view=rev Log: Initial commit of tool to download Maven artifacts from Nexus. TODO filter downloads, e.g. omit jars Added: commons/scripts/NexusGet.java (with props) Added: commons/scripts/NexusGet.java URL: http://svn.apache.org/viewvc/commons/scripts/NexusGet.java?rev=1743678&view=auto ============================================================================== --- commons/scripts/NexusGet.java (added) +++ commons/scripts/NexusGet.java Fri May 13 14:12:31 2016 @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +/** + * Utility class to parse Nexus staging repo and optionally download all the Maven artifacts for a given RC. + *

+ * $ java NexusGet nexusId [download folder] + *

+ * The nexusId is either a 4 digit id, in which case it is assumed to be under orgapachecommons, + *
+ * or it is the name of a /content/ folder parent as in {project}-nnnn below + *
+ * https://repository.apache.org/service/local/repositories/{project}-nnnn/content/ + *
+ * or it is a full URL as above (can also be a subdirectory of content) + *

+ * Note that Nexus does not redirect if the trailing "/" is missing + */ +public class NexusGet { + + private static final String COMMONS_BASE = "https://repository.apache.org/service/local/repositories/orgapachecommons-%s/content/"; + private static final String NEXUS_BASE = "https://repository.apache.org/service/local/repositories/%s/content/"; + + public static void main(String[] args) throws Exception { + if (args.length == 0) { + System.err.println("Need Nexus number, e.g. 1234 or full URL to /content/ or below"); + return; + } + + final String param = args[0]; + final String start; + if (param.length() == 4) { // we assume commons Nexus id + start = String.format(COMMONS_BASE, param); + } else if (!param.contains("/")){ // assume it is orgapachecommons-1234 + start = String.format(NEXUS_BASE, param); + } else { // Must be a full URL + start = param; + } + final String folder; + if (args.length > 1) { + folder = args[1]; + } else { + folder = null; + } + XMLInputFactory fact = XMLInputFactory.newFactory(); + parseNexus(start, folder, fact); + } + + private static void download(URL url, File localFilename) throws IOException { + byte[] buffer = new byte[4096]; + int len; + URLConnection urlConn = url.openConnection(); + try (FileOutputStream fos = new FileOutputStream(localFilename);InputStream is = urlConn.getInputStream();){ + while ((len = is.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + } + } + + /* + * We assume that the XML contains an entry with the tag + * whose value is either a file name or a directory with trailing "/". + * So we don't need to bother to check the tag. + */ + private static void parseNexus(String name, String folder, XMLInputFactory fact) + throws XMLStreamException, IOException, FileNotFoundException { + URLConnection urlConn = new URL(name).openConnection(); + try (InputStream is = urlConn.getInputStream()) { + XMLStreamReader xr = fact.createXMLStreamReader(is); + while(xr.hasNext()) { + if (xr.isStartElement()) { + if (xr.getName().toString().equals("resourceURI")) { + xr.next(); + final String text = xr.getText(); + if (text.endsWith("/")) { + parseNexus(text, folder, fact); + } else { + getFile(text, folder); + } + continue; + } + } + xr.next(); + } + xr.close(); + } + } + + private static void getFile(final String nexusUrl, String folder) throws IOException { + final URL url = new URL(nexusUrl); + final String name = new File(url.getPath()).getName(); + if (name.startsWith("maven-metadata.xml") || name.equals("archetype-catalog.xml")) { + return; // not wanted + } + if (folder != null) { + final File path = new File(folder, name); + if (path.exists()) { + System.out.println(path + " exists locally"); + } else { + System.out.println(nexusUrl + " => " + path); + download(url, path); + } + } else { + System.out.println(nexusUrl); + } + } + +} Propchange: commons/scripts/NexusGet.java ------------------------------------------------------------------------------ svn:eol-style = native