Sonatype Nexus 3.0 does not have the REST API which was available in Nexus 2.x (see the discussion here). This provides a challenge in case you want to automate certain tasks. Nexus 3 does provide a Groovy API however which allows you to write your own scripts and upload them to Nexus. You can then call your scripts and use the JSON result. In order to get this working however, several things need to be done. First the script needs to be developed (during which code completion comes in handy). Next the script needs to be condensed to a single line and put in a JSON request. After that, the JSON request needs to be send to a specific endpoint. You can imagine this can be cumbersome. Sonatype has provided Groovy scripts to deploy Groovy scripts. See here. I’ve created something similar using Python so you do not require a download of dependencies, a JVM and a Groovy installation to perform this task. This makes it more easy to do this from for example a build-server.
Getting up and running
First you can install your development environment. The documentation mentions IntelliJ IDEA. The community edition supports Groovy editing and code completion so I installed that. Next you can download sample code from here. Below is an example of how the project structure looks and working code completion. It is quite easy to get this part working.
Since Nexus 3 is (at the moment of writing) quite new, there is not much information about the API online available yet. There are some samples and links at: https://books.sonatype.com/nexus-book/3.0/reference/scripting.html#scripting-configuration.
You can download Javadoc JAR files from http://repo1.maven.org/maven2/org/sonatype/nexus. These can help you understanding the class structure and how to get from one class to another. Also it helps a lot to watch the YouTube movies mentioned here (thank you Manfred!).
Building your script
The mechanism by which you can deploy a script is interesting to say the least. You have to create a JSON request which contains your Groovy script and send it to an API to deploy your API. Your Groovy script needs to be a single line in this JSON file for it to remain valid JSON. Also you have to take into account the JSON file uses ” characters. Your script should thus use ‘ characters to delimit strings. You can POST your JSON file to http://[HOST]:[PORT]/service/siesta/rest/v1/script. Next you can call the script by calling http://[HOST]:[PORT]/service/siesta/rest/v1/script/[scriptname]/run”. You have the option to use arguments in your script.
It is nice you can use an API to upload and run Groovy scripts and use an IDE to write Groovy scripts with code completion support, however there is no ‘one-button-deploy’ option to quickly test your script.
Running locally from IntelliJ IDEA is also not straightforward because your script will miss the context from running on the server. The script itself cannot be used as is to send to the server since it cannot contain line breaks in the code. Also sending the script will fail if the script has been previously deployed, thus it should first be undeployed. Sample shell scripts are available to do all these tasks, however during development it can be tedious to execute them all constantly one after each other after every change in the code. Also the shell scripts of course are *NIX specific and will not execute on Windows (unless you are using Bash on Ubuntu on Windows, Cygwin, a VM or something similar). Next to that, the more complex example provided, provides you with Groovy scripts but they have some dependencies. They require for example downloading of dependencies, a JVM and a Groovy installation.
That’s why I wrote the below Python script. Why Python? Well, next to personal preference, also because it is present on most Linux systems and can easily be installed on Windows as well. In my opinion it is a bit more flexible/readable than shell scripts or Groovy scripts and has few dependencies.
You can download the script code here. I’ve also supplied an IntelliJ workspace/project to complete the example. You of course need to replace the obvious variables like script location and the Nexus host name, port, username, password.
Disclaimer: the script has not been tested thoroughly and has not been put to use in an enterprise environment. Especially the part where the Groovy script is rewritten to be put inside a JSON request can use some tweaking.