Available in Nexus Repository OSS and Nexus Repository Pro
Automation via scripts is a common scenario. The Nexus Repository Manager provides APIs that simplify provisioning and other tasks in the repository manager. These APIs can be invoked from scripts that are published to the repository manager and executed there.
The scripting language used on the repository manager is Groovy. Any editor can be used to author the scripts.
The available APIs are contained in a number of JAR files. All these files, including JavaDoc and Sources archives, are available from the Central Repository. They can be manually downloaded and extracted. E.g. the different versions and the specific JAR files for
org.sonatype.nexus:nexus-core are available in versioned directories at
This manual process can be simplified and improved by the usage of a Maven project declaring the relevant components as dependencies. An example project with this setup called
nexus-script-example and a few scripts are available in the documentation examples project.
Maven Project pom.xml Declaring the API Dependencies for Scripting
Development environments such as IntelliJ IDEA or Eclipse IDE can download the relevant JavaDoc and Sources JAR files to ease your development. Typically you would create your scripts in
The scripting API exposes specific tooling for IntelliJ IDEA that allows you to get access to code completion and similar convenience features, while writing your scripts in this Maven project. Currently the API exposes four main providers with numerous convenient methods:
The API is deliberately designed to be simple to use. It encapsulates complex configuration in single method invocations. Many of the included methods use default values that can be omitted. For example, the method to create a hosted repository using the Maven format in the simplest usage simply requires a name.
This method simply uses the default values for the rest of the parameters and is therefore equivalent to:
You can inspect the default values in the API documentation available by inspecting the declaration of the specific methods in your IDE or by viewing the JavaDoc.
In terms of overall complexity of the scripts created, it is best to break large tasks up into multiple scripts and therefore invocations.
Managing and Running Scripts
Once you have completed the creation of your script, you need to publish it to the repository manager for execution. This is done by REST API invocations against the endpoint:
This endpoint accepts JSON-formatted payloads with your script as the
Example JSON formatted file
maven.json with a simple repository creation script:
The JSON file
maven.json located in the current directory can be published to the repository manager with an HTTP POST like:
A list of scripts stored on the repository manager can be accessed with:
The same call with a script name appended returns the actual script content.
A script can be executed by sending a POST to the run method of the specific script.
A successful execution should result in a
HTTP/1.1 200 OK.
Scripts can be removed with a HTTP DELETE operation to the specific script:
Scripts can receive run-time parameters via the REST API
and receive them as arguments that have to be parsed by the script as desired
Interaction with the REST API for scripts can be done with any scripting language capable of HTTP calls as mentioned above. In the following section you can find some further detailed examples.
The API for scripts is capable of a number of different tasks. This section provides examples for script writing, publishing and executing them.
simple-shell-example project in the scripting section of the documentation examples project includes a number of JSON file with simple scripts:
simplest script to create a hosted Maven repository
simple script to create a hosted and proxy repository as well as a repository group for npm usage
simple script to create a hosted and proxy repository as well as a repository group for bower usage
parameterized script to enable or disable anonymous access
Simple shell scripts are added to contain the curl invocations to manage scripts via the REST API:
Upload a specified JSON file
Delete a script specified by its name
List all deployed scripts
Run a script specified by its name
Run the anonymous script on the server with the parameter true or false
Update an existing script by specifying the name and the JSON file to use for the update
An example sequence of creating and running a script is:
Subsequently you could list all scripts and delete the maven script with
Since scripts are typically longer than a single line and creating them in a separate file in the IDE is recommended, using a helper script that formats a .groovy file into a JSON file and submits it to the repository manager can be a convenient approach.
complex-script project in the scripting section of the documentation examples project includes an example implementation using Groovy invoked from a shell script. All scripts in this folder can be published and executed via the
provision.sh file. This results in the download of all required dependencies and the upload and execution of the referenced script. Alternatively, you can provision the scripts individually:
The following scripts are available:
creates a new blob store and uses it for a hosted raw repository
disables anonymous access, creates a new administrator account, creates a new role with a simple expansion to anonymous user role and a user, creates a new role with publishing access to all repositories and a user
configures the base URL capability and a proxy server
Logging from your scripts into the repository manager logs is automatically available and performed with the usual calls
The result of the last script line is by default returned as a string. This can be a message as simple as
Success! or more complex structured data.
For instance, you can easily return JSON using built-in Groovy classes like:
which looks like:
Passing parameters to the script can use JSON encoded arguments like:
which in turn can be parsed using the
JsonSlurper class in the script: