In addition to using the Nexus Repository Manager’s provided UI to upload an artifact, we can also use the RESTful API, which is also available from the Nexus Repository Manager, to do this. How is it in details? Let’s find out in this tutorial.
First, I will talk about the RESTful API provided by the Nexus Repository Manager.
If you notice, you will see in the management of the Nexus Repository Manager, the menu on the left will have a menu item called API. Click on it and you will be taken to a page containing all of the RESTful APIs that the Nexus Repository Manager supports, enabling us to manipulate artifacts through the RESTful API in the command line or code snippets easily.
You can easily view the information of each API by expanding them. Scroll down to the bottom and you will see the base URL “service/rest” and the request to the Nexus Repository Manager will look like this:
Now, I’ll try using the “/beta/components” POST API in my Nexus Repository Manager to upload an artifact!
As you can see, the API has a request parameter string of type “repository” that is required to specify which repository we need to upload the artifact to. In addition, we also need to specify the artifact information that we need to upload. For an artifact, there is all 5 information we need to declare:
- maven2.groupId specifies the Group Id of the artifact. If you upload a .pom file, you do not need this parameter.
- maven2.artifactId specifies the Artifact Id of the artifact. If you upload a .pom file, you do not need this parameter.
- maven2.version specifies the version of the artifact. If you upload a .pom file, you do not need this parameter.
- maven2.assetN: Nexus Repository Manager allows us to simultaneously upload multiple files for the same artifact. The N character in this parameter has a value of 1 to N, increasing the number of files that need to be uploaded. For example, if you need to upload .jar and .pom files, the corresponding request parameters will be maven2.asset1 and maven2.asset2. The data type of this parameter will be a binary File.
- maven2.assetN.extension: extension of the file to upload.
Here, we also have an optional parameter called maven2.generate-pom which allows us to request that the Nexus Repository Manager generate a default .pom file if we do not have the pom.xml file of the library, to upload.
Now, I will try to upload the .jar file of the wlthint3client library using this API.
I will use the curl tool on macOS to execute this request. The syntax for this command is as follows:
curl -v -u admin:admin123 POST 'http://localhost:8081/service/rest/beta/components?repository=huongdanjava-releases'
As you can see, here we also need to declare the username and password to execute the request URL.