Search code examples
artifactoryartifactory-query-lang

Artifactory Query Language - Fetch Artifact Name and Version - Search based on Group


I would like to fetch all artifact name and their versions which are under specific group using AQL. Can you please help with the API endpoint and AQL.

Below the GET REST API gets me the versions related to runner on group com.load.runners. But I like to get all the artifacts and their versions from group com.load.runners.

https://localhost/artifactory/api/search/versions?g=com.load.runners&a=runner&repos=maven

Solution

  • There seems to be no direct way which is simple and performant enough to do that using AQL.
    BUT-
    What you can do is use prior knowledge on the Maven layout (how Maven artifacts are stored in the repository), use AQL to query for relevant items (files), and then extract the information you need.

    Relying on *.pom files (since every artifact version has exactly one) in the Maven layout (where a . is replaced with a /):

    <groupId>/<artifactId>/<version>/<artifactId>-<version>.pom
    

    For example, the path to the .pom file of the artifact com.load.runners:runners:1.0.0 will be:

    com/load/runners/runner/1.0.0/runner-1.0.0.pom
    

    With this knowledge you can run the following query:

    POST /artifactory/api/search/aql
    Content-Type: text/plain
    
    items.find({
      "repo": "maven",
      "path": {
        "$match": "com/load/runners/*"
      },
      "name": {
        "$match": "*.pom"
      }
    }).include("path")
    

    This will return the paths of all the .pom files, without the files name. For example:

    {
      "results": [
        {
          "path": "com/load/runners/runner/1.0.0"
        },
        {
          "path": "com/load/runners/runner/2.0.0"
        },
        {
          "path": "com/load/runners/other-runner/1.0.0"
        },
        ...
      ]
    }
    

    So now, all you have to do is to extract the information from these paths - split by / where the last part is the version (e.g. 1.0.0), the one before last is the artifactId (e.g. runner), and the rest is the groupId where all / need to be replaced with . (e.g. com.load.runners). Note that you'll need to do the aggregation of versions to artifacts on your own.