How can I programmatically upload an artifact into Nexus 2?

There are several ways to upload artifacts into Nexus 2 without running a build. (See this article for Nexus 3)

Direct Deploy

You can do an HTTP PUT of a file into /content/repositories/<repo-id>/<path-of-file>.  Using curl you can do this with:

curl -v -u admin:admin123 --upload-file pom.xml http://localhost:8081/nexus/content/repositories/releases/org/foo/1.0/foo-1.0.pom

Maven "deploy-file" Deployment

You can deploy files using the "deploy-file" goal on the Maven deploy plugin

Example without a pom file:

mvn deploy:deploy-file -DgroupId=com.somecompany -DartifactId=project -Dversion=1.0.0 -DgeneratePom=true -Dpackaging=jar -DrepositoryId=nexus -Durl=http://localhost:8081/nexus/content/repositories/releases -Dfile=target/project-1.0.0.jar

With a pom file:

mvn deploy:deploy-file -DgeneratePom=false -DrepositoryId=nexus -Durl=http://localhost:8081/nexus/content/repositories/releases -DpomFile=pom.xml -Dfile=target/project-1.0.0.jar

This plugin goal also allows deployment of snapshot versions, and the plugin will take care of calculating the timestamped version needed:

mvn deploy:deploy-file -DgroupId=com.somecompany -DartifactId=project -Dversion=1.0.0-SNAPSHOT -DgeneratePom=true -Dpackaging=jar -DrepositoryId=nexus -Durl=http://localhost:8081/nexus/content/repositories/snapshots -Dfile=target/project-1.0.0-SNAPSHOT.jar

Note: The "repositoryId" parameter is not a Nexus repository ID, it is the ID of a server section in your settings.xml file which has then credentials needed for deployment.

<servers>
...
  <server>
      <id>nexus</id>
      <username>deployment</username>
      <password>deployment123</password>
    </server>
  </servers>

REST API Upload

Alternatively you can use the REST API.  This requires a multi-part form POST to /service/local/artifact/maven/content.

Here are some examples using curl.

1) Uploading an artifact and generating a pom file:

curl -v -F r=releases -F hasPom=false -F e=jar -F g=com.test -F a=project -F v=1.0 -F p=jar -F file=@project-1.0.jar -u admin:admin123 http://localhost:8081/nexus/service/local/artifact/maven/content

2) Uploading an artifact with a pom file:

curl -v -F r=releases -F hasPom=true -F e=jar -F file=@pom.xml -F file=@project-1.0.jar -u admin:admin123 http://localhost:8081/nexus/service/local/artifact/maven/content

3) Uploading a pom file by itself (with no artifact):

curl -v -F r=releases -F hasPom=true -F file=@pom.xml -u admin:admin123 http://localhost:8081/nexus/service/local/artifact/maven/content
Important: The file parameters MUST COME LAST or the curl commands will not work. If you are uploading a pom file, then the pom file parameter must come before the rest of the file parameters, but after all the other parameters.

For reference, here are all the available form parameters for this endpoint:

r : repository

hasPom - whether you are supplying the pom or you want one generated. If you are uploading a pom, then the parameters g, a, v, p, and c are not needed as part of the command as those values are extracted from the pom.ml

e - extension

g - group id

a - artifact id

v - version

p - packaging

c - classifier (optional, not shown in examples above)

file - each file to be uploaded, use one file parameter per file.

Note: Uploading via the REST API requires that the user has the "artifact upload" privilege in Nexus, in addition to create and update privileges on the target repository.

Uploading Multiple Artifacts at Once

Enable the Nexus Unpack Plugin

In Nexus Professional 2.7.x+, the plugin is already installed by default.

In Nexus Professional 2.6.x and earlier, the optional plugin needs to be installed. You can enable this plugin in earlier versions of Nexus Professional by moving "$NEXUS_HOME/nexus/WEB-INF/optional-plugins/nexus-unpack-plugin-<version>" into "$NEXUS_HOME/nexus/WEB-INF/plugin-repository" and restarting the server.

In Nexus OSS you need to manually install the plugin.  Download the bundle.zip which matches your nexus version, and unpack it in $NEXUS_HOME/nexus/WEB-INF/plugin-repository. Then restart the server.

Use the Nexus Unpack Plugin

Once enabled, you can use a REST endpoint to upload zip files via PUT request. This takes the form of (ensure that there is no trailing slash in the URL):

/service/local/repositories/{repositoryId}/content-compressed/path/you/want/them/to/appear/in

So for example, using curl:

curl --upload-file my.zip -u admin:admin123 -v http://localhost:8081/nexus/service/local/repositories/releases/content-compressed/foo/bar

The files will be unpacked and deployed individually on the server side, just as if they had been individually deployed from a client using standard deploy mechanisms.

If the zipped file has a directory structure of "com/company/project/...", then you want to deploy to the root level of repository.

curl --upload-file my.zip -u admin:admin123 -v http://localhost:8081/nexus/service/local/repositories/releases/content-compressed

If the zipped file has a directory structure example of "project/...", then you want to deploy to the "com/company" level of repository.

curl --upload-file my.zip -u admin:admin123 -v http://localhost:8081/nexus/service/local/repositories/releases/content-compressed/com/company

Note that the user performing this deploy must have the "Unpack" privilege in Nexus, as well as create privileges in the target repository.

Also note that the files in the zip must be in the standard repository layout of the target repository.

Uploading using Powershell

There is an external guide which describes how to upload artifacts using Powershell.

Have more questions? Submit a request

45 Comments

  • 0
    Avatar
    Ritesh Keloth

    I tried the below command for uploading the artifact(a sample txt file) using GAV parameters. But getting the error as "HTTP/1.1 500 Server Error"

     

    curl -v -F "r=Release" -F "g=com.name" -F "a=Ritesh.txt" -F "v=1.2.1" -F "p=txt" -F "file=Ritesh.txt" -u <<username>>:<<password>> <<httpURL>>

     

    Is there any mistake in this ?

     

    Regards

    Ritesh

  • 0
    Avatar
    Ritesh Keloth

    And tried with this command as well .

    curl -v -F r=Release -F hasPOM=false -F e=txt -F g=com.test -F a=Ritesh -F v=1.2.1 -F p=txt -F file=Ritesh.txt -u <<username>>:<<password>> <<httpURL>>
  • 0
    Avatar
    Ritesh Keloth

    And tried with this command as well . But no luck. :(

    curl -v -F r=Release -F hasPOM=false -F e=txt -F g=com.test -F a=Ritesh -F v=1.2.1 -F p=txt -F file=Ritesh.txt -u <<username>>:<<password>> <<httpURL>>

    Can you help me with this please ?

  • 0
    Avatar
    Rich Seddon

    If you're getting a 500 error it means an unexpected exception is getting thrown in the server.  There is likely something wrong with your installation, file a support ticket for this and we can help.

  • 0
    Avatar
    Dorkbox

    I have a writeup on StackExchange detailing exactly how to upload, close, and promote an artifact DIRECTLY to nexus (in my case, oss.sonatype.org). Using curl (based on my java examples) is easy enough, as all of the header/body information is clearly outlined. This was done to remove the dependency on maven libraries for deployment.

    http://stackoverflow.com/a/33845202/2101812

  • 0
    Avatar
    Daniel Holmes

    Question on uploading a file. We are trying to upload Debian (.deb) files to later retrieve with the Nexus APT Plugin from the exposed apt repo. However, the filename in the repository is constructed from the artifact/version/classifier arguments to the call with the maven convention of '-' separators. The Debian styleguide is that "_" is used as the separator.

    Is there a way via the file upload method I could get the file in the repo to have the name that I want?

    If not, could the UnpackPlugin be a potential method/workaround to make this happen?

  • 0
    Avatar
    Rich Seddon

    Upload via REST will enforce maven artifact naming conventions. But direct upload via PUT to /content doesn't. Have you tried using that?

  • 0
    Avatar
    prabhu radhakrishnan

    I do have access to my repo i.e

    https://nexus.xxx.com/content/repositories/level0/com/org/banker

    But when I tried to upload content via http POST from javascript ajax callusing url

    https://nexus.xxx.com/content/repositories/level0/com/org/banker/aaa/1.0/aaa-1.0.txt 

    i get 403 Forbidden error,

    My requirement is to upload files into different project repositories - could you help us here what is wrong with this http url /process, I am able to post(create file) in to

    http://nexus-repository.xxx.com/nexus/content/repositories/thirdparty/com/org/ banker/ 

    What is the difference in accessing level0 vs thirdparty repos

  • 0
    Avatar
    Rich Seddon
    You can't POST to a /content/repositories URL. You need to use a PUT. POST is for upload to /service/local/artifact/maven/content.
  • 0
    Avatar
    Daniel

    Thank you SOOO much Rich! 

     

    I have been Googeling my behind off trying to deploy a file (generated via Pybuilder) into a Nexus server, without the need for a POM file.

     

    This example answered my prayers:

    mvn deploy:deploy-file -DgroupId=com.somecompany -DartifactId=project -Dversion=1.0.0 -DgeneratePom=true -Dpackaging=jar -DrepositoryId=nexus -Durl=http://localhost:8081/nexus/content/repositories/releases -Dfile=target/project-1.0.0.jar
  • 0
    Avatar
    Martin Gainty

    http://stackoverflow.com/questions/4029532/upload-artifacts-to-nexus-without-maven

    Programatic upload of artifact to known nexus repository id

  • 0
    Avatar
    kalyan

     

    Is there any script in Powershell which will help me to upload .Zip files to Nexus. I used above script in Linux machine but in my new scenario we have windows machine.I got stuck.Please help me out.

  • 0
    Avatar
    Mahendra Surani

    Is there any script in Powershell which will help me to upload .Zip files to Nexus. I used above script in Linux machine but in my new scenario we have windows machine.I got stuck.Please help me out.

    I am not familiar with Powershell, but I found the following links that may help you.

    http://stackoverflow.com/questions/23552704/how-to-recreate-a-working-curl-command-with-invoke-webrequest-in-powershell

    https://technet.microsoft.com/en-us/library/hh849901.aspx

    The other option is to install cygwin on the windows machine.

     

     

  • 0
    Avatar
    Manfred Moser

    @kalyan ... here is a blog post that shows how to upload with Powershell.

     

    http://www.sonatype.org/nexus/2016/04/14/uploading-artifacts-into-nexus-repository-via-powershell/

  • 0
    Avatar
    Peter Lynch

    We are closing this article for comments.

    If you have a support license, please contact us by submitting a support ticket.

    If you do not have a support license, please use our Nexus Users List or our other free support resources.

Article is closed for comments.