Search code examples
mavennexussonatype

Creating a scheduled task in Nexus via the REST API


One of the parameters that the schedules REST API endpoint of Nexus provides is the schedule. It basically defines the frequency with which the created task will be executed. The available options in the UI are: manual, once, hourly, daily, weekly, monthly and advanced.

However, when I try to create a scheduled task via the REST API and I specify a schedule that is different than manual, I get a 500 status code in the reply (and that means the server encountered an unexpected condition that did not allow it to fulfil the request). My guess is that I'm missing some additional parameters, because, via the Nexus UI, I see that choosing (for instance) an hourly schedule presents a few extra options (e.g. start date and start time).

As explained in the API's documentation, the schedulesend point provides a way to pass extra parameters in key, value pairs (via the properties element).

My question is two fold:

  1. Is my assumption about missing parameters correct?
  2. If so, then what are the correct formats for the key, value pairs for each of the possible schedules?

Solution

  • Disclaimer: the following works with nexus 2.14. I think nexus 3.0 doesn't have a REST-API and for 3.1, I don't know if it has one or no.

    So,
    another way of figuring out which data is required for a scheduled task is to first do a HTTP GET on http://<your-repo>/nexus/service/local/schedules which will return all the scheduled tasks you currently have. Among other infos like their name and type, the output also contains, their respective IDs.

    (Make sure that you first created via the UI a task that is similar to the one you are going to create via REST).

    Then, by doing a GET on http://<your-repo>/nexus/service/local/schedules/<ID> with the scheduled-task-ID you are interested in, you get all the stuffs and parameters that you need.

    In my case, I wanted to create a scheduled task that removes old release artifacts and the data which I needed to post to nexus looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <scheduled-task>
      <data>
        <enabled>true</enabled>
        <name>Remove Releases from 123-releases-repo</name>
        <typeId>ReleaseRemoverTask</typeId>
        <schedule>daily</schedule>
    <!-- startDate is some timestamp, in millis, required to avoid a nasty NumberFormatException -->
        <startDate>1234567</startDate>
        <recurringTime>03:00</recurringTime>
        <properties>
          <scheduled-task-property>
            <key>numberOfVersionsToKeep</key>
            <value>1</value>
          </scheduled-task-property>
          <scheduled-task-property>
            <key>indexBackend</key>
            <value>false</value>
          </scheduled-task-property>
          <scheduled-task-property>
            <key>repositoryId</key>
            <value>123-releases-repo</value>
          </scheduled-task-property>
        </properties>
      </data>
    </scheduled-task>