Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use REST API to create repositories in sonatype Nexus

how do i create a repository through the REST API and was hoping someone could help me with the xml and curl script used.

like image 940
Antony Avatar asked Dec 18 '22 10:12

Antony


2 Answers

TLDR

For Nexus3, use one of the example scripts from here to create your desired repository using the Script API.


Sonatype Nexus 3

The REST APIs have undergone (and I think are still undergoing) a bit of a revamp since version 2.

Repositories API

The current Repositories API is in BETA, and has only one endpoint to list repositories... so that won't work.

Script API

The Script API (v1, not BETA), allows tapping into the underlying Nexus libraries via Groovy. You can use this API to create and execute a script to create a repository.

e.g. Creating NPM repositories

Create a script to create NPM repositories, then execute the script.

curl -X POST -u admin:admin123 --header 'Content-Type: application/json' \
    http://127.0.0.1:8081/service/rest/v1/script \
    -d '{"name":"npm","type":"groovy","content":"repository.createNpmHosted('\''npm-internal'\'');repository.createNpmProxy('\''npmjs-org'\'','\''https://registry.npmjs.org'\'');repository.createNpmGroup('\''npm-all'\'',['\''npmjs-org'\'','\''npm-internal'\''])"}'
curl -X POST -u admin:admin123 --header "Content-Type: text/plain" 'http://127.0.0.1:8081/service/rest/v1/script/npm/run'

Recommended reading:

  • Writing Scripts
  • Managing and Running Scripts
  • Examples
like image 191
Nick Grealy Avatar answered Feb 01 '23 22:02

Nick Grealy


Firstly, if you are curious on how to do these things, you can use this article by us at Sonatype on how to learn the Nexus Repository 2 REST API: http://www.sonatype.org/nexus/2015/01/26/learn-the-nexus-rest-api-automating-sonatype-nexus/

Secondly, here's an example one of our internal team members came up with:

This is a POST request to http://localhost:8081/nexus/service/local/repositories. You can use it like this with curl:

curl -H "Content-Type: application/json" -d @repo.json -u admin:admin123 http://localhost:8081/nexus/service/local/repositories

Here's some sample content for the "repo.json" file referenced by the above, which shows how to create a proxy Maven repository. Note that the payload for creating other types of repositories will be different, use the article above to find out what those are.

{
    "data": {
        "repoType": "proxy",
        "id": "somerepo",
        "name": "Some Repo Name",
        "browseable": true,
        "indexable": true,
        "notFoundCacheTTL": 1440,
        "artifactMaxAge": -1,
        "metadataMaxAge": 1440,
        "itemMaxAge": 1440,
        "repoPolicy": "RELEASE",
        "provider": "maven2",
        "providerRole": "org.sonatype.nexus.proxy.repository.Repository",
        "downloadRemoteIndexes": true,
        "autoBlockActive": true,
        "fileTypeValidation": true,
        "exposed": true,
        "checksumPolicy": "WARN",
        "remoteStorage": {
            "remoteStorageUrl": "http://someplace.com/repo",
            "authentication": null,
            "connectionSettings": null
        }
    }
}
like image 41
DarthHater Avatar answered Feb 01 '23 23:02

DarthHater