How to download pom file from maven repository given g:a:v using python

问题描述 投票:0回答:1

我想在 pyton 中创建一个简短的应用程序,能够以

G:A:V
(group:artefact:version)的形式输入 java pakage 例如:
g:javax.servlet a:javax.servlet-api v:4.0.1
并输出其 pom 文件。

我做的第一次尝试是在 https://central.sonatype.com 上手动插入搜索字符串

g:javax.servlet a:javax.servlet-api v:4.0.1
一切顺利。

但我想让 python 做到这一点。所以我写了

    url = "https://search.maven.org/solrsearch/select?q="
    src = "g:javax.servlet a:javax.servlet-api v:4.0.1"

    response = requests.get(url + src)
    print(response.content)

我得到了:

    {
   "responseHeader":{
      "status":0,
      "QTime":5,
      "params":{
         "q":"g:javax.servlet a:javax.servlet-api v:4.0.1",
         "core":"",
         "indent":"off",
         "fl":"id,g,a,v,p,ec,timestamp,tags",
         "start":"",
         "sort":"score desc,timestamp desc,g asc,a asc,v desc",
         "rows":"20",
         "wt":"json",
         "version":"2.2"
      }
   },
   "response":{
      "numFound":10095,
      "start":0,
      "docs":[
         {
            "id":"javax.servlet:javax.servlet-api:4.0.1",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.1",
            "p":"jar",
            "timestamp":1524208760000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0",
            "p":"jar",
            "timestamp":1502819985000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0-b07",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0-b07",
            "p":"jar",
            "timestamp":1496427547000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0-b06",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0-b06",
            "p":"jar",
            "timestamp":1495641725000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0-b05",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0-b05",
            "p":"jar",
            "timestamp":1490828464000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0-b04",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0-b04",
            "p":"jar",
            "timestamp":1489699571000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0-b03",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0-b03",
            "p":"jar",
            "timestamp":1488422457000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0-b02",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0-b02",
            "p":"jar",
            "timestamp":1486164134000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:4.0.0-b01",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"4.0.0-b01",
            "p":"jar",
            "timestamp":1444350065000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1.0",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1.0",
            "p":"jar",
            "timestamp":1366933959000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b09",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b09",
            "p":"jar",
            "timestamp":1366781024000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b08",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b08",
            "p":"jar",
            "timestamp":1365184903000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b07",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b07",
            "p":"jar",
            "timestamp":1362725874000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b06",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b06",
            "p":"jar",
            "timestamp":1360618713000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b05",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b05",
            "p":"jar",
            "timestamp":1357844186000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b04",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b04",
            "p":"jar",
            "timestamp":1355787467000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b03",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b03",
            "p":"jar",
            "timestamp":1355244613000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b02",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b02",
            "p":"jar",
            "timestamp":1347054630000,
            "ec":[
               "-sources.jar",
               "-javadoc.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.1-b01",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.1-b01",
            "p":"jar",
            "timestamp":1341521966000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         },
         {
            "id":"javax.servlet:javax.servlet-api:3.0.1",
            "g":"javax.servlet",
            "a":"javax.servlet-api",
            "v":"3.0.1",
            "p":"jar",
            "timestamp":1310499653000,
            "ec":[
               "-javadoc.jar",
               "-sources.jar",
               ".jar",
               ".pom"
            ],
            "tags":[
               "technology",
               "collaboration",
               "source",
               "java"
            ]
         }
      ]
   }
}

如何在这里检索 POM 文件表格?

python maven repository pom.xml
1个回答
0
投票

我找到了解决方案:

def download(self, g, a, v, extension="pom"):
    '''
    This method will manage the different parts of the downloading process
    :param g: groupID
    :param a: artefactID
    :param v: artefact version
    :param ext: set pom if you want to create a link for the pom file,
                set jar if you want to create a link for the jar file
    :return:
    '''

    self.g = g
    self.a = a
    self.v = v

    url = self.gav_to_url(g, a, v, extension)   # create the url from the GAV format
    url = url.replace(".jar", "-sources.jar")   # specify to download the source files
    # the following if is not mandatory
    if self.is_downloadable(url):
        return self.perform_download(url)       #return the downloaded file
    else:
        print(f"The url = {url} is not a downloadable URL")
        exit(1)
    return 0
# -----------------------------------------------------------------------------
def gav_to_url(self, g, a, v, ext):
    '''
    This method creates the JAR or POM file link from G:A:V coordinates
    :param g: groupID
    :param a: artefactID
    :param v: artefact version
    :param ext: set pom if you want to create a link for the pom file,
                set jar if you want to create a link for the jar file
    :return: url pointing to the desired file: jar or pom
    '''

    gid = g.replace(".", "/")
    return self.base + gid + "/" + a + "/" + v + "/" + a + "-" + v + "."+ ext

# -----------------------------------------------------------------------------
    def perform_download(self, url):
        '''
        Do the downlaod of the file
        :param url: url of the file to be downloaded
        :return:  downloaded file
        '''

        filename = url.split("/")[-1]
        try:
        response = requests.get(url, allow_redirects=True)
        open(filename, "wb").write(response.content)    # overwritting file in case it exists
    except BaseException as be:
        print(f"Something went wrong while downloading: {url} and the following exception was raised: {be}. Exiting!"
              f"This operation is mandatory, exiting!")
        exit(1)
    return filename
© www.soinside.com 2019 - 2024. All rights reserved.