SoFunction
Updated on 2025-04-12

Implementation of uploading jar package using mvn deploy command

1. Background

In daily Java development, Maven private server, as the core hub of team collaboration, carries the important task of version dependency distribution. However, when one day I found that the key JAR package in the private server was mysteriously lost, and the local repository still retains historical build files, how can I quickly "resurrect" the missing dependencies from the local area? At this time, how to quickly repost jar packages in the local repository to the private server becomes the key to restoring the build environment. This article will introduce in detail how to use the mvn deploy:deploy-file command to publish jar packages in the local repository to the private server, and share the problems and solutions that may be encountered in the process to help developers solve similar problems efficiently.

2. Environment

  • Nexus Repository Manager OSS 2.15.1-02
  • Apache Maven 3.3.9
  • Java version: 17.0.9
  • win 10

3. Configure nexus upload account

Configure the maven file, add 2 server nodes to the configuration file, and enter the username and password of nexus. Remember the id in the configuration to use in the command.

<servers>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>******</password>
    </server>
    <server>
        <id>release</id>
        <username>admin</username>
        <password>******</password>
    </server>
</servers>

4. Execute the deploy command to upload the package

The following commands are all based on cmd, which is easy to view. They use ^ to divide the command into multiple lines. The mvn configuration is not only in the maven installation directory, but also in the user's environment configuration.
It is best to avoid interference with other configurations before executing the command. It is best to only retain the configuration in the maven directory.

  • Check the environment and execute the mvn -X command to see which configurations are loaded.
  • You can execute the mvn help:effective-settings command to view the valid configuration

1. First, you need to upload the local warehouse to jar and pom to other directories.

cd /d D:\temp\maven\

2. Confirm the package version to be uploaded

The uploaded package version determines, upload path and use the repoitoryId value. -The actual path of the Durl parameter needs to be found in the private server by yourself~~~, if the configuration is wrong, all kinds of mistakes will be made! ! !
-DrepositoryId parameter value needs to be the server id node value to the same.

SNAPSHOT package parameters

  • The package name has snapshot keyword, such as: mydtemo-0.0. The following parameter values ​​are required
  • -Durl=/nexus/content/repositories/snapshots/
  • -DrepositoryId=snapshots

Release package parameters

  • The release package without snapshot keyword, such as: mydtemo-0.0. The following parameter values ​​are required
  • -Durl=/nexus/content/repositories/releases/
  • -DrepositoryId=releases

3. Execute the upload command

If the jar package to be uploaded has a corresponding pom file and the maven coordinates in the pom do not reference variables, it is easier to specify a -DpomFile

# When specifying pomFile parameters, you do not need to fill in the three parameters groupId, artifactId, and versionmvn -e deploy:deploy-file ^
 -Dfile=./mydtemo-2025.3. ^
 -DpomFile=./mydtemo-2025.3. ^
 -Dpackaging=jar ^
 -Durl=/nexus/content/repositories/releases/ ^
 -DrepositoryId=releases 

If there is no pom file, you need to manually specify the maven coordinates. If you are careful, otherwise errors will occur, resulting in the pulling dependencies not being able to find the package.

# When there is no pom file, you need to fill in the three parameters of groupId, artifactId, and versionmvn -e deploy:deploy-file ^
 -Dfile=./mydtemo-2025.3. ^
 -DgroupId= ^
 -DartifactId=mydtemo ^
 -Dversion=2025.3.7  ^
 -Dpackaging=jar ^
 -Durl=/nexus/content/repositories/releases/ ^
 -DrepositoryId=releases 

If it is a snapshot package, the parameters are slightly different. There is another scenario here that specifies the -Dclassifier parameter. In Maven, the classifier attribute is used to distinguish different versions or variants of the same artifact. Usually, an artifact is determined by three basic elements: groupId, artifactId and version. However, in some cases, we may need to create multiple different versions or variants for the same artifact, such as source code packages, documentation packages, or platform-specific binary packages, etc.

# Specify the classifier parameter and publish the jar package to the snapshot warehousemvn -e deploy:deploy-file ^
 -Dfile=./mydtemo-spring-1.0. ^
 -DgroupId= ^
 -DartifactId=mydtemo ^
 -Dversion=spring-1.0.11-SNAPSHOT  ^
 -Dclassifier=dto ^
 -Dpackaging=jar ^
 -Durl=/nexus/content/repositories/snapshots/ ^
 -DrepositoryId=nexus-snapshots 

If you just upload a pom file, the file parameter needs to be specified.

cd /d D:\temp\maven\
# Only upload pom filesmvn -e deploy:deploy-file ^
 -Dfile=./mydemo-1.2. ^
 -DpomFile=./mydemo-1.2. ^
 -Durl=/nexus/content/repositories/releases/ ^
 -DrepositoryId=releases 

5. Execute dependency command to test download dependency

If the above goes well, it has been uploaded to the private server. The following tests the drop-down dependency, of course, you can also test it in idea.

Command format

mvn -e dependency:get ^
-Dartifact=mydtemo::jar:2025.3.7

You can also specify configuration files to get dependencies of other warehouses.

# Specify configuration file pullmvn dependency:get ^
-Dartifact=mydemo:mydemo:pom:1.2.0 ^
-s D:\maven\apache-maven-3.3.9\conf\

If you are too lazy to search in the local warehouse, you can specify the download address and just open a directory during testing.

# Specify the download directory (first download to this warehouse and then copy to -Ddest specified directory)mvn -e -X dependency:get ^
-Dartifact=:mydtemo:2025.3.7 ^
-DrepoUrl=/nexus/content/repositories/releases/ ^
-Ddest=E:\downloads\mvn-test

6. Command explanation

Command explanation: mvn deploy:deploy-file

mvn deploy:deploy-fileis a command provided by Maven to upload local files (such as jar packages or pom files) directly to Maven private servers (such as Nexus or Artifactory). The following are detailed parameters of this command:

1. Command name

  • mvn deploy:deploy-fileThis is the command name of Maven, used to upload local files to a remote repository.

2. -Dfile=

Function: Specify the path of the jar file to be uploaded.

illustrate:

  • The path can be an absolute path or a relative path.
  • It is recommended to place the jar package you want to upload in a simple directory to avoid excessively long or complicated paths.

Example:

-Dfile=target/my-artifact-1.0.

3. -DpomFile=

Function: Specify the pom file path corresponding to the jar package.

illustrate:

  • If the uploaded jar package, you must specify it at the same time-Dfileand-DpomFile
  • If you only upload pom files, just specify-DpomFile

Example:

-DpomFile=

4. -Dpackaging=

Function: Specify the file type to upload.

illustrate:

  • Common types includejarandpom
  • When uploading a jar package, the type isjar; When uploading a pom file, the type ispom

Example:

-Dpackaging=jar

5. -Durl=

Function: Specify the address of the Nexus repository to be uploaded.

illustrate:

  • The Snapshots version of the jar package must be uploaded to the Snapshots repository.
  • The Releases version of the jar package must be uploaded to the Releases repository.
  • If the type does not match (for example uploading the Releases package to the Snapshots repository), it will reportstatus code 400Exception.

Example:

-Durl=/repository/maven-releases/

6. -DrepositoryId=

Function: Specify the repository ID to use.

illustrate:

  • The ID must be within the file<server>ConfiguredidConsistent.
  • If the ID is not matched, it will reportstatus code 405Exception.

Example:

-DrepositoryId=my-nexus-repo

7. Automatically parse pom files

illustrate:

  • After the upload is successful, Maven will parse the pom file and automatically identify the storage path of the jar package.
  • Make sure the pom file is ingroupIdartifactIdandversionThe information is correct, otherwise it may cause upload failure or path errors.

Complete sample command

Here is a complete example command to upload local jar packages and pom files to the Nexus private server:

mvn deploy:deploy-file \
  -Dfile=target/my-artifact-1.0. \
  -DpomFile= \
  -Dpackaging=jar \
  -Durl=/repository/maven-releases/ \
  -DrepositoryId=my-nexus-repo

7. Frequently Asked Questions and Solutions

1. status code 400 exception

Cause: The uploaded jar package type does not match the repository type (for example, uploading the Releases package to the Snapshots repository).

Solution: Check-DurlParameters to ensure upload to the correct repository.
Details:

[ERROR] Failed to execute goal :maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact :**mydemo:jar:2025.3.7 from/to releases (/nexus/content/repositories/public/): Failed to transfer file: /nexus/content/repositories/public/com/com/mydemo/**mydemo/2025.3.7/**mydemo-2025.3.. Return code is: 400, ReasonPhrase: Bad Request.

2. status code 405 exception

  • reason:-DrepositoryIdandIn<server>Configuration mismatch.
  • Solution: CheckDocumentation, ensureidConsistent.
    Details:
[ERROR] Failed to execute goal :maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact :**mydemo:jar:2025.3.7 from/to releasesx (/nexus/content/repositories/releases/): Failed to transfer file: /..../....jar. Return code is: 401, ReasonPhrase: Unauthorized.

3. Could not find artifact Exception

  • Reason: The pom filegroupIdartifactIdorversionInformation error.
  • Solution: Check the pom file to ensure the coordinate information is correct.

4. Return code is: 401 permission issue

Cause: Authentication is required for uploading the repository, but the correct username and password are not configured.

Solution: inMedium configuration<server>Authentication information.
Error details:

[ERROR] Failed to execute goal :maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact :**mydemo:jar:2025.3.7 from/to releases (/nexus/content/repositories/releases/): Failed to transfer file: /..../....jar. Return code is: 401, ReasonPhrase: Unauthorized.

5. Cannot upload directly from the local warehouse

You need to copy the jar package of the local warehouse to another directory.

[ERROR] Failed to execute goal :maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Cannot deploy artifact from the local repository: D:\java\maven\maven_repo\com\mydemo\test\**mydemo\2025.3.7\**mydemo-2025.3.

Summarize

mvn deploy:deploy-fileIt is an important command to upload local jar packages or pom files to the Maven private server. By correct configuration-Dfile-DpomFile-Durland-DrepositoryIdand other parameters can efficiently complete the upload task. At the same time, pay attention to the correctness of the warehouse type, permissions and pom file information to avoid common errors. Sometimes maven version is high or first, it can also cause upload failure.

This is the article about uploading jar packages using the mvn deploy command. For more related contents of uploading jar packages by mvn deploy, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!